697. Degree of an Array - LeetCode
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的更多相关文章
- 【LeetCode】697. Degree of an Array 解题报告
[LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...
- 【Leetcode_easy】697. Degree of an Array
problem 697. Degree of an Array 题意:首先是原数组的度,其次是和原数组具有相同的度的最短子数组.那么最短子数组就相当于子数组的首末数字都是统计度的数字. solutio ...
- 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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求出最短相同子数组度的长度 使用堆求最大次数和最小长 ...
- 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 ...
- [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 ...
- 697. Degree of an Array 频率最高元素的最小覆盖子数组
[抄题]: Given a non-empty array of non-negative integers nums, the degree of this array is defined as ...
- 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 ...
随机推荐
- putty 远程登录ubuntu的方法
首先,要确定linux的ssh服务已经开启了: 首先要确定开启了ssh-server: 安装:sudo apt-get install openssh-server 安装:su ...
- 认识 Function.prototype.bind()
欢迎前端爱好者加入QQ群:112916679 答疑解惑,且可获取更多前端资料! bind()方法创建一个新的函数, 当被调用时,将其this关键字设置为提供的值,在调用新函数时,在任何提供之前提供一个 ...
- 聊一聊Web端的即时通讯
聊一聊Web端的即时通讯 Web端实现即时通讯的方法有哪些? - 短轮询 长轮询 iframe流 Flash Socket 轮询 客户端定时向服务器发送Ajax请求,服务器接到请求后马上返回响应信息并 ...
- 前端文件上传-javascript-ajax
书写是为了更好的记忆. 方案一:form表单上传 该方案优点是支持好,缺点刷新页面. <form action="url" method="post" e ...
- 【Android开发】URL 转义与反转义
1,转义 @org.junit.Test public void testEncode(){ String url="http://192.168.0.19:8888/cas/login&q ...
- 抽象方法不能为private,final或者static,为什么?
4)抽象方法不能为private,final或者static, native, synchrozied为什么?[新手可忽略不影响继续学习]马克-to-win:抽象方法的最实质的意义在于被未来的子类覆盖 ...
- Spring Boot-使用Spring Initializer快速创建Spring Boot项目
File->project->Spring Initializer 点击next 点击下一步即可,如果是第一次可能需要下载jar包,如下图 resources文件中的目录结构如上图所示 s ...
- openfeign使用踩坑记录
1.报ClassNotFound com.netflix.config.CachedDynamicIntProperty问题,原因是spring-cloud-starter-openfeign的spr ...
- 【图文教学】如何轻松下载tiktok上的视频
我是真诚的想和大家分享这个办法的!这个叫光影存图,图标就是这个绿色的箭头,我是苹果手机,安卓手机也可以用,就是它分免费版和会员版的,没有强制要你充会员,看一下广告就可以获取下载次数,我就是这么干的,只 ...
- ASP.NET Core WebApi返回结果统一包装实践
前言 近期在重新搭建一套基于ASP.NET Core WebAPI的框架,这其中确实带来了不少的收获,毕竟当你想搭建一套框架的时候,你总会不自觉的去想,如何让这套框架变得更完善一点更好用一点.其中在关 ...