【Leetcode_easy】697. Degree of an Array
problem
题意:首先是原数组的度,其次是和原数组具有相同的度的最短子数组。那么最短子数组就相当于子数组的首末数字都是统计度的数字。
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的更多相关文章
- 【LeetCode】697. Degree of an Array 解题报告
[LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...
- 【LeetCode】697. Degree of an Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求出最短相同子数组度的长度 使用堆求最大次数和最小长 ...
- 【26】Remove Duplicates from Sorted Array
[26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...
- 697. Degree of an Array - LeetCode
697. Degree of an Array - LeetCode Question 697. Degree of an Array - LeetCode Solution 理解两个概念: 数组的度 ...
- 【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 ...
- 【LeetCode】Two Sum II - Input array is sorted
[Description] Given an array of integers that is already sorted in ascending order, find two numbers ...
- 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 ...
- 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 ...
- [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 ...
随机推荐
- Mybatis之动态SQL&OGNL表达式
1.接口 public interface MemberMapperDynamicSQL { public List<Members> selectMembersByIf(Members ...
- guava字符串工具--------Joiner 根据给定的分隔符把字符串连接到一起
public class JoinerTest { public static void main(String args[]){ //1.将list字符串集合,以,形式转为字符串 List<S ...
- NodeJS事件环
1. 执行顺序说明 1. 清空主执行栈 2. 清空微任务队列 3. 运行一个timer队列的回调函数,询问微任务队列,如果有回调函数,清空. 4. 循环第3步,直到清空timer队列 5. 进入pol ...
- Convert AS400 Spool to PFD Tools – PDFing
1. Steps There’s a tool PDFing convert spool file to PDF with simple way. No need install AS400 obje ...
- Tomcat 解决jvm中文乱码,控制台乱码
解决方法 打开tomcat/conf/目录 修改logging.properties 找到 java.util.logging.ConsoleHandler.encoding = utf-8 这行 更 ...
- Proxmox VE 的安装和简单使用
Proxmox VE Proxmox ve 安装 如果proxmox源太慢了.可以使用国内源 download.proxmox.wiki 直接替换就可以了. ISO U盘方式安装 下载地址:https ...
- slax自启动程序
Fluxbox 本身提供了自启动程序的功能.~/.fluxbox/startup 文件是一个像启动 Fluxbox 一样自启动应用程序的脚本.# 标记是注释. 一个简单的例子: #!/bin/sh # ...
- 将页面中所有的checkbox设成单选得
$(function () { var allBox = $(":checkbox"); allBox.click(function () { allBox.removeAttr( ...
- CSPS2019谢幕记
留坑待填. 开始填坑. Day -1 最后一场模拟题.T1花2h切掉,T2啥都不会,T3看出部分解法然后写了暴搜,教练没放子任务,得了45,然后就$rk3$了. 晚上疯狂写板子,1.5h写了8份板子. ...
- IIS Express(电脑无管理员权限如何启用VS调试)
转载页面:https://www.cnblogs.com/xbblogs/p/4756552.html(详细版) 直接按照红色字体步骤执行,其他字体可先忽略 出问题再详细看 1.设置配置文件 ...