Leetcode697.Degree of an Array数组的度
给定一个非空且只包含非负数的整数数组 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数组的度的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- 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 ...
- 697. Degree of an Array 频率最高元素的最小覆盖子数组
[抄题]: Given a non-empty array of non-negative integers nums, the degree of this array is defined as ...
- C#LeetCode刷题之#697-数组的度( Degree of an Array)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3738 访问. 给定一个非空且只包含非负数的整数数组 nums, ...
- leetcode 之 Degree of an Array
1.题目描述 Given a non-empty array of non-negative integers nums, the degree of this array is defined as ...
- 【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- ...
随机推荐
- thymeleaf报错元素类型必须由匹配的结束标记终止
spring boot 1.x 版本中thymeleaf报错元素类型 “link” 必须由匹配的结束标记 “</link>” 终止解决办法: 1.在pom.xml文件中添加: <de ...
- 初识莫队——小Z的袜子
以前一直觉得莫队是多么高大上的一种算法,然而仔细看了下发现其实并不复杂,实质上就是技巧性的暴力美学. 在我看来莫队是一种分块排序后降低复杂度的算法,当答案可以通过左右端点一个一个移动维护出来的时候就可 ...
- MySQL8.0.17 - 初探 Clone Plugin
MySQL8.0.17推出了一个重量级的功能:clone plugin.允许用户可以将当前实例进行本地或者远程的clone.这在某些场景尤其想快速搭建复制备份或者在group replication里 ...
- day20 作业
目录 今日作业 1.下面这段代码的输出结果将是什么?请解释. 2.多重继承的执行顺序,请解答以下输出结果是什么?并解释. 3.什么是新式类,什么是经典类,二者有什么区别?什么是深度优先,什么是广度优先 ...
- transform函数
C++学习[原创]transform函数的应用 transform(first,last,result,op);//first是容器的首迭代器,last为容器的末迭代器,result为存放结果的容器, ...
- JavaSE_01_Exception类
1.1 异常概念 指的是程序在执行过程中,出现的非正常的情况,最终会导致JVM的非正常停止. 在Java等面向对象的编程语言中,异常本身是一个类,产生异常就是创建异常对象并抛出了一个异常对象.Java ...
- 强制以32位ie运行程序
最近被一个问题给郁闷住了.给电脑重装系统后,发现发布好的程序.或者VS2012总是以64位ie运行程序,这样的话skyline的三维控件无法显示.到现在我是确定ie64无法识别skyline的控件. ...
- PHP实现微信退款的分析与源码实现
原文:https://blog.csdn.net/jason19905/article/details/78628349 网上的很多PHP微信支付接入教程都颇为复杂,且需要配置和引入较多的文件,本人通 ...
- k8s 内部各个部件运转
Master节点部署的都是kubernetes的核心模块APIServer提供资源操作的唯一入口,并且提供认证/授权/kubernets的访问控制可以通过kubectl和自己开发的客户端,通过http ...
- <form>(表单)标签和常用的类型
1.定义和用法 <form> 标签用于为用户输入创建 HTML 表单. 表单能够包含 input 元素,比如文本字段.复选框.单选框.提交按钮等等. 表单还可以包含 menus.texta ...