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- ...
随机推荐
- 跟我一起学习webpack使用配置文件(二)
接着跟我一起学习webpack(一)中的项目来,我们接下来使用配置文件 使用npx webpack -h 我们可以查看webpack的配置参数 从我们在package.json中添加的命令来看,当项目 ...
- ajaxStart 和 ajaxSend 不执行
我们一般会在loading 效果的时候会用上这两个全局事件 ajaxStart 和 ajaxSend 但是要注意的是 在同时有多个ajax 执行的时候ajaxStart 只会执行一次 所以一般情况下 ...
- php 随意参数方法的使用
1, 用到的PHP函数: func_get_arg() / func_get_args()/ func_num_args 2, func_get_arg(index) :根据索引取得参数具体值 ...
- 19-10-23-K-Aft
没改完题就过来沽博客是不是有点不好…… ZJ一下: 好好好题. T1数组大小…… $$10^7 \rightarrow 60$$ 事实上…… $$7 \times 10^7 \rightarrow 0 ...
- Miller Rabin算法学习笔记
定义: Miller Rabin算法是一个随机化素数测试算法,作用是判断一个数是否是素数,且只要你脸不黑以及常数不要巨大一般来讲都比\(O(\sqrt n)\)的朴素做法更快. 定理: Miller ...
- 二叉查找树、平衡二叉树(AVL)、B+树、联合索引
1. [定义] 二叉排序树(二拆查找树)中,左子树都比节点小,右子树都比节点大,递归定义. [性能] 二叉排序树的性能取决于二叉树的层数 最好的情况是 O(logn),存在于完全二叉排序树情况下,其访 ...
- 通过三个DEMO学会SignalR的三种实现方式 转载https://www.cnblogs.com/zuowj/p/5674615.html
一.理解SignalR ASP .NET SignalR 是一个ASP .NET 下的类库,可以在ASP .NET 的Web项目中实现实时通信(即:客户端(Web页面)和服务器端可以互相实时的通知消息 ...
- RSA加密算法在WEB中的应用
加密算法有很多,如不可逆的摘要算法MD5.SHA(安全哈希算法),可逆的Base64编码,对称加密算法DES.AES,还有非对称加密算法DH.RSA等.那是不是说明我们可以使用任何一种加密算法就能保证 ...
- 通过原生JS打印一个空心菱形图案
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- [转载] OpenCV2.4.3 CheatSheet学习(一)
OpenCV向MATLAB靠拢,图像的操作方法变得不那么C了,更m了一些.比如,MATLAB中的常用函数imshow.imread.imwrite函数在OpenCV中已经有了同名的兄弟. 此外,Ope ...