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.

题目标签:Array

  题目给了我们一个 nums array, 让我们首先找到 出现最多次数的数字(可能多于1个),然后在这些数字中,找到一个 长度最小的 数字。返回它的长度。

  比较直接的想法就是,既然我们首先要知道每一个数字出现的次数,那么就利用HashMap,而且我们还要知道 这个数字的最小index 和最大index,那么也可以存入map记录。

  设一个HashMap<Integer, int[]> map, key 就是 num,value 就是int[3]: int[0] 记录 firstIndex; int[1] 记录 lastIndex; int[2] 记录次数。

  这样的话,我们需要遍历两次:

    第一次遍历nums array:记录每一个数字的 firstIndex, lastIndex, 出现次数; 还要记录下最大的出现次数。

    第二次遍历map key set:当key (num) 的次数等于最大次数的时候,记录最小的长度。(lastIndex - firstIndex + 1)。

Java Solution:

Runtime beats 74.35%

完成日期:10/24/2017

关键词:Array

关键点:HashMap<num, [firstIndex, lastIndex, Occurrence]>

 class Solution
{
public int findShortestSubArray(int[] nums)
{
HashMap<Integer, int[]> map = new HashMap<>();
int maxFre = 0;
int minLen = Integer.MAX_VALUE; // first nums iteration: store first index, last index, occurrence and find out the maxFre
for(int i=0; i<nums.length; i++)
{
if(map.containsKey(nums[i])) // num is already in map
{
map.get(nums[i])[1] = i; // update this num's end index
map.get(nums[i])[2]++; // update this num's occurrence
}
else // first time that store into map
{
int[] numInfo = new int[3];
numInfo[0] = i; // store this num's begin index
numInfo[1] = i; // store this num's end index
numInfo[2] = 1; // store this num's occurrence
map.put(nums[i], numInfo);
} maxFre = Math.max(maxFre, map.get(nums[i])[2]); // update maxFre
} // second map keys iteration: find the minLen for numbers that have maxFre
for(int num: map.keySet())
if(maxFre == map.get(num)[2])
minLen = Math.min(minLen, map.get(num)[1] - map.get(num)[0] + 1); return minLen;
}
}

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

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] 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

    题目: Given a non-empty array of non-negative integers nums, the degree of this array is defined as th ...

  4. Leetcode697.Degree of an Array数组的度

    给定一个非空且只包含非负数的整数数组 nums, 数组的度的定义是指数组里任一元素出现频数的最大值. 你的任务是找到与 nums 拥有相同大小的度的最短连续子数组,返回其长度. 示例 1: 输入: [ ...

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

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

  6. 697. Degree of an Array - LeetCode

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

  7. 【Leetcode_easy】697. Degree of an Array

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

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

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

  9. 697. Degree of an Array 频率最高元素的最小覆盖子数组

    [抄题]: Given a non-empty array of non-negative integers nums, the degree of this array is defined as ...

随机推荐

  1. lintcode.22 平面列表

    平面列表    描述 笔记 数据 评测 给定一个列表,该列表中的每个要素要么是个列表,要么是整数.将其变成一个只包含整数的简单列表. 注意事项 如果给定的列表中的要素本身也是一个列表,那么它也可以包含 ...

  2. Intellij idea插入表数据【在UI界面插入出错】

    使用Intellij idea向数据库插入表数据的时候,如果该表是联合主键的,那么不能使用UI界面来进行插入-- 必须通过SQL语句才能插入-- 至于为什么?我也不知道-.搞了大半天--想省时间不写S ...

  3. 关于使用lombok遇到的问题

    在官网上下载了lombok.jar包以后,有两种安装方式 : 1. 双击下载下来的 JAR 包安装 lombok    我选择这种方式安装的时候提示没有发现任何 IDE,所以我没安装成功,我是手动安装 ...

  4. Redis学习笔记之一 : 配置redis

    Redis 简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis 与其他 key - value 缓存产品有以下三个特点: Redis支持数据的持久 ...

  5. Java开发中遇到的问题

    head丢失 html的dtd不对 Integer数据类型 使用==比较 这个肯定错(事后才知道) sql语句处理分组的时候,在本地服务使用没问题,在服务器上出现sql异常 group by语句规范, ...

  6. GCD之Apply

    dispatch_apply函数是dispatch_sync函数和dispatch_group的结合体.该函数将按指定的次数将指定的block追加到指定的dispatch queue中,并等待全部处理 ...

  7. 从C#到TypeScript - 高级类型

    C# vs TypeScript - 高级类型 上一篇讲了基础类型,基本上用基础类型足够开发了,不过如果要更高效的开发,还是要看下高级类型,这篇和C#共同点并不多,只是延用这个主题. 联合类型 可以从 ...

  8. 翻译 | 使用A-Frame打造WebVR版《我的世界》

    原文地址:Minecraft in WebVR with HTML Using A-Frame 原文作者:Kevin Ngo 译者:Felix 校对:阿希 我是 Kevin Ngo,一名就职于 Moz ...

  9. [js高手之路] html5 canvas系列教程 - 掌握画直线图形的常用API

    我们接着上文[js高手之路] html5 canvase系列教程 - 认识canvas以及基本使用方法继续. 一.直线的绘制 cxt.moveTo( x1, y1 ): 将画笔移动到x1, y1这个点 ...

  10. Angularjs-Forms(表单)

    点击查看AngularJS系列目录 转载请注明出处:http://www.cnblogs.com/leosx/ Angular表单 input, select, textarea控件都是给用户输入数据 ...