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. Python之旅第四天(列表、元祖、字典和习题)

    第四天,感觉时间还是过得很快,今天内容确实有点多,关于list的方法实在是太多了,元组tuple感觉有点鸡肋,可能得到后面才知道他的作用吧,然后是字典,看了很多关于字典的介绍,但是这货到底是干啥用的一 ...

  2. AM335X的SD卡更新系统学习记录

    一般利用一张SD卡就能进行系统的更新,以前一直不知是什么原理,最近了解了下,对了解到的内容做个记录.使用的是AM335X平台,系统是Linux,文件系统是EXT3: 1.首先需要一张分好分区的SD卡( ...

  3. HITCON-Training-Writeup

    HITCON-Training-Writeup 原文链接M4x@10.0.0.55 项目地址M4x's github,欢迎star~ 更新时间5月16 复习一下二进制基础,写写HITCON-Train ...

  4. try/catch/finally 语句

    定义和用法 try/catch/finally 语句用于处理代码中可能出现的错误信息. 错误可能是语法错误,通常是程序员造成的编码错误或错别字.也 可能是拼写错误或语言中缺少的功能(可能由于浏览器差异 ...

  5. JS高级---函数声明和函数表达式的区别

    函数声明和函数表达式的区别 多用函数表达式 var ff=function(){}; //函数声明 // // if(true){ // function f1() { // console.log( ...

  6. C++-HDU1003-Max Sum

    时间复杂度O(n) 空间复杂度O(1) #include <cstdio> int main() { int T;scanf("%d",&T); ,n,a,l, ...

  7. DVWA全级别之CSRF(跨站请求伪造)

    CSRF(Cross-site request forgery)  CSRF,全称Cross-site request forgery,翻译过来就是跨站请求伪造,是指利用受害者尚未失效的身份认证信息( ...

  8. 购物单 && 动态规划 && 背包问题

    题目叙述的言语倒是蛮多的: 王强今天很开心,公司发给N元的年终奖.王强决定把年终奖用于购物,他把想买的物品分为两类:主件与附件,附件是从属于某个主件的,下表就是一些主件与附件的例子: 主件 附件 电脑 ...

  9. mysql5.7导出数据提示--secure-file-priv选项问题的解决方法

    mysql可使用 into outfile 参数把表中数据导出到csv,例如可用以下命令把user表的数据导出到user.csv select * from user into outfile '/t ...

  10. hdu1874 (spfa 最短路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 很简单的最短路问题,刚刚学习spfa,其实很简单,思想和一维动态规划差不多,数组d[i]表示起点 ...