题目:

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的更多相关文章

  1. 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 ...

  2. [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 ...

  3. 【LeetCode】697. Degree of an Array 解题报告

    [LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...

  4. 697. Degree of an Array - LeetCode

    697. Degree of an Array - LeetCode Question 697. Degree of an Array - LeetCode Solution 理解两个概念: 数组的度 ...

  5. 【Leetcode_easy】697. Degree of an Array

    problem 697. Degree of an Array 题意:首先是原数组的度,其次是和原数组具有相同的度的最短子数组.那么最短子数组就相当于子数组的首末数字都是统计度的数字. solutio ...

  6. 【LeetCode】697. Degree of an Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求出最短相同子数组度的长度 使用堆求最大次数和最小长 ...

  7. [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 ...

  8. [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 ...

  9. 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. 201521123068《Java程序设计》第4周学习总结

    1. 本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 点击查看->高清脑图 1.2 使用常规方法总结其他上课内容. 答:学习继承与多态的知识,了解它们之间的关系:super.ext ...

  2. 201521123011《Java程序设计》第11周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 2. 书面作业 本次PTA作业题集多线程 1.互斥访问与同步访问 完成题集4-4(互斥访问)与4-5(同步访问) ...

  3. Java SpringMVC小白的成长(一)

    如果你是一个小白,请跟着我走,我会让你少走弯路,如果你是大牛,那么多谢大牛可以给我提提建议. 说实话,来公司这么久,一直在做的是维护与修改bug.(我的语言是php,来公司才开始接触java). 要毕 ...

  4. lintcode.46 主元素

    给定一个整型数组,找出主元素,它在数组中的出现次数严格大于数组元素个数的二分之一. 注意事项 You may assume that the array is non-empty and the ma ...

  5. java通过JDBC链接SQLServer2012【转载!!!超详细】

    http://blog.csdn.net/stewen_001/article/details/19553173/

  6. Java 删除项目中的.svn信息

    有时候拿过来的war包或者源代码中有.svn信息,我们想删除掉它,然后再上传到自己的svn中. 我这里是自己写的java代码实现的. package com.bstek.transit; import ...

  7. Python装饰器主要用法

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = '人生入戏' user = "admin" passwd = ...

  8. Maven下载、安装和配置(二)

    前言 在上篇博文[项目管理和构建]--Maven简介(一)中我们了解到maven是一种全新的项目构建方式,让我们的开发更加简单,高效.Maven主要做的是两件事: 统一开发规范与工具 统一管理jar包 ...

  9. HTTP协议报文、工作原理

    一.web及网络基础       1.HTTP的历史            1.1.HTTP的概念:                 HTTP(Hyper Text Transfer Protocol ...

  10. 我是如何利用Hadoop做大规模日志压缩的

    背景 刚毕业那几年有幸进入了当时非常热门的某社交网站,在数据平台部从事大数据开发相关的工作.从日志收集.存储.数据仓库建设.数据统计.数据展示都接触了一遍,比较早的赶上了大数据热这波浪潮.虽然今天的人 ...