697. Degree of an Array - LeetCode

Question

697. Degree of an Array - LeetCode

Solution

理解两个概念:

数组的度:[1,2,2,3,1]这个数组,去重后的元素为[1,2,3],每个元素在原数组中重复的次数分别是221,数组的度就是元素最大重复次数,这个数组中元素最大重复次数就是2

连续子数组:和字符串中的了串概念类似,数组元素顺序保持不变,取其中一部分,该题就是求在度相同的情况下最小连续子数组的长度

思路:数组的度是以元素为对象,每个元素都有一个度(degree),第一次出现的坐标(start),最后一次出现的坐标(end),求出degree最大,end-start最小的那个情况,end-start+1就是该数组最小连续子数组的长度了。

public int findShortestSubArray(int[] nums) {
int start = 0;
int end = 0;
int degree = 0;
Map<Integer, Element> numElementMap = new HashMap<>(); for (int i = 0; i < nums.length; i++) {
int num = nums[i];
Element element = numElementMap.get(num);
if (element == null) {
element = new Element(i, 0);
numElementMap.put(num, element);
}
element.degree++;
int elementEnd = i; // degree最大 end-start最小
boolean change = i == 0 ? true : false; // 第一个元素要初始化
if (element.degree >= degree) {
change = true;
if (element.degree == degree && (end - start < elementEnd - element.start)) {
change = false;
}
}
if (change) {
start = element.start;
end = elementEnd;
degree = element.degree;
} }
return end - start + 1;
} class Element {
int start;
int degree; public Element(int start, int degree) {
this.start = start;
this.degree = degree;
}
}

Reference

697. Degree of an Array - LeetCode的更多相关文章

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

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

  2. 【Leetcode_easy】697. Degree of an Array

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

  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 the ma ...

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

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

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

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

  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. 697. Degree of an Array 频率最高元素的最小覆盖子数组

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

  9. 697. Degree of an Array@python

    Given a non-empty array of non-negative integers nums, the degree of this array is defined as the ma ...

随机推荐

  1. Python这些位运算的妙用,绝对让你大开眼界

    位运算的性能大家想必是清楚的,效率绝对高.相信爱好源码的同学,在学习阅读源码的过程中会发现不少源码使用了位运算.但是为啥在实际编程过程中应用少呢?想必最大的原因,是较为难懂.不过,在面试的过程中,在手 ...

  2. NE555脉冲模块电路

  3. 微信小程序&mpvue问题总结(1)

    微信小程序进入到首页的时候,日志打印出"created", "onlaunch", "mounted",具体代码如下:那么,在小程序中 cr ...

  4. js读取cookie 根据cookie名称获取值的方法

    //方法1 //存在问题:如果cookie中存在 aaaname=aa;name=bb 获取name的值就会出现错误function getCookie(c_name){ if (document.c ...

  5. java中接口interface可以持有多个类的共享常量

    3.接口持有多个类的共享常量  接口另一主要功能,马克-to-win: 可以使用接口来引入多个类的共享常量.所有的这些变量名都将作为常量看待.所有定义在接口中的常量都默认为public.static和 ...

  6. add jars、add external jars、add library、add class folder的区别

    add external jars = 增加工程外部的包add jars = 增加工程内包add library = 增加一个库add class folder = 增加一个类文件夹add jar是表 ...

  7. 《头号玩家》AI电影调研报告(五)

    4.VR自由行走跑步机 电影中,它可以让玩家朝任意方向无限奔跑并保持在平台最中央,还可以模拟上台阶和走斜坡的情况.而下面这件VR跑步装备,可以让你在游戏世界穿行自如. 奥地利创意公司Cyberith公 ...

  8. redis笔记补充

    redis补充 这篇文章是redis入门笔记的补充. 1.info命令 用来显示服务的信息. info命令可以跟下面的选项: server: 关于 Redis 服务器的一些信息 clients: 客户 ...

  9. linux3种安装软件、yum仓库、防火墙、乱码

    Linux中安装软件的三种方式 1.哪三种方式? rpm安装 yum安装 源代码编译安装 2.区别 rpm安装类似于windows中的安装包,下载下来之后直接安装.缺点是不能自己解决依赖. yum安装 ...

  10. mybatisPlus crud操作注意事项

    1.调用IService里的update方法,如果是自定义根据除主键外其它字段更新的时候,如果给主键id设置其它值不会更新主键id,如果未设置主键id值或者设置为null,同样不会更新主键id. 2. ...