leetcode 697
Given a non-empty array of non-negative integers nums, the degree of this array is defined as the maximum frequency of any one of its elements.
Your task is to find the smallest possible length of a (contiguous) subarray of nums, that has the same degree as nums.
Example 1:
Input: [1, 2, 2, 3, 1]
Output: 2
Explanation:
The input array has a degree of 2 because both elements 1 and 2 appear twice.
Of the subarrays that have the same degree:
[1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2]
The shortest length is 2. So return 2.
Example 2:
Input: [1,2,2,3,1,4,2]
Output: 6
给定非空整数nums的非空数组,该数组的度数被定义为其任何一个元素的最大频率。
你的任务是找到num的(连续的)子阵列的最小可能长度,其与nums具有相同的度数。
例1:
输入:[1,2,2,3,1]
输出:2
说明:
输入数组的度数为2,因为元素1和2都出现两次。
在具有相同程度的子阵列中:
[1,2,2,3,1],[1,2,2,3],[2,2,3,1],[1,2,2],[2,2,3],[2] ,2]
最短的长度是2.所以返回2。
例2:
输入:[1,2,2,3,1,4,2]
输出:6
class Solution {
public int findShortestSubArray(int[] nums) {
int maxcount = 1;
HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
for (int i : nums) {
if (hm.containsKey(i)) {
hm.put(i, hm.get(i) + 1);
if (maxcount < hm.get(i)) {
maxcount = hm.get(i);
}
} else {
hm.put(i, 1);
}
}
Set<Integer> set = hm.keySet();
int minlength = Integer.MAX_VALUE;
for (int s : set) {
int temp = Integer.MAX_VALUE;
if (hm.get(s) == maxcount) {
int i = 0, j = nums.length - 1;
while (nums[i] != s && i < j)
i++;
while (nums[j] != s && i < j)
j--;
temp = j - i + 1;
}
minlength = Math.min(temp, minlength);
}
return minlength;
}
}
leetcode 697的更多相关文章
- 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] 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 ...
- Java实现 LeetCode 697 数组的度(类似于数组的map)
697. 数组的度 给定一个非空且只包含非负数的整数数组 nums, 数组的度的定义是指数组里任一元素出现频数的最大值. 你的任务是找到与 nums 拥有相同大小的度的最短连续子数组,返回其长度. 示 ...
- 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_Easy tag: Hash Table
Given a non-empty array of non-negative integers nums, the degree of this array is defined as the ma ...
- [LeetCode] All questions numbers conclusion 所有题目题号
Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, ...
- 【LeetCode】697. Degree of an Array 解题报告
[LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...
- 697. Degree of an Array - LeetCode
697. Degree of an Array - LeetCode Question 697. Degree of an Array - LeetCode Solution 理解两个概念: 数组的度 ...
- 【LEETCODE】50、数组分类,简单级别,题目:888,1013,896,485,448,697
package y2019.Algorithm.array; import java.util.HashSet; import java.util.Set; /** * @ProjectName: c ...
随机推荐
- JavaScript对象之get/set方法
我们可以重写js对象属性的get和set方法. 从上图我们可以看出set和get的语法. 上图则是使用set和get方法对对象的属性进行了输入校验. 从上图可得若对象的原型链上具有不可配置的同名属性( ...
- 实现简单的 JS 模块加载器
实现简单的 JS 模块加载器 1. 背景介绍 按需加载是前端性能优化的一个重要手段,按需加载的本质是从远程服务器加载一段JS代码(这里主要讨论JS,CSS或者其他资源大同小异),该JS代码就是一个模块 ...
- c数据结构 -- 使用链表实现计数
#include <stdio.h> #include <stdlib.h> typedef struct _node{ int value; struct _node *ne ...
- 【网页浏览】怀旧xp画图网页版
非常古老的WindowsXP画图工具 传送链接
- 安装 Navicat for MySQL
安装 Navicat for MySQL 下载地址:https://www.pcsoft.com.cn/soft/20832.html
- Java基础(十二)之包和权限访问
软件包 软件包解决了两个类名字一样的问题.软件包就是一个"文件夹". 包名的命名规范:1.要求所有字母都小写:2.包名一般情况下,是你的域名倒过来写.比如baidu.com,pac ...
- 【做题笔记】[NOIOJ,非NOIp原题]装箱问题
题意:给定一些矩形,面积分别是 \(1\times 1,2\times 2,3\times 3,4\times 4,5\times 5,6\times 6\).您现在知道了这些矩形的个数 \(a,b, ...
- 如何创建redis集群
1.下载redis源码包 wget http://download.redis.io/releases/redis-3.2.4.tar.gz 2.解压并安装 tar xvf redis-.tar.gz ...
- VIM学习(转)
原文:http://www.cnblogs.com/nerxious/archive/2012/12/21/2827303.html 断断续续的使用VIM也一年了,会的始终都是那么几个命令,效率极低 ...
- Mongodb学习笔记(四)管理
一.数据导出.导入 数据导入:mongoexport ./mongoimport -d 数据库 -c 集合 文件名 数据导出:mongoimport 参数说明: -d:指明使用的库,如text -c: ...