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/ 目录 题目描述 题目大意 解题方法 求出最短相同子数组度的长度 使用堆求最大次数和最小长 ...
随机推荐
- CentOS ./configure && make && make install详解
码的安装一般由3个步骤组成:配置(configure).编译(make).安装(make install). 在Linux中利用源码包安装软件最重要的就是要仔细阅读安装包当中的README INST ...
- NumPy 广播(Broadcast)
NumPy 广播(Broadcast) 广播(Broadcast)是 numpy 对不同形状(shape)的数组进行数值计算的方式, 对数组的算术运算通常在相应的元素上进行. 如果两个数组 a 和 b ...
- TOJ 4383 n % ( pow( p , 2) ) ===0
传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=4383 描述 There is a ...
- Django的视图函数和路由系统中一些没有用过的小点
1.request对象 print("返回用户访问的url,但是不包括域名",request.path_info) print("返回请求的方法,全大写",re ...
- Web API中常用Filter的执行顺序举例讲解
在WEB Api中,引入了面向切面编程(AOP)的思想,在某些特定的位置可以插入特定的Filter进行过程拦截处理.引入了这一机制可以更好地践行DRY(Don’t Repeat Yourself)思想 ...
- grep与正则表达式的使用
正则表达式以及grep的使用 grep是一种文本过滤工具(模式:pattern)基本使用用法如下: grep [option] PATTERN FILE grep [OPTIONS] [-e PATT ...
- 【WH】MVC数据分页扩展类
public static class QueryableExtensions { #region 内存分页 /// <summary> /// 返回对象分页列表 /// </sum ...
- 梦殇 chapter five
一弦情殇未谱,半纸离愁难书,不记年,叹花开几度...... 蜡烛用它自己的死亡来温暖别人,奉献自己的同时,它内心是快乐的吗?那残留的蜡油是它的泪水,还是它快乐的预兆? 这个世界上除了父母家人外,会有真 ...
- VS新建API控制器时提示“运行所选代码生成器时出错”
使用Nuget安装microsoft.entityframeworkcore.tools这个包就行了,安装时注意版本. 根据下图提示应该是新建控制器时用到了这个包,所以安装一下就好了.之前遇到过一次, ...
- 使用/\_ 打印正三角形 C/C++
输入size,level,使用/\_,打印正三角形 #include<stdio.h> int main() { int level, size; printf("Enter l ...