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 ...
随机推荐
- CS231n学习笔记-图像分类笔记(下篇)
原文地址:智能单元 K-Nearest Neighbor分类器 大家可能注意到了,为什么只用最相似的一张图片的标签来作为测试图像的标签呢?这不是很奇怪吗!是的,使用K-Nearest Neighbor ...
- 【转】使用SQL Server 2012的FileTable轻松管理文件
一 .FileStream和FileTable介绍 我们经常需要把结构化数据(int.Char等)和非结构化数据(如Varbinary(max))一起存储,那我们在怎么存储的呢? 1. 在SQL Se ...
- mysql5.6常用查询sql
查看连接数,状态 1.查询进程 show processlist 查询到相对应的进程===然后 kill id 2.查询是否锁表show OPEN TABLES where In_use &g ...
- 中小团队快速构建SQL自动审核系统
SQL审核与执行,作为DBA日常工作中相当重要的一环,一直以来我们都是通过人工的方式来处理,效率低且质量没办法保证.为了规范操作,提高效率,我们决定引入目前市面上非常流行的SQL自动审核工具Incep ...
- springboot-mongodb的多数据源配置
pom.xml中引入mongodb的依赖 <dependency> <groupId>org.springframework.boot</groupId> < ...
- 《Head First 设计模式》读后总结:基础,原则,模式
基础 抽象 封装 多态 继承 原则 封装变化 多用组合,少用继承 针对接口编程,不针对实现编程 为交互对象之间的松耦合设计而努力 类应该对扩展开放,对修改关闭 依赖抽象,不要依赖具体类 只和朋友交谈 ...
- redis实战笔记(2)-第2章 使用 Redis构建Web应用
第2章 使用 Redis构建Web应用 本章主要内容 1.登录cookie 2.购物车cookie 3.缓存生成的网页 4.缓存数据库行 5.分析网页访问记录 本章的所有内容都是围绕着发现并解 ...
- .Net Core使用 MiniProfiler 进行性能分析(转)
转自:http://www.cnblogs.com/ideacore/p/9505425.html 官方文档: https://miniprofiler.com/dotnet/AspDotNetCor ...
- [有料组每日学习分享计划--00087]32行代码帮你导出IOS酷我音乐下载的无损音乐
需求与研究: 1.IOS的酷我音乐软件,还是不错滴,可以直接下载APE或是320K的MP3音乐,但是我发现PC上的酷我反而没这个功能,而且其他的音乐软件一般只能下载中低品质的音乐.所以能够从IOS中找 ...
- 撩课-Java每天5道面试题第24天
151.springMVC和struts2的区别有哪些? .springmvc的入口是一个servlet即前端控制器(DispatchServlet), 而struts2入口是一个filter过虑器( ...