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 ...
随机推荐
- PC端操作系统、移动端操作系统、嵌入式操作系统
左侧部分已是历史的操作系统,右侧的还是活跃的操作系统.安卓系统Android 是Google开发的基于Linux平台的开源手机操作系统.它包括操作系统.用户界面和应用程序-- 移动电话工作所需的全部软 ...
- 删除html标签或标签属性以及样式
JavaScript module for stripping HTML tags and/or HTML element attributes from strings. 安装 npm instal ...
- Redis 中的过期删除策略和内存淘汰机制
Redis 中 key 的过期删除策略 前言 Redis 中 key 的过期删除策略 1.定时删除 2.惰性删除 3.定期删除 Redis 中过期删除策略 从库是否会脏读主库创建的过期键 内存淘汰机制 ...
- 布局框架frameset
<!DOCTYPE html>//demo.html <html> <head> <meta charset="UTF-8"> &l ...
- SQL注入之延迟盲注
延迟盲注 你不知道你输入的数据在sql被执行后是否是正确或错误的.你只知道有数据. 利用原理 借助if语句,如果正确就sleep(5),延迟5秒返回数据.通过数据返回的时间大小判断自己的语句是否正确执 ...
- Golang | 并发
goroutine 协程(Coroutine) Golang 在语言层面对并发编程进行了支持,使用了一种协程(goroutine)机制, 协程本质上是一种用户态线程,不需要操作系统来进行抢占式调度,但 ...
- Pinpoint介绍及docker安装方式
一.介绍 Pinpoint是用Java编写的大型分布式系统的APM(Application Performance Management应用程序性能管理)工具,受Dapper论文的启发,Pinpoin ...
- 解决pycharm的爬虫乱码问题(初步了解各种编码格式)
Ascii码(American Standard Code for Information Interchange,美国信息互换标准代码):最初计算机只在美国使用时,只用8位的字节来组合出256(2的 ...
- Golang 泛型的简单使用
go 学习泛型,利用泛型编写对数据集合执行操作的方法.
- MassTransit 入门(一)
本文地址源码 MassTransit是一个面向.net的免费开源分布式应用程序框架. MassTransit使得创建应用程序和服务变得很容易,这些应用程序和服务利用基于消息的.松散耦合的异步通信来获得 ...