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.lengthwill 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 (数组的度)的更多相关文章
- [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] 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 th ...
- Leetcode697.Degree of an Array数组的度
给定一个非空且只包含非负数的整数数组 nums, 数组的度的定义是指数组里任一元素出现频数的最大值. 你的任务是找到与 nums 拥有相同大小的度的最短连续子数组,返回其长度. 示例 1: 输入: [ ...
- 【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/ 目录 题目描述 题目大意 解题方法 求出最短相同子数组度的长度 使用堆求最大次数和最小长 ...
- 697. Degree of an Array 频率最高元素的最小覆盖子数组
[抄题]: Given a non-empty array of non-negative integers nums, the degree of this array is defined as ...
随机推荐
- linux(5)--补充(管道| / 重定向> / xargs)/find 与xargs结合使用/vi,grep,sed,awk(支持正则表达式的工具程序)
本节中正则表达式的工具程序 grep,sed和awk是重点,也是难点!!! 先补充一下一. 管道| / 重定向> / xargs 如:1. 管道和重定向的区别:具体可以见 http://www. ...
- springmvc04-文件上传-JSON数据
文件上传部分: 1, 导入commons-fileupload-1.2.2.jar commons-io-2.4.jar 两个jar包. 2, 在主配置文件中,添加如下信息 <!-- 文件上传- ...
- Activiti-06-.事件
Events 事件 1, 事件用于对发生在流程生命周期的事情进行建模.事件总是被形象成一个圆圈.在BPMN 2.0 中,存在两种主要的事件类型:捕获事件和抛出事件. 捕获:流程执行到该事件时,会等待 ...
- centOS 6 服务管理与服务脚本
服务管理与服务脚本 linux服务 服务管理与服务脚本 linux服务 服务启动过程详解 chkconfig命令 非独立服务与xinetd进程 一个特殊的服务脚本 服务启动过程详解 在开机启动 ...
- SIT 和 UAT
在企业级软件的测试过程中,经常会划分为三个阶段--单元测试,SIT和UAT,如果开发人员足够,通常还会在SIT之前引入代码审查机制(Code Review)来保证软件符合客户需求且流程正确.下面简单介 ...
- 百度富文本编辑器Ueditor使用
首先我们登上ueditor下载,可以看到多种版本. UBuilder:可以自己选择需要的工具. 我用的开发版,Java的jsp版本,在这里是全部工具,但是工具在配置文件中也是可以自己选择的. 下载下来 ...
- java集合系列——Map介绍(七)
一.Map概述 0.前言 首先介绍Map集合,因为Set的实现类都是基于Map来实现的(如,HashSet是通过HashMap实现的,TreeSet是通过TreeMap实现的). 1:介绍 将键映射到 ...
- 《深入理解Java虚拟机》读书笔记-垃圾收集器与内存分配策略
在堆里存放着java世界中几乎所有的对象实例,垃圾收集器在对堆进行回收前需要知道哪些对象还存活,哪些对象已经死去.那怎么样去判断对象是否存活呢? 一.判断对象是否存活算法 1.引用计数法 实现思路:给 ...
- Springboot 学习笔记 ①
前言 之前一直在寻找Springboot的学习资料,终于得偿所愿...那么,先给自己定一个小目标 - 能够使用Springboot这套架构来搭建自己的服务. 准备阶段 1. 开发环境 开发环境其实还是 ...
- .NET DateTime 源码学习
今天下载了微软.Net 源码,看了一下DateTime类,做下记录 DaysInMonth 这个方法是获取某年某月的天数,平时直接用觉得很简单,今天看到源码,发现设计的还是很好的 我想如果是我的话,封 ...