给定一个非空且只包含非负数的整数数组 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. Python-异常处理 使用selenium库自动爬取数据

    异常处理 处理程序的报错 语法 捕捉万能异常: try: print(a) except Exception as e: print("你的代码有问题") print(" ...

  2. Vscode中问题

    1.VScode中如果安装vim插件,那么编辑代码时会默认使用vim 2.出现任何问题都在设置的首选项里面修改,比如终端无法复制,或者终端右击的默认操作等

  3. E: 无法获得锁 /var/lib/apt/lists/lock - open (11: 资源暂时不可用) E: 无法对目录 /var/lib/apt/lists/ 加锁 问题解决方法

    使用Ubuntu16.04安装软件执行apt-get update时出现如下错误: E: 无法获得锁 /var/lib/apt/lists/lock - open (11: 资源暂时不可用) E: 无 ...

  4. webpack打包less与sass

    less 1.安装 less-loader 与 less npm install less-loader less --save-dev 2.配置webpack.config.js module.ex ...

  5. 常用的git操作命令

    整理来源于廖雪峰的git教程https://www.liaoxuefeng.com git: 分布式版本控制系统  本地有完整的代码库,还有远程代码库 svn: 集中式版本控制系统 必须联网时才可提交 ...

  6. PAT甲级——A1012 The Best Rank

    To evaluate the performance of our first year CS majored students, we consider their grades of three ...

  7. redis键(key)

    redis键: 用于管理redis的键 command key_name > set runoodkey redis OK > del runoodkey 上面的例子中,del是一个命令, ...

  8. k8s(openshift) 部署istio1.1

    准备工作: openshift 默认不允许UID为0的容器运行,要先授权scc以便安装istio # oc adm policy add-scc-to-user anyuid -z istio-ing ...

  9. 万能的pdftk

    pdftk (the pdf toolkit) 是一个功能强大的命令行的 PDF 文件编辑软件,可以合并/分割 PDF 文档.对 PDF 文件加密解密.给 PDF 文档加水印.从 PDF 文档中解出附 ...

  10. 在VUE中实现打印

    1.安装 npm install vue-print-nb --save 2.在mian.js中引入 import Print from 'vue-print-nb' Vue.use(Print); ...