leetcode 之 Degree of an Array
1、题目描述
Given a non-empty array of non-negative integers nums, the degree of this array is defined as the maximum frequency of any one of its elements.
Your task is to find the smallest possible length of a (contiguous) subarray of nums, that has the same degree as nums.
题目是说给定一个非空数组,找出其中出现最多的元素(不只一个),然后返回数组中包含出现最多的元素的最小子数组的长度。
2、题目分析
首先使用hash表统计每个元素的出现次数,然后找出每个出现最多的元素放入一个vector中,对vector中每个元素进行统计,找出包含vector中每个元素的最小子数组。
3、代码
int findShortestSubArray(vector<int>& nums) { unordered_map<int ,int> m; // 将数组中所有元素放入一个hash_table 中
vector<int> maxItem();
int maxindex = ;
for( auto n : nums )
m[n]++; for(auto itr = m.begin(); itr != m.end() ; itr++ ) // 找出出现次数最多的元素,放在一个vector中
if(itr->second > maxindex )
{
maxItem.clear();
maxItem.push_back(itr->first);
maxindex = itr->second;
}
else if( itr->second == maxindex )
{
maxItem.push_back(itr->first);
} int i=,j= nums.size()-; // 对每个出现次数最多的元素进行检查,找出“度”最小的。
int ans=nums.size(); for(auto itr = maxItem.begin(); itr != maxItem.end(); itr++)
{
i = ;j = nums.size()-;
while( nums[i] != *itr ) i++;
while(nums[j] != *itr ) j--;
ans = min(ans,j-i+);
} return ans; }
leetcode 之 Degree of an Array的更多相关文章
- 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 ...
- [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 ...
- 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 ...
- [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 Degree of an Array
原题链接在这里:https://leetcode.com/problems/degree-of-an-array/description/ 题目: Given a non-empty array of ...
- 【LeetCode】697. Degree of an Array 解题报告
[LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...
- 【LeetCode】697. Degree of an Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求出最短相同子数组度的长度 使用堆求最大次数和最小长 ...
- 697. Degree of an Array - LeetCode
697. Degree of an Array - LeetCode Question 697. Degree of an Array - LeetCode Solution 理解两个概念: 数组的度 ...
- [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 ...
随机推荐
- linux中查找某端口,并关闭对应的端口
1,netstat -ntlp (n表示不反向域名杰斯 t表示查看tcp协议的连接 l查看正在监听端口 p获取进程号和端口) 2,然后直接kill -9 端口号 参考全文:https://linux ...
- Android Studio打开非本机项目比较慢的问题。
使用Android Studio打开其他项目的时候,如果使用的AS版本.gradle不同的话,会在打开项目的时候下载gradle版本,网速不好的情况下回非常的慢. 解决方案: 1.将本机创建的AS项目 ...
- 如何虚拟机里安装Win10操作系统
不多说,直接上干货! Windows Server 2003.2008.2012系统的安装 推荐网址:打开MSDN网站(http://msdn.itellyou.cn ) 关于给电脑换系统,很多人会花 ...
- Redis笔记(一):Redis安装教程
Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. Redis是目前应用最广泛的内存数据存储技术,相比之前的Me ...
- Pascal的3种注释
1.单行注释 {大括号里的内容都被注释掉} 2.单行注释2 //这种注释几乎是绝大部分语言中通用的了 3.多行注释 (*pascal的多行注释有点奇怪,使用的是:括号+星号的方式*) 其他 ...
- Classpath entry org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER will not be exported or published
sometimes when importing a maven project in eclipse, i get the following error: Classpath entry org. ...
- ZOJ 1586 QS Network(Kruskal算法求解MST)
题目: In the planet w-503 of galaxy cgb, there is a kind of intelligent creature named QS. QScommunica ...
- 使用 ahk 让普通键盘变为Dvorak键盘
本文告诉大家,如何使用软件做出Dvorak键盘. 在开始说如何做之前,需要告诉大家,什么是Dvorak键盘. Dvorak Simplified Keyboard /ˈdvɔːræk, dəˈvɔː- ...
- Java - 数组JVM角度详解
http://developer.51cto.com/art/201001/176671.htm
- IDEA 构建为了打 jar 包的工程,包含 maven 打 jar 包的过程
前言:最近自己写了一个单表查询的组件,包含前端.后台,所以需要向阿里的 druid 一样将所有文件打到一个 jar 包里,这里首先记录如何打 jar 包. 附:自己的一个 jar 包源码 https: ...