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 ...
随机推荐
- 攻防世界PHP2
PHP2 进入环境就一个英文其他啥都没有,英文也没啥提示信息 我们使用dirsearch扫描一下,一开始确实没扫到什么东西,到最后看了wp发现原来源码是在index.phps中,这里只提供一个思路,不 ...
- 顺利通过EMC实验(17)
- [译] 沙箱中的间谍 - 可行的 JavaScript 高速缓存区攻击
原文 The Spy in the Sandbox – Practical Cache Attacks in Javascript 相关论文可在 https://github.com/wyvernno ...
- 【译】客户端存储(Client-Side Storage)
本文转载自:众成翻译译者:文蔺链接:http://www.zcfy.cc/article/660原文:http://www.html5rocks.com/en/tutorials/offline/st ...
- 小程序TAB列表切换内容动态变化,scrollview高度根据内容动态获取
滑动tab选项卡 一.在小程序里面tab选项卡是用的是自带的swiper组件,下面直接上代码 <view class="container"> <view cla ...
- Android.mk文件如何打日志信息
1. 在mk文件中添加:$(warning "xxx="$(变量名)) 2. 执行lunch,选一个分支,此过程中可以打出添加的log.
- IDEA中 Debug 调试工具(图文详解)
DEBUG调试工具 一. Debug 调试工具 1. Debug的作用 2. Debug的使用步骤 3. IDEA中Debug按钮详解 总结 参考博文:https://blog.csdn.net/qq ...
- 小程序picker的使用
效果图: 代码: <view class='infoItem'> <view class='infoItem-left'><text style='color:red'& ...
- mongodb安装错误以及原理
安装mongodb,默认是安装到"C:\Program Files\MongoDB\"这里的,我在注册表里没有找到mongodb的信息,所以猜测它只是将其解压到那个位置而已,它只是 ...
- Vue3 + Echarts 5 绘制带有立体感流线中国地图,建议收藏
本文绘制的地图效果图如下: 一.Echarts 使用五部曲 1.下载并引入 echarts Echarts 已更新到了 5.0 版本,安装完记得检查下自己的版本是否是 5.0 . npm instal ...