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. Jmeter+Jenkins持续集成(一、环境准备)

    1.安装JDK (1)下载JDK,jdk8u181-x64.dmg 并进行安装 (2)配置环境变量.打开配置文件 open -e .bash_profile.按照自己的安装路径进行配置. JAVA_H ...

  2. List集合和Set集合UML图总结

    1.List和Set,用RationalRose展示 2.Map

  3. STM32F10XX学习笔记的石墨连接

    https://shimo.im/docs/QHGRrWxbeb0NiBm9/ <STM32F10X系列笔记>,可复制链接后用石墨文档 App 打开

  4. BZOJ2616 SPOJ PERIODNI(笛卡尔树 + DP)

    题意 N,K≤500,h[i]≤106N,K\le 500,h[i]\le10^6N,K≤500,h[i]≤106 题解 建立出小根堆性质的笛卡尔树,于是每个节点可以代表一个矩形,其宽度为子树大小,高 ...

  5. myeclipse不同版本共存破解办法

    我自己破解的是myeclipse10+myeclipse2018: 方法是:先破解myeclipse10.7,使用破解工具,到最后一步不关闭破解工具,再进行替换文件那一步,路径不选择10版本的,换成M ...

  6. Laradock Laravel database connection refused

    Laradock Laravel database connection refused SHARE  Laradock is a PHP development environment which ...

  7. BZOJ 4368: [IOI2015]boxes纪念品盒 贪心

    题意:给定一个环,环上有一些点包裹,你要从 $0$ 号点出发,然后每次带上一个容量为 $k$ 的背包. 问:如果要把所有的包裹都带回 $0$ 好点最少要走多少距离. 每一次只有 $3$ 种走法:走整圆 ...

  8. 解决chrome浏览器自动填充密码

    chrome会自动填充密码,解决方法很简单 使用下面的参考代码即可: <input type="password" readonly οnfοcus="this.r ...

  9. 【原创】go语言学习(十三)struct介绍2

    目录: 方法的定义 函数和方法的区别 值类型和指针类型 面向对象和继承 结构体和json序列化 方法的定义 1.和其他语言不一样,Go的方法采⽤用另外一种方式实现. package main impo ...

  10. create table:使用SELECT语句创建表

    oracle下直接(创建表) create table newtablename as select * from oldtablename sqlserver的语法是(自动创建表) : select ...