力扣 <数组> 697. 数组的度 中等

题目

给定一个非空且只包含非负数的整数数组 nums, 数组的度的定义是指数组里任一元素出现频数的最大值。

你的任务是找到与 nums 拥有相同大小的度的最短连续子数组,返回其长度。

示例 1:

输入: [1, 2, 2, 3, 1]     输出: 2
解释: 输入数组的度是2,因为元素1和2的出现频数最大,均为2。
连续子数组里面拥有相同度的有如下所示:
[1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2]
最短连续子数组[2, 2]的长度为2,所以返回。
示例 2: 输入: [1,2,2,3,1,4,2]    输出: 6
注意: nums.length 在1到50,000区间范围内。nums[i] 是一个在0到49,999范围内的整数。

题解

解题思路不难,就看能不能想到用三个Map了。

class Solution {
    public int findShortestSubArray(int[] nums) {
        //left存储最左边的值,right存储最右边的值,count用来计数
        Map left = new HashMap(); 
        Map right = new HashMap(); 
        Map count = new HashMap();
        for(int i = 0; i < nums.length; i++){
            int x = nums[i];
            if(left.get(x) == null) 
                left.put(x, i);
            right.put(x, i);
            count.put(x, count.getOrDefault(x, 0) + 1);
        }

        int ans = nums.length;
        int degree = Collections.max(count.values()); //最大度
        for(int x : count.keySet()){
            if(count.get(x) == degree){
                ans = Math.min(ans, right.get(x) - left.get(x) + 1); //可能有多个值数量相等 
            }
        }
        return ans;
    }
}

 

你可能感兴趣的:(数组卡片)