leetcode 之 Degree of an Array

1、题目描述

Given a non-empty array of non-negative integers nums, the degree of this array is defined as the maximum frequency of any one of its elements.

Your task is to find the smallest possible length of a (contiguous) subarray of nums, that has the same degree as nums.

题目是说给定一个非空数组,找出其中出现最多的元素(不只一个),然后返回数组中包含出现最多的元素的最小子数组的长度。

2、题目分析

首先使用hash表统计每个元素的出现次数,然后找出每个出现最多的元素放入一个vector中,对vector中每个元素进行统计,找出包含vector中每个元素的最小子数组。

3、代码

 1 int findShortestSubArray(vector<int>& nums) {
 2
 3         unordered_map<int ,int> m;   // 将数组中所有元素放入一个hash_table 中
 4         vector<int> maxItem(0);
 5         int maxindex = 0;
 6         for( auto n : nums )
 7             m[n]++;
 8
 9
10         for(auto itr = m.begin(); itr != m.end() ; itr++ )  // 找出出现次数最多的元素,放在一个vector中
11             if(itr->second > maxindex )
12             {
13                 maxItem.clear();
14                 maxItem.push_back(itr->first);
15                 maxindex = itr->second;
16             }
17             else if( itr->second == maxindex )
18             {
19                 maxItem.push_back(itr->first);
20             }
21
22
23         int i=0,j= nums.size()-1;     // 对每个出现次数最多的元素进行检查,找出“度”最小的。
24         int ans=nums.size();
25
26         for(auto itr = maxItem.begin(); itr != maxItem.end(); itr++)
27         {
28             i = 0;j = nums.size()-1;
29             while( nums[i] != *itr ) i++;
30             while(nums[j] != *itr ) j--;
31             ans = min(ans,j-i+1);
32         }
33
34         return ans;
35
36     }

原文地址:https://www.cnblogs.com/wangxiaoyong/p/8744436.html

时间: 2024-11-09 04:45:11

leetcode 之 Degree of an Array的相关文章

[leetcode]Array-697. Degree of an Array

Given a non-empty array of non-negative integers nums, the degree of this array is defined as the maximum frequency of any one of its elements. Your task is to find the smallest possible length of a (contiguous) subarray of nums, that has the same de

leetcode 697.Degree of an Array

题意是指在数组中取得出现频率最高的数,然后计算数之间的间隔的. 那么这里可以简单的使用map映射关系来求解的,并且统计出现的次数以及对应的位置关系的. class Solution { public: int findShortestSubArray(vector<int>& nums) { map<int,int> m; map<int,pair<int,int>> pos; int degree=0,res=INT_MAX; for(int i=

697. Degree of an Array - LeetCode

Description: Given a non-empty array of non-negative integers nums, the degree of this array is defined as the maximum frequency of any one of its elements. Your task is to find the smallest possible length of a (contiguous) subarray of nums, that ha

LeetCode: Search in Rotated Sorted Array

LeetCode: Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, othe

Leetcode | Remove Duplicates from Sorted Array I &amp;&amp; II

Remove Duplicates from Sorted Array I Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memor

LeetCode:Remove Duplicates from Sorted Array &amp;&amp; Remove Element

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example, Given input array A =

Leetcode | Search in Rotated Sorted Array I &amp; II

Search in Rotated Sorted Array I Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise re

LeetCode——Remove Duplicates from Sorted Array

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example, Given input array A = 

[LeetCode] Search in Rotated Sorted Array [35]

题目 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no dup