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/ 目录 题目描述 题目大意 解题方法 求出最短相同子数组度的长度 使用堆求最大次数和最小长 ...
随机推荐
- WdatePicker控件Javascript取得当前时间、取得减30分钟时间
1.取得当前时间 function getNowFormatDate() { var date = new Date(); var seperator1 = "-"; var se ...
- kraken-ejs创建一个项目【学习札记】
Keep in Touch. 保持联络. Who’s calling? 是哪一位? You did right. 你做得对. You set me up! 你出卖我! kraken-express-e ...
- Intellij创建简单Springboot项目
Intellij创建简单Springboot项目 第一步:选择创建新项目——file-new-project 第二步:选择项目类型——Spring Initializr-next 第三步:输入项目信息 ...
- cocoapods 更新本地仓库 pod setup/update 无限远程中断
升级 cocoapods 无限远程中断:网络不好 试了很多解决方法: 1.替换源,2.设置下载速度,3.清空本地master仓库,4.删了本地的pod库,5.半夜3-5点更新,6.按照失败提示的 (p ...
- 交叉编译python2.7.7
一.python下载网址: http://www.python.org/ftp/python/ 二.python的交叉编译依赖openssl ,openssl的下载网址: https://www.op ...
- Java_13.1.1 字符串的应用
1获取一个字符串中,另一个字符串出现的次数 思想: 1. indexOf到字符串中到第一次出现的索引 2. 找到的索引+被找字符串长度,截取字符串 3. 计数器++ p ...
- idea工程中web.xml报错Servlet should have a mapping
搭建ssm工程过程中web.xml报错:Servlet should have a mapping ....但是mapping已经配置好了...如下图: 搜索无果,后来发现是工程的web.xml位置配 ...
- 使用SQL语句创建数据库1——创建一个数据库文件和一个日志文件的数据库
目的:创建一个数据库文件和一个日志文件的数据库 在matser数据库下新建查询,输入的命令如下: USE master——指向当前使用的数据库.创建数据库实际上是向master数据库中增加一条数据库信 ...
- power designer 从sqlserver数据库获取字段说明&导出rtf文档模板
具体的操作稍后在修改 附件下载:https://files.cnblogs.com/files/zinan/powerDesigner.rar
- 12.Mysql存储过程和函数
12.存储过程和函数12.1 什么是存储过程和函数存储过程和函数是事先经过编译并存储在数据库中的一段SQL语句的集合,调用存储过程和函数简化应用开发人员的工作,减少数据在数据库和应用服务器之间的传输, ...