LeetCode # Array # Easy # 697 Degree of an Array

题意:给定一个数组,数组的度是其中出现最多次数的元素。求,最小连续子数组的度和原数组一致。

思路:(参考最佳答案)

遍历数组,找到数组最大值,然后根据最大值设置三个长度为max+1的数组left[],right[],counts[],分别用于存储一个数第一次出现的索引、最后一次出现的索引、出现次数。然后,根据counts找到数组的度,再根据right-left求出最小的子数组长度。

 1 public class Solution {
 2     public int findShortestSubArray(int[] nums) {
 3         if(nums.length == 0 || nums == null ) return  0;
 4         int max = 0,n=nums.length;
 5         for(int num : nums){//求出最大值
 6             max=Math.max(max, num);
 7         }
 8         int[] counts = new int[max+1];
 9         int[] left  = new int[max+1];
10         int[] right  = new int[max+1];
11         for(int i =0; i<n;i++){
12             if(counts[nums[i]] == 0){
13                 left[nums[i]] = i;
14                 right[nums[i]] = i;
15             }else {
16                 right[nums[i]] = i;
17             }
18             counts[nums[i]]++;
19         }
20         int max_count =0;
21         for(int count : counts){//求出数组的度
22             max_count = Math.max(max_count, count);
23         }
24         int min = max+1;
25         for(int i=0;i<max+1 ; i++){//求出最小长度
26             if(counts[i] == max_count) {
27                 min = Math.min(min, right[i]-left[i]+1);
28             }
29         }
30         return min;
31     }
32 }

参考:https://leetcode.com/submissions/detail/154332786/

原文地址:https://www.cnblogs.com/DongPingAn/p/9043224.html

时间: 2024-11-09 02:38:29

LeetCode # Array # Easy # 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 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=

[LeetCode] 026. Remove Duplicates from Sorted Array (Easy) (C++/Java)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 026. Remove Duplicates from Sorted Array (Easy) 链接: 题目:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/ 代码(github):https://github.com/ill

[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 # 217. Contains Duplicate

Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. 题目:给定一个数组,判断其中有无重复元素,返回true或false. 思路:首

leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 说明: 1)和1比只是有重复的数字,整体仍采用二分查找 2)方法二 : 实现:  

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, otherwise return -1. You may assume no du