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的更多相关文章

  1. 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 ...

  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. 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 ...

  4. [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 ...

  5. LeetCode Degree of an Array

    原题链接在这里:https://leetcode.com/problems/degree-of-an-array/description/ 题目: Given a non-empty array of ...

  6. 【LeetCode】697. Degree of an Array 解题报告

    [LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...

  7. 【LeetCode】697. Degree of an Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求出最短相同子数组度的长度 使用堆求最大次数和最小长 ...

  8. 697. Degree of an Array - LeetCode

    697. Degree of an Array - LeetCode Question 697. Degree of an Array - LeetCode Solution 理解两个概念: 数组的度 ...

  9. [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 ...

随机推荐

  1. linux内核模块的安全

    linux可以动态的加载内核模块,在很多场合可能需要确保加载内核的安全性.如果被攻击者加载恶意内核模块,将会使得内核变得极其危险. 当然,稳妥的做法就是给内核模块进行签名,内核只加载能正确验证的签名. ...

  2. 快速初步了解Neo4j与使用

    快速初步了解Neo4j与使用 Neo4j是一个高性能的,NOSQL图形数据库,它将结构化数据存储在网络上而不是表中.它是一个嵌入式的.基于磁盘的.具备完全的事务特性的Java持久化引擎,但是它将结构化 ...

  3. Tomcat性能调优-让小猫飞奔

    一.总结前一天的学习 从“第三天”的性能测试一节中,我们得知了决定性能测试的几个重要指标,它们是: ü   吞吐量 ü   Responsetime ü   Cpuload ü   MemoryUsa ...

  4. android 解决studio生成aar包并在其他工程引用aar包的坑,不需要任何gradle配置

    1.首先我们创建一个module 2.编写我们的一个类 3.编译我们的module,生成release版本的aar,注意千万不要是debug版本的, 点击最右边的gradle面板,选择我们的modul ...

  5. C#中通过Lambda表达式为委托传入更多的参数

    如: DispatcherTimer dispatcherTimer = new DispatcherTimer(); dispatcherTimer.Tick += (o, e) => { d ...

  6. Kafka安装kafka-manager

    1 .下载kafka-manager 想要查看和管理Kafka,完全使用命令并不方便,我们可以使用雅虎开源的Kafka-manager,GitHub地址如下: https://github.com/y ...

  7. HDU 2682 Tree(Kruskal算法求解MST)

    题目: There are N (2<=N<=600) cities,each has a value of happiness,we consider two cities A and ...

  8. [转]How to nest transactions nicely - "begin transaction" vs "save transaction" and SQL Server

    本文转自:http://geekswithblogs.net/bbiales/archive/2012/03/15/how-to-nest-transactions-nicely---quotbegi ...

  9. VB如何连接访问数据库Access

    VB如何连接访问数据库Access 听语音 | 浏览:10675 | 更新:2015-05-05 11:26 | 标签:连接 access 1 2 3 4 5 6 7 分步阅读 VB即Visual B ...

  10. sql先分组,再算百分比

    --先分组,再算百分比 SELECT  a.CooperationIntention ,         a.OrganizationID ,         COUNT(*) 数量 , CONVER ...