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. for循环中使用async/await

    async function printFiles () { const files = await getFilePaths(); await Promise.all(files.map(async ...

  2. DYNAMIC LINK LIBRARY - DLL

    https://www.tenouk.com/ModuleBB.html MODULE BB DYNAMIC LINK LIBRARY - DLL Part 1: STORY What do we h ...

  3. tsql 通过row_number() over() 产生行号

    先按userIP分组,再按时间排序,最后编号. select row_number() over (partition by UserIp order by insertTime),* from us ...

  4. 01_Request和Response

    参考文档 http://www.iamnancy.top/djangorestframework/Responses/ https://q1mi.github.io/Django-REST-frame ...

  5. 34、[源码]-AOP原理-链式调用通知方法

    34.[源码]-AOP原理-链式调用通知方法

  6. How to Set Up a NFS Server on Debian 10 Buster

    How to Set Up a NFS Server on Debian 10 Buster Nick Congleton Debian 24 May 2019   Contents 1. Softw ...

  7. Educational Codeforces Round 55 题解

    题解 CF1082A [Vasya and Book] 史上最难A题,没有之一 从题意可以看出,翻到目标页只有三种办法 先从\(x\)到\(1\),再从\(1\)到\(y\) 先从\(x\)到\(n\ ...

  8. Java 有状态和无状态对象的区别

    无状态会话Bean   无状态就是对于一次操作,不能保存数据.无状态对象(Stateless Bean)是没有实例变量的对象,不能保存数据,是不变类,是线程安全的.例如:  public class ...

  9. postgresql数据的入门教程

    postgreSQL数据库简介 PostgreSQL 是一个免费的对象-关系数据库服务器(ORDBMS),在灵活的BSD许可证下发行. PostgreSQL 开发者把它念作 post-gress-Q- ...

  10. [CMS漏洞]EmpireCMS_V7.5的一次审计【转载】

    i春秋作家:Qclover 原文来自:EmpireCMS_V7.5的一次审计 0x01 概述 最近在做审计和WAF规则的编写,在CNVD和CNNVD等漏洞平台寻找各类CMS漏洞研究编写规则时顺便抽空对 ...