697. Degree of an Array
static int wing=[]()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return ;
}(); class Solution
{
public:
int findShortestSubArray(vector<int>& nums)
{
int sz=nums.size();
vector<int> firstindex(,-);
vector<int> lastindex(,-);
vector<int> times(,);
int maxtimes=; int minlength=INT_MAX;
for(int i:nums)
{
times[i]++;
maxtimes=max(maxtimes,times[i]);
} for(int i=;i<sz;i++)
{
if(firstindex[nums[i]]==-)
firstindex[nums[i]]=i;
} for(int j=sz-;j>-;j--)
{
if(lastindex[nums[j]]==-)
lastindex[nums[j]]=j;
}
for(int k=;k<sz;k++)
{
if(times[nums[k]]==maxtimes)
minlength=min(minlength,lastindex[nums[k]]-firstindex[nums[k]]+);
}
return minlength;
}
};
这题用map和vector都行,一个容器统计元素次数,一个容器放元素首次出现位置,一个容器放元素最后一次出现位置,然后扫描原数组,遇到最大出现次数更新最短长度即可。
697. Degree of an Array的更多相关文章
- 【Leetcode_easy】697. Degree of an Array
problem 697. Degree of an Array 题意:首先是原数组的度,其次是和原数组具有相同的度的最短子数组.那么最短子数组就相当于子数组的首末数字都是统计度的数字. solutio ...
- 【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 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 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&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 ...
- [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/ 目录 题目描述 题目大意 解题方法 求出最短相同子数组度的长度 使用堆求最大次数和最小长 ...
随机推荐
- Gym - 100989G 二分
链接:ECJTU 2018 Summer Training 1 - Virtual Judge https://vjudge.net/contest/236677#problem/G 谷歌翻译: 距 ...
- 不包含数据和字母的Webshell
在做php20中的challenge5时学习到了php中可代替数字和字母的符号,悲剧的是这道题没做出来,目前先整理知识点吧555555555555 情况是:很多时候在敏感地方WAF会阻 ...
- JavaScript各种继承方式(一):原型链继承(prototype chaining)
一 原理 子类的构造函数的原型对象,是父类的构造函数创建的实例. function Fruit(){ this.name = '水果'; this.nutrition=['维生素','膳食纤维']; ...
- python脚本linux上后台执行
1.脚本后加& 加了&以后可以使脚本在后台运行,这样的话你就可以继续工作了.但是有一个问题就是你关闭终端连接后,脚本会停止运行 python3 run.py >/dev/null ...
- HTTP Basic Authentication认证(Web API)
当下最流行的Web Api 接口认证方式 HTTP Basic Authentication: http://smalltalllong.iteye.com/blog/912046 什么是HTTP B ...
- Java并发-ConcurrentModificationException原因源码分析与解决办法
一.异常原因与异常源码分析 对集合(List.Set.Map)迭代时对其进行修改就会出现java.util.ConcurrentModificationException异常.这里以ArrayList ...
- C# 切换到二级域名,使用Cookie
之前的网站一直用的是一级域名,现因为其他原因,需要使用一个二级域名访问,原先的域名不用了. 使用二级域名后,发现Cookie一直取不到,分析代码发现,原来是需要在设置cookie的时候,把二级域名加上 ...
- angular2.0学习笔记5.关于组件
1.组件文件应在/src/app文件夹下 2.组件文件命名应遵循小写中线形式,每个单词之间用中线分隔,并且以.component.ts结尾. 如:HeroDetailComponent类应该放在her ...
- geoserver 发布无坐标png
1.geoserver 安装一些插件的时候,直接去http://docs.geoserver.org/ 官网,选择自己geoserver对应的版本的插件就可以了. 2.geoserver 发布自身带有 ...
- iOS最全的常用正则表达式大全
很多不太懂正则的朋友,在遇到需要用正则校验数据时,往往是在网上去找很久,结果找来的还是不很符合要求.所以我最近把开发中常用的一些正则表达式整理了一下,包括校验数字.字符.一些特殊的需求等等.给自己留个 ...