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 ...
随机推荐
- 201521123004 《Java程序设计》第3周学习总结
1. 本周学习总结 (1)①使用构造函数(constructor) eg:Date now = new Date(); new Date(); //创建了一个Date对象 now是Date类型变量,存 ...
- 201521123026《JAVA程序设计》第14周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多数据库相关内容. 2. 书面作业 1. MySQL数据库基本操作 建立数据库,将自己的姓名.学号作为一条记录插入.(截图,需出现自 ...
- jquery-easyUI第一篇【介绍、入门、使用常用的组件】
什么是easyUI 我们可以看官方对easyUI的介绍: easyUI就是一个在Jquery的基础上封装了一些组件-.我们在编写页面的时候,就可以直接使用这些组件-非常方便-easyUI多用于在后台的 ...
- [LeetCode] 231 Power of Two && 326 Power of Three && 342 Power of Four
这三道题目都是一个意思,就是判断一个数是否为2/3/4的幂,这几道题里面有通用的方法,也有各自的方法,我会分别讨论讨论. 原题地址:231 Power of Two:https://leetcode. ...
- PuTsangTo-单撸游戏开发01 Flag与计划
先立下flag,至少1年之内坚持并2年之内完成自己的一个梦想--游戏开发. 没有参加培训也不打算参加培训,就纯靠业余时间自学并用自己的思路完成一整套游戏体系.做出此决心时也已经做好准备烂尾了,但是有种 ...
- 支持向量机SVM(Support Vector Machine)
支持向量机(Support Vector Machine)是一种监督式的机器学习方法(supervised machine learning),一般用于二类问题(binary classificati ...
- 两句话动态修改table数据并提交到后台
//为所有的input 添加click事件,我将对象的id放入到name属性中,行数放入到alt属性中 $("input").click(function(obj){ //获得当前 ...
- 第5章 不要让线程成为脱缰的野马(Keeping your Threads on Leash) ---简介
这一章描述如何初始化一个新线程,如何停止一个执行中的线程,以及如何了解并调整线程优先权. 读过这一章之后,你将有能力回答一个 Win32 多线程程序设计的最基本问题.你一定曾经在 Usenet ...
- Palindrome poj3974
Palindrome Time Limit: 15000MS Memory Limit: 65536K Total Submissions: 3280 Accepted: 1188 Descr ...
- S2_SQL_第五章
UNIQUE|FULLTEXT|SPATIAL:分别表示唯一索引,全文索引和空间索引,为可选参数index_name;指定索引名称table_name;指定创建索引表名colymn_name;指定需要 ...