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 ...
随机推荐
- 团队作业4——第一次项目冲刺(Alpha版本) 3
一.Daily Scrum Meeting照片 二.燃尽图 注:由于前两天燃尽图制作不准确,所以重新制作一个,现已由工作量改为功能数,工作日从今天开始. 三.项目进展 功能: 算法这一块已全部完成 文 ...
- 团队作业4----第一次项目冲刺(Alpha版本)4.25
a.提供当天站立式会议照片 会议内容: ①:对有的接口编写遇到的困难,由于基础问题,建议百度,谷歌现成的接口 ②:课程较多,时间不够,任务的调整以及进度的调整 b. 每个人的工作 每个人在尽量完成自己 ...
- 团队作业8——第二次项目冲刺(Bata版本)--第二天
一.Daily Scrum Meeting照片 二.燃尽图 三.项目进展 学号 成员 贡献比 201421123001 廖婷婷 15% 201421123002 翁珊 17% 201421123004 ...
- JAVA基础第七组(5道题)
31.题目:将一个数组逆序输出. 1.程序分析:用第一个与最后一个交换. package com.niit.homework1; /** * @author: Annie * ...
- 201521123037 《Java程序设计》第5周学习总结
1. 本周学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 1.2 可选:使用常规方法总结其他上课内容. 接口: 接口简而言之是方法声明和常量值的集合,接口中所有的方法默认为public ...
- 201521123052《Java程序设计》第10周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常与多线程相关内容. 2. 书面作业 本次PTA作业题集异常.多线程 1.finally 题目4-2 1.1 截图你的提交结果(出 ...
- linux fork两次避免僵尸进程的程序(简单)
#include<stdio.h> #include<unistd.h> int main() { pid_t pid,pid1; int status; ) { printf ...
- 《MySQL必知必会》[01] 基本查询
<MySQL必知必会>(点击查看详情) 1.写在前面的话 这本书是一本MySQL的经典入门书籍,小小的一本,也受到众多网友推荐.之前自己学习的时候是啃的清华大学出版社的计算机系列教材< ...
- .NET Core 使用RabbitMQ
RabbitMQ简介 AMQP,即Advanced Message Queuing Protocol,高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间件设计.消息中间件主要用于组件之间的 ...
- jenkins~集群分发功能和职责处理
jenkins的多节点集群 在进行自动化部署时,你可以按着它们的项目类型去进行分别部署,这样即可以达到负载均衡,又可以达到一种职责的明确,比如像java的项目你可以使用linux服务来进行部署(拉代码 ...