【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 ...
随机推荐
- Educational Codeforces Round 75 (Rated for Div. 2) A. Broken Keyboard
链接: https://codeforces.com/contest/1251/problem/A 题意: Recently Polycarp noticed that some of the but ...
- Restful架构API编码规范
Restful API 目前比较成熟的一套互联网应用程序的API设计理论 一.协议 API与用户的通信协议,总是使用HTTPs协议. 二.域名 应该尽量将API部署在专用域名之下. https://a ...
- 009——Matlab调用cftool工具的函数方法
(一)参考文献:http://blog.sina.com.cn/s/blog_4a0a8b5d0100uare.html (二)视频教程:https://v.qq.com/x/page/x30392r ...
- junit报错
java.lang.RuntimeException: iwap 环境还没有初始化,请先调用IWapContext.init(). at com.nantian.ofpiwap.IWapContext ...
- 【luogu1251】餐巾计划问题--网络流建模,费用流
题目描述 一个餐厅在相继的 N 天里,每天需用的餐巾数不尽相同.假设第 iii 天需要 ri块餐巾( i=1,2,...,N).餐厅可以购买新的餐巾,每块餐巾的费用为 p 分;或者把旧餐巾送到快洗部 ...
- P1143 进制转换
漂亮小姐姐点击就送:https://www.luogu.org/problemnew/show/P1143 题目描述 请你编一程序实现两种不同进制之间的数据转换. 输入输出格式 输入格式: 输入数据共 ...
- linux下redis的安装、启动、关闭和卸载
edis 在Linux 和 在Windows 下的安装是有很大的不同的,和通常的软件安装是一样的. 一 下载 Redis 安装包 去redis 官网下载reids 安装包, redis 官网默认只提 ...
- C++ 结构体指针的定义
struct node { …… } ; struct node *p1, *p2 ; typedef struct node { …… }Node; typedef Node* pNode; typ ...
- 牛客OI周赛10-提高组:B-Taeyeon的困惑(值域线段树)
做法 单点加单点删,在值域线段树上直接二分就能求值前\(K\)小的和 Code #include<bits/stdc++.h> typedef long long LL; const LL ...
- vue中子组件传值给父组件
index.js 子组件 父组件