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.

Example 1:

Input: [1, 2, 2, 3, 1]
Output: 2
Explanation:
The input array has a degree of 2 because both elements 1 and 2 appear twice.
Of the subarrays that have the same degree:
[1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2]
The shortest length is 2. So return 2.

Example 2:

Input: [1,2,2,3,1,4,2]
Output: 6

给定非空整数nums的非空数组,该数组的度数被定义为其任何一个元素的最大频率。

你的任务是找到num的(连续的)子阵列的最小可能长度,其与nums具有相同的度数。

例1:
输入:[1,2,2,3,1]
输出:2
说明:
输入数组的度数为2,因为元素1和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:
输入:[1,2,2,3,1,4,2]
输出:6

class Solution {
public int findShortestSubArray(int[] nums) {
int maxcount = 1;
HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
for (int i : nums) {
if (hm.containsKey(i)) {
hm.put(i, hm.get(i) + 1);
if (maxcount < hm.get(i)) {
maxcount = hm.get(i);
}
} else {
hm.put(i, 1);
}
}
Set<Integer> set = hm.keySet();
int minlength = Integer.MAX_VALUE;
for (int s : set) {
int temp = Integer.MAX_VALUE;
if (hm.get(s) == maxcount) {
int i = 0, j = nums.length - 1;
while (nums[i] != s && i < j)
i++;
while (nums[j] != s && i < j)
j--;
temp = j - i + 1;
}
minlength = Math.min(temp, minlength);
}
return minlength;
}
}

  

leetcode 697的更多相关文章

  1. LeetCode 697. Degree of an Array (数组的度)

    Given a non-empty array of non-negative integers nums, the degree of this array is defined as the ma ...

  2. [LeetCode] 697. Degree of an Array 数组的度

    Given a non-empty array of non-negative integers nums, the degree of this array is defined as the ma ...

  3. Java实现 LeetCode 697 数组的度(类似于数组的map)

    697. 数组的度 给定一个非空且只包含非负数的整数数组 nums, 数组的度的定义是指数组里任一元素出现频数的最大值. 你的任务是找到与 nums 拥有相同大小的度的最短连续子数组,返回其长度. 示 ...

  4. leetcode 697. Degree of an Array

    题目: Given a non-empty array of non-negative integers nums, the degree of this array is defined as th ...

  5. [LeetCode] 697. Degree of an Array_Easy tag: Hash Table

    Given a non-empty array of non-negative integers nums, the degree of this array is defined as the ma ...

  6. [LeetCode] All questions numbers conclusion 所有题目题号

    Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, ...

  7. 【LeetCode】697. Degree of an Array 解题报告

    [LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...

  8. 697. Degree of an Array - LeetCode

    697. Degree of an Array - LeetCode Question 697. Degree of an Array - LeetCode Solution 理解两个概念: 数组的度 ...

  9. 【LEETCODE】50、数组分类,简单级别,题目:888,1013,896,485,448,697

    package y2019.Algorithm.array; import java.util.HashSet; import java.util.Set; /** * @ProjectName: c ...

随机推荐

  1. DTU DeepLearning: exercise 6

      Does batch_size have any affects in results quality? how to set the optimal batch size and number ...

  2. linux修改主机名(hostname)

    修改/etc/sysconfig/network文件并重启

  3. C#String类型转换成Brush类型

    C#String类型转换成Brush类型: using System.Windows.Media; BrushConverter brushConverter = new BrushConverter ...

  4. 0107 spring操作数据库的3个架子

    背景 数据库开发是java的核心内容之一,基础就是jdbc了: 然而直接使用jdbc,需要写大量的try-catch-finally模板代码: 管理系统使用hibernate作为orm框架比较方便,遵 ...

  5. c数据结构 -- 使用链表实现计数

    #include <stdio.h> #include <stdlib.h> typedef struct _node{ int value; struct _node *ne ...

  6. 16day 重定向符号:

    >/1> 标准输出重定向符号 2> 错误输出重定向符号 >>/1> 标准输出追加重定向符号 2>> 错误输出追加重定向符号 如何将正确信息和错误信息都输 ...

  7. 安装pecl

    $ wget http://pear.php.net/go-pear.phar $ php go-pear.phar //php版本 < 7  $ yum install php-pear // ...

  8. 【转载】Java反射机制详解

    转自:http://baike.xsoftlab.net/view/209.html#3_8 1反射机制是什么 反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对 ...

  9. springboot @ComponentScan注解

    @ComponentScan 告诉Spring从哪里找到bean. 如果你的其他包都在@SpringBootApplication注解的启动类所在的包及其下级包,则你什么都不用做,SpringBoot ...

  10. 线段树 区间查询区间修改 poj 3468

    #include<cstdio> #include<iostream> #include<algorithm> #include<string.h> u ...