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. 【数组】Maximum Product Subarray

    题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...

  2. Java跨语言调用,使用JNA访问Java外部接口

    1. JNA简单介绍 先说JNI(Java Native Interface)吧,有过不同语言间通信经历的一般都知道,它允许Java代码和其他语言(尤其C/C++)写的代码进行交互,只要遵守调用约定即 ...

  3. mybatis异常:Error instantiating class com.psc.bean.User with invalid types () or values ().

    Error instantiating class com.psc.bean.User with invalid types () or values (). 是由于bean类没有无参构建方法,添加一 ...

  4. ggplot2基础学习

    前言 ggplot2是R语言最流行的第三方扩展包,是RStudio首席科学家Hadley Wickham读博期间的作品,是R相比其他语言一个独领风骚的特点.包名中“gg”是grammar of gra ...

  5. 《深入理解Java虚拟机》目录

    第一部分 走进Java 第1章 走进Java   第二部分 自动内存管理机制 第2章 Java内存区域与内存溢出异常 2.2 运行时数据区域 2.3 HotSpot虚拟机对象探秘 第3章 垃圾收集器与 ...

  6. lucene基本原理

    1.术语 lucene 在存储它的全文索引结构时,是有层次结构的,这涉及到5个层次:索引(Index):段(Segment):文档(Document):域(Field):词(Term),他们的关系如下 ...

  7. [转]wx.getUserInfo(OBJECT) 微信小程序 获取用户信息

    本文转自:http://mp.weixin.qq.com/debug/wxadoc/dev/api/open.html wx.getUserInfo(OBJECT) 获取用户信息,withCreden ...

  8. @Resource和@Autowired区别

    @Resource和@Autowired都是做bean的注入时使用 历史:  @Autowired     属于Spring的注解    org.springframework.beans.facto ...

  9. C#动态创建Gridview及批量插入到数据库

    这里介绍两种动态创建Gridview的方法: (一).有时需要应付上头领导的检查,所以就弄一些静态的Gridview来显示数据,这种方法的优点就是不用连接数据库,比较方便,但是代码灵活性不高,所有数据 ...

  10. 【转】C#中continue、break和return用法

    continue和break的用法一样,直接写上这个单词,后面加一个分号就行 比如: continue; break; 我们先来谈continue 看代码 for (int i=0; i<10; ...