给定一个非空且只包含非负数的整数数组 nums, 数组的度的定义是指数组里任一元素出现频数的最大值。

你的任务是找到与 nums 拥有相同大小的度的最短连续子数组,返回其长度。

示例 1:

输入: [1, 2, 2, 3, 1] 输出: 2 解释: 输入数组的度是2,因为元素1和2的出现频数最大,均为2. 连续子数组里面拥有相同度的有如下所示: [1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2] 最短连续子数组[2, 2]的长度为2,所以返回2.

示例 2:

输入: [1,2,2,3,1,4,2] 输出: 6

注意:

  • nums.length 在1到50,000区间范围内。
  • nums[i] 是一个在0到49,999范围内的整数。
class Solution {
public:
int findShortestSubArray(vector<int>& nums) {
map<int, int> check;
vector<int> save;
int len = nums.size();
int MAX = 0;
for(int i = 0; i < len; i++)
{
check[nums[i]]++;
MAX =max(MAX, check[nums[i]]);
}
for(map<int, int> :: iterator itr = check.begin(); itr != check.end(); itr++)
{
if(itr ->second == MAX)
save.push_back(itr ->first);
}
int res = len;
for(int i = 0; i < save.size(); i++)
{
int j, k;
for(j = 0; j < len; j++)
{
if(nums[j] == save[i])
break;
}
for(k = len -1; k >= 0; k--)
{
if(nums[k] == save[i])
break;
}
if(k >= j)
{
res = min(res, k - j + 1);
}
}
return res;
}
};

Leetcode697.Degree of an Array数组的度的更多相关文章

  1. [LeetCode] Degree of an Array 数组的度

    Given a non-empty array of non-negative integers nums, the degree of this array is defined as the ma ...

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

  3. [Swift]LeetCode697. 数组的度 | Degree of an Array

    Given a non-empty array of non-negative integers nums, the degreeof this array is defined as the max ...

  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 the ma ...

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

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

  6. C#LeetCode刷题之#697-数组的度( Degree of an Array)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3738 访问. 给定一个非空且只包含非负数的整数数组 nums, ...

  7. leetcode 之 Degree of an Array

    1.题目描述 Given a non-empty array of non-negative integers nums, the degree of this array is defined as ...

  8. 【Leetcode_easy】697. Degree of an Array

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

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

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

随机推荐

  1. 04.Hibernate常用的接口和类---SessionFactory类和作用

    是一个生成Session的工厂类 特点: 1.由Configuration通过加载配置文件创建该对象. SessionFactory factory = config.buildSessionFact ...

  2. Linux自动化工具之crontab (windows需要手动配置相关服务,具体百度)

    //有的shell解释器是/bin/tcsh而不是传统的/bin/bash.而且两者语法有些差异,注意避免.比如设置变量tcsh是set 变量 =`****`   1.crontab是什么,是linu ...

  3. HZOI20190820模拟27题解

    A. 小奇挖矿2 显然的O(m)dp:$f[i]=max(f[i-4],f[i-7])+a[i]$,当然你要保证i,i-4,i-7都能到达 #include<iostream> #incl ...

  4. ArcGIS中线转面

    1. 打开ArcMap用Add Data加载shp Polyline线文件. 2. 选Editor编辑\Start Editing开始编辑. 3. 选Editor编辑\More Editing Too ...

  5. 关于python中 and 和 or 的一些特殊使用

    print(True or 1)  # True print(1 or True) # 1 print(3 or 1) # 3 print(0 or 3) # 3 总结:or左边无论是 数字还是Boo ...

  6. ajax原理及使用

    1.关于同步和异步 异步传输是面向字符的传输,它的单位是字符:而同步传输是面向比特的传输,它的单位是桢,它传输的时候要求接受方和发送方的时钟是保持一致的. 具体来说,异步传输是将比特分成小组来进行传送 ...

  7. 统一建模语言简介UML

    统一建模语言(Unified Modeling Language,UML)是用来设计软件蓝图的可视化建模语言,1997 年被国际对象管理组织(OMG)采纳为面向对象的建模语言的国际标准.它的特点是简单 ...

  8. Hadoop 单机安装配置

  9. 数字统计类题目的非数位DP解法

    ZJOI2010 数字统计 上题题意为求[l,r]区间中每个数字(0~9)出现的次数 一般的做法为将区间当成[0,r]-[0,l-1],然后进行数位DP 但事实上将区间当成[0,r]-[0,l-1]后 ...

  10. Luogu P1314 聪明的质监员(二分+前缀和)

    P1314 聪明的质监员 题意 题目描述 小\(T\)是一名质量监督员,最近负责检验一批矿产的质量.这批矿产共有\(n\)个矿石,从\(1\)到\(n\)逐一编号,每个矿石都有自己的重量\(w_i\) ...