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的更多相关文章

  1. 【Leetcode_easy】697. Degree of an Array

    problem 697. Degree of an Array 题意:首先是原数组的度,其次是和原数组具有相同的度的最短子数组.那么最短子数组就相当于子数组的首末数字都是统计度的数字. solutio ...

  2. 【LeetCode】697. Degree of an Array 解题报告

    [LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...

  3. 697. Degree of an Array - LeetCode

    697. Degree of an Array - LeetCode Question 697. Degree of an Array - LeetCode Solution 理解两个概念: 数组的度 ...

  4. 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 ...

  5. 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 ...

  6. [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 ...

  7. 697. Degree of an Array 频率最高元素的最小覆盖子数组

    [抄题]: Given a non-empty array of non-negative integers nums, the degree of this array is defined as ...

  8. 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 ...

  9. [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 ...

  10. 【LeetCode】697. Degree of an Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求出最短相同子数组度的长度 使用堆求最大次数和最小长 ...

随机推荐

  1. JS 解决json字符串转换成json树形输出

    问题: 后台获取一个字符串,格式为  string +jsonList+string+..... 就是传过来一堆数据,然后其中包含了一个json格式的list, 我们希望能将它输出成树形结构显示,能够 ...

  2. 大数据入门推荐 - 数据之巅 大数据革命,历史、现实与未来等五本PDF

    扫码时备注或说明中留下邮箱付款后如未回复请至https://shop135452397.taobao.com/联系店主

  3. Jmeter cookie不兼容问题

    历史脚本,今天准备执行测试,报出这种错误 解决方案:HTTP Cookie Manager里的 Cookie Policy 由rfc2109设置为兼容模式(Compatibility) 参考:http ...

  4. linux下查看项目端口号,杀掉对应端口号的方法

    查看端口号:netstat -anp 结束端口号:sudo iptables -A INPUT -p tcp --dport 8012 -j DROP"

  5. php ActiveMQ的发送消息,与处理消息

    我们以一个简单的用户注册为例,当用户点击注册按钮后,我们发送一个消息,后台php接收到该消息然后处理. 1.php代码如下: <?php $stomp = new Stomp('tcp://19 ...

  6. JFinal Web开发学习(七)使用layUI美化的登录功能

    效果: 验证码还是没有布局好.背景比较怀古. 1.写前端html login.jsp <!DOCTYPE html> <html> <head> <meta ...

  7. Android TV开发 焦点控制

    转载:http://www.eoeandroid.com/thread-264177-1-1.html 最近在做一款基于Android的互联网电视客户端,开发与phone/pad差不多,但是有一个值得 ...

  8. webstorm 打包angular Module build failed: Error: No PostCSS Config found

    angular创建项目后,在webstorm中启动时,报出如题错误,奇怪的是我从命令行启动(ng server)是没有问题的,多方寻求无果,在网上看到过说要加一个配置文件,我不信.我觉得是我配置哪里有 ...

  9. txt写入时报错出现:正由另一进程使用,原来是多此一举的操作

    //if (!File.Exists(newfilepath + "\\" + name + num + ".txt")) //{ // File.Create ...

  10. CSS学习总结2:CSS框模型

    1.CSS框模型概述 CSS框模型规定了元素框处理元素内容.内边框.边框和外边框的方式. 元素框的最内部分是实际的内容,直接包围内容的是内边距.内边距呈现了元素的背景.内边距的边缘是边框.边框以外是外 ...