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=0;i<nums.size();i++){
            m[nums[i]]++;
            if(m[nums[i]]==1){
                pos[nums[i]]={i,i};
            }else{
                pos[nums[i]].second=i;
            }
            degree=max(degree, m[nums[i]]);
        }
        for(auto a:m){
            if(degree==a.second){
                res=min(res,pos[a.first].second-pos[a.first].first+1);
            }
        }
        return res;
    }
};

原文地址:https://www.cnblogs.com/newnoobbird/p/9634997.html

时间: 2024-11-09 04:55:58

leetcode 697.Degree of an Array的相关文章

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

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]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 之 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

LeetCode # Array # Easy # 697 Degree of an Array

题意:给定一个数组,数组的度是其中出现最多次数的元素.求,最小连续子数组的度和原数组一致. 思路:(参考最佳答案) 遍历数组,找到数组最大值,然后根据最大值设置三个长度为max+1的数组left[],right[],counts[],分别用于存储一个数第一次出现的索引.最后一次出现的索引.出现次数.然后,根据counts找到数组的度,再根据right-left求出最小的子数组长度. 1 public class Solution { 2 public int findShortestSubArr

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