problem

697. Degree of an Array

题意:首先是原数组的度,其次是和原数组具有相同的度的最短子数组。那么最短子数组就相当于子数组的首末数字都是统计度的数字。

solution1:

class Solution {
public:
int findShortestSubArray(vector<int>& nums) {
int n = nums.size(), res = INT_MAX, degree = ;
unordered_map<int, int> m;
unordered_map<int, pair<int, int>> pos;
for(int i=; i<n; ++i)
{
m[nums[i]]++;
if(m[nums[i]]==) pos[nums[i]] = {i, i};
else pos[nums[i]].second = i;
degree = max(degree, m[nums[i]]);
}
for(auto a:m)
{
if(degree == a.second)
{
res = min(res, pos[a.first].second-pos[a.first].first+);
}
}
return res;
}
};

solution2:

参考

1. Leetcode_easy_697. Degree of an Array;

2. Grandyang;

【Leetcode_easy】697. Degree of an Array的更多相关文章

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

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

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

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

  3. 【26】Remove Duplicates from Sorted Array

    [26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...

  4. 697. Degree of an Array - LeetCode

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

  5. 【LeetCode】Search in Rotated Sorted Array——旋转有序数列找目标值

    [题目] Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...

  6. 【LeetCode】Two Sum II - Input array is sorted

    [Description] Given an array of integers that is already sorted in ascending order, find two numbers ...

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

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

  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. Java图形界面

    图形界面 JFrame在swingbao JFrame jframe = new JFrame(); iframe.setVisible(true); //设置窗口显示 jframe.setLocat ...

  2. 第一章使用JSP/Server技术开发新闻发布系统第一章动态网页开发基础

      一:为什么需要动态网页    由于静态网页的内容是固定的,不能提供个性化和定制化得服务,使用动态网页可真正地与用户实现互动. 二:什么是动态网页  ①:动态网页是指在服务器端运行的,使用程序语言设 ...

  3. 使用webuploader实现大文件上传分片上传

    本人在2010年时使用swfupload为核心进行文件的批量上传的解决方案.见文章:WEB版一次选择多个文件进行批量上传(swfupload)的解决方案. 本人在2013年时使用plupload为核心 ...

  4. ueditor从word粘贴公式

    官网地址http://ueditor.baidu.com Git 地址 https://github.com/fex-team/ueditor 参考博客地址 http://blog.ncmem.com ...

  5. python 调用未绑定的超类构造方法

    class Bird: def __init__(self): self.hungry = True def eat(self): if self.hungry: print('Aaaaah') se ...

  6. Python学习之--基础语法

    一.定义 Python 是一种解释型.面向对象.动态数据类型的高级程序设计语言. 二.Python变量的命名规则 1. 变量名只能包含字母.数字和下划线: 2. 变量名不能包含空格: 3. 不要将Py ...

  7. 分布式锁 ----zookeeper实践 (排它锁)

    排它锁概念: Exclusive Locks,被称为X锁,写锁,独占锁.如果事物T1对数据对象O1加上了排它锁,那么在整个加锁期间,只允许事务T1对O1进行读写操作,其他事务必须等到T1释放锁后才能进 ...

  8. mysql的 UUID的生成方式

    之前一直用的  int 自增的方式,之后总觉得缺少自信.  之后,我觉得采用uuid的方式,可能会好一些,至于用户统计排序等,则另用属性进行记录.   这里设计到一对矛盾:  安全性 与  网络带宽利 ...

  9. $('#jyzjg').combobox('clear');

        $('#jyzjg').combobox('clear');                   alert($('#jyzjg').combobox("getValue" ...

  10. Spring Boot :Failed to instantiate SLF4J LoggerFactory Reported exception:

    Spring Boot出现以下错误: Failed to instantiate SLF4J LoggerFactory Reported exception: Failed to instantia ...