leetcode 697
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的更多相关文章
- 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 ...
- [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 ...
- Java实现 LeetCode 697 数组的度(类似于数组的map)
697. 数组的度 给定一个非空且只包含非负数的整数数组 nums, 数组的度的定义是指数组里任一元素出现频数的最大值. 你的任务是找到与 nums 拥有相同大小的度的最短连续子数组,返回其长度. 示 ...
- 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 ...
- [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 ...
- [LeetCode] All questions numbers conclusion 所有题目题号
Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, ...
- 【LeetCode】697. Degree of an Array 解题报告
[LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...
- 697. Degree of an Array - LeetCode
697. Degree of an Array - LeetCode Question 697. Degree of an Array - LeetCode Solution 理解两个概念: 数组的度 ...
- 【LEETCODE】50、数组分类,简单级别,题目:888,1013,896,485,448,697
package y2019.Algorithm.array; import java.util.HashSet; import java.util.Set; /** * @ProjectName: c ...
随机推荐
- soundtouch 变速算法matlab实现
soundtouch变速主要采用WSOLA算法来进行变速. http://www.surina.net/soundtouch/ https://blog.csdn.net/suhetao/articl ...
- C语言-数组指针与指针数组
1.思考 下面这些声明合法吗? int array[5]; int matrix[3][3]; int * pa = array; int * pm = matrix; 问题: array代表数组首元 ...
- C#中ESRI.ArcGIS.esriSystem的引用问题
ESRI.ArcGIS.esriSystem,在引用里没有它的同名引用,其实它对应的引用为ESRI.ArcGIS.System,所以添加“ESRI.ArcGIS.System”这个引用即可
- 解决Creating Server TCP listening socket 54.179.160.162:7001: bind: Cannot assign requested address
背景:之前在测试环境搭过一个redis集群,运维把服务器重启之后我重新开启redis集群始终起不来,但是有没有任何日志,经过如下步骤最终解决问题 1.修改日志路径,根据日志查看为什么会启动失败[前期操 ...
- vscode中vim插件对ctrl键的设置
vim配置 在使用中经常想使用ctrl-c,虽然在vscode中有配置选项可以让vim与ctrl键解绑,但是这样就使用不了vim的VISUAL BLOCK.所以进行了自定义设置. 设置 - Vim C ...
- 题解【洛谷P1038/CJOJ1707】[NOIP2003提高组]神经网络
[NOIP2003]神经网络 Description 问题背景:人工神经网络( Artificial Neural Network )是一种新兴的具有自我学习能力的计算系统,在模式识别.函数逼近及贷款 ...
- 剑指offer 面试题38 字符串的排列
我惯用的dfs模板直接拿来套 class Solution { public: vector<string> Permutation(string str) { if(str.empty( ...
- js实现图片无缝轮播
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Python - 将iterable拆分成等长的数据块
说明 看文档发现一个有趣的应用(利用zip函数) 例如[1, 2, 3, 4] --> [(1, 2), (3, 4)],拆分成长度为2的数据块 Code >>> a = [1 ...
- VS2015 编译程序时提示 无法查找或打开 PDB 文件
“mode.exe”(Win32): 已加载“C:\Windows\System32\api-ms-win-core-file-l2-1-0.dll”.无法查找或打开 PDB 文件.“mode.exe ...