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 ...
随机推荐
- mongodb多字段去重
单字段去重 db.student.distinct("name"); 多字段去重 db.student.aggregate([{ $group:{ ...
- Centos7 安装VNC实现远程桌面
1.设置root用户方法: 1.# 复制一个服务设置的模板,命令如下: cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vn ...
- H5Plus 入门学习-Dcloud H5+ API调用实例
使用API Reference完整简单的操作,更多操作查看官方文档. 最后提供项目的下载地址[下载][一款移动APP演示]
- 集成unittest做接口测试
unittest接口测试 上篇已经讲了接口测试的做法,利用的是postman工具,工具始终是工具,它有一定的局限性,比如测试数据的存放,断言的方法以及上下接口关联使用灵活性.python对http接口 ...
- 使用鼠标左键事件实现VR中的Eye Gaze Input
1.光标以及光标动画的显示 using System.Collections; using System.Collections.Generic; using UnityEngine; using U ...
- 部署web应用程序到tomcat
昨天将一个web项目部署到本地的tomcat,历程很艰辛,各种报错.首先这个项目可以用eclipse内嵌的jetty启动起来,试着用tomcat容器,各种报错.以下是详细步骤: 1.用eclipse打 ...
- (ghrd)pio设置
设置中是下降沿,边沿触发.
- 钉钉内网穿透工具在windows的使用。
钉钉内网穿透工具在windows环境下使用 1.WIN+R,然后cmd,调出dos控制台 2.进入内网穿透程序ding.exe所在目录 3.执行 ./ding.exe -config=ding.cfg ...
- Centos7 FRPS
#下载Sever端 wget https://github.com/fatedier/frp/releases/download/v0.16.1/frp_0.16.1_linux_amd64.tar. ...
- 【网页浏览】怀旧xp画图网页版
非常古老的WindowsXP画图工具 传送链接