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 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
Note:
nums.length
will be between 1 and 50,000.nums[i]
will be an integer between 0 and 49,999.
我的答案
import java.util.*; public class DegreeOfArray {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String num = sc.nextLine();
int[] nums = stringToInts(num);//把字符串变成数列
Map<Integer,Integer> count = count(nums);//统计数列中各种字符出现的次数
int degree = findFrequency(count);//找到数列的最高频率
//System.out.println("degree="+degree);
int shortestSubarr = findShortestSubarr(nums,degree,count);//统计最高频率的数字的子序列长度
System.out.println(shortestSubarr); }
public static int[] stringToInts(String nums){
String num[] = nums.replace("[","").replaceAll("]","").split(",");
int[] n = new int[num.length];
for(int i = 0;i<num.length;i++) {
n[i] = Integer.valueOf(num[i]).intValue();
//System.out.println(n[i]+" "+i);
}
return n;
}
public static Map<Integer,Integer> count(int[] nums) {
Map<Integer,Integer> count = new HashMap<>();
for (int item : nums){
if (count.containsKey(item)) {
count.put(item, count.get(item) + 1);
}else {
count.put(item, 1);
}
}
//System.out.println("count="+count);
return count;
}
public static int findFrequency(Map count){
int value[] = new int[count.size()];
int i=0;
Iterator<Integer> iter = count.values().iterator();
while (iter.hasNext()) {
value[i] = iter.next();
i++;
}
Arrays.sort(value);
return value[value.length-1];
}
public static int findShortestSubarr(int[] nums,int degree,Map<Integer,Integer> count){
Map<Integer,Integer> subarrMap = new HashMap<>();
Iterator<Integer> iter = count.keySet().iterator();
for (Integer key : count.keySet()){
if(count.get(key)==degree){
int formmer = 0,latter = 0;
for (int i=0;i<nums.length;i++){
if (nums[i]==key){
formmer=i;
break;
}
}
for(int i=nums.length-1;i>0;i--){
if(nums[i]==key){
latter=i;
break;
}
}
//System.out.println("FORMMER="+formmer+" latter="+latter);
subarrMap.put(key,latter-formmer+1);
}
}
int shortestSubarr = 50000;
for (Integer key : subarrMap.keySet()){
if (shortestSubarr > subarrMap.get(key)) {
shortestSubarr = subarrMap.get(key);
}
}
return shortestSubarr;
}
} 运行时间最短的答案
class Solution {
public int findShortestSubArray(int[] nums) {
Map<Integer, Integer> left = new HashMap(),
right = new HashMap(), 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);//getOrDefault方法
//用这种方法数每个元素出现多少次
} int ans = nums.length;
int degree = Collections.max(count.values());//用Collections.max找value最大值
for (int x: count.keySet()) {
if (count.get(x) == degree) {
ans = Math.min(ans, right.get(x) - left.get(x) + 1);//用这种方法找最短的子序列长度
}
}
return ans;
}
}
leetcode 697. Degree of an Array的更多相关文章
- 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 ...
- 【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_easy】697. Degree of an Array
problem 697. Degree of an Array 题意:首先是原数组的度,其次是和原数组具有相同的度的最短子数组.那么最短子数组就相当于子数组的首末数字都是统计度的数字. solutio ...
- 【LeetCode】697. Degree of an Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求出最短相同子数组度的长度 使用堆求最大次数和最小长 ...
- [LeetCode&Python] Problem 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_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 之 Degree of an Array
1.题目描述 Given a non-empty array of non-negative integers nums, the degree of this array is defined as ...
随机推荐
- 结对编程1-四则运算GUI实现(58、59)
题目描述 我们在个人作业1中,用各种语言实现了一个命令行的四则运算小程序.进一步,本次要求把这个程序做成GUI(可以是Windows PC 上的,也可以是Mac.Linux,web,手机上的),成为一 ...
- 【Alpha】Daily Scrum Meeting——Day6
站立式会议照片 1.本次会议为第五次Meeting会议: 2.本次会议在上午大课间09:40,在禹州楼召开,本次会议为30分钟讨论昨天的任务完成情况以及接下来的任务安排. 燃尽图 每个人的工作分配 成 ...
- 201521123027 <java程序设计>第八周学习总结
1.本周学习总结 1.1思维导图 2.书面作业 Q1.List中指定元素的删除(题目4-1) 1.1 实验总结 总结:判断List中是否存在指定元素,需要用到equals方法,若存在就用remove进 ...
- 201521123109《java程序设计》第七周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 2. 书面作业 ArrayList代码分析 1.1 解释ArrayList的contains源代码 源代码: pub ...
- 201521123026 《Java程序设计》第6周学习总结
1. 本章学习总结 请使用思维导图,以封装.继承.多态为核心概念画一张思维导图,对面向对象思想进行一个总结 2. 书面作业 Q1.clone方法 1.1 Object对象中的clone方法是被prot ...
- 201521123036 《Java程序设计》第3周学习总结
本周学习总结 初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化的概念.知识组织起来. 书面作业 Q1:代码阅读 public class Test1 { private ...
- 关于百度DNS的解析过程
if现在我用一台电脑,通过ISP接入互联网,那么ISP就会分配给我一个DNS服务器(非权威服务器). now,我的computer向这台ISPDNS发起请求查询www.baidu.com. 首先,IS ...
- 201521123034《Java程序设计》第十周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常与多线程相关内容. 2. 书面作业 本次PTA作业题集异常.多线程 1.finally 题目4-2 1.1 截图你的提交结果(出 ...
- postman: 用于网页调试和发送Http请求的chrome插件
一 简介 Postman 是一款功能超级强大的用于发送 HTTP 请求的 Chrome插件 .做web页面开发和测试的人员应该是无人不晓无人不用!其主要特点 特点: 创建 + 测试:创建和发送任何的H ...
- JAVA多线程高并发学习笔记(三)——Callable、Future和FutureTask
为什么要是用Callable和Future Runnable的局限性 Executor采用Runnable作为基本的表达形式,虽然Runnable的run方法能够写入日志,写入文件,写入数据库等操作, ...