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/ 目录 题目描述 题目大意 解题方法 求出最短相同子数组度的长度 使用堆求最大次数和最小长 ...
随机推荐
- linux下主从同步和redis的用法
前言 mariadb其实就是mysql mysql已经被oracle收购,它即将闭源,马上要开始收费了因此还想免费试用开源的数据库mysql,就在centos7上,将mysql分支为mariadb 操 ...
- Docker基础入门
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.容器是完全使用沙箱机制,相互之间不会有任何 ...
- ATX 安卓设备 WiFi 统一管理以及设备自动化测试
众所周知,安卓单台设备的UI自动化测试已经比较完善了,有数不清的自动化框架或者工具.但是介绍多设备管理的内容并不多,当手里的手机多了之后,要做自动化测试平台,这块的东西又不得不碰,摆脱USB限制,接入 ...
- 浅谈CSRF
CSRF是什么? (Cross Site Request Forgery, 跨站域请求伪造)是一种网络的攻击方式,它在 2007 年曾被列为互联网 20 大安全隐患之一,也被称为“One Click ...
- stark组件之过滤操作【模仿Django的admin】
一.先看下django的admin是如何实现过滤操作 首先在配置类中顶一个list_filter的列表,把要过滤的字段作为元素写i进去就可以了 class testbook(admin.ModelAd ...
- echarts中国地图坐标弹框
echarts链接:http://gallery.echartsjs.com/editor.html?c=xHkdOlpwWz 代码: var geoCoordMap = { '上海': [121.4 ...
- linux服务器搭建
centos7 java web项目环境搭配 2018年07月19日 17:20:21 阅读数:25 首先进行系统安装,此处不进行详细介绍,自行百度安装 一.配置ip地址信息 1.进入/etc/sys ...
- PHP统计网站pv(访问量)
//首先判断有没有统计的文件 if(is_file("pv.txt")){//有 //取文件里面的值 $count=file_get_contents("pv.txt&q ...
- WEB框架之Django实现分页功能
一 Paginator分页器 1 首先在数据库中生成大量数据 def index(request) book_list = [] for i in rang(1000) book = Book(tit ...
- win下Apache2.4的下载与安装
1.到apache官网上下载apache的安装文件 http://httpd.apache.org/download.cgi 点击链接Files for Microsoft Windows,因为a ...