697. Degree of an Array

来源:互联网 发布:万达告别房地产 知乎 编辑:程序博客网 时间:2024/06/06 01:20

问题: 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

Note:
nums.length will be between 1 and 50,000.
nums[i] will be an integer between 0 and 49,999.

问题分析: 输入一个字符串,输出具有最多相同字符的子串的最短长度。

解题思路: 用哈希映射数据结构来解决问题,在键中存储数组元素,重新定义一个包含起始位置begin、长度length、与相同个数times的数据类型,用于值中。最后对值进行选取,先判断个数最多的值,其次再次数最多且相等的情况下,选择长度最短长度,即是所要的输出。

*class Solution {                    public int findShortestSubArray(int[] nums) {                         Map<Integer,Beginlength> map = new HashMap<Integer,Beginlength>();                        int begin ;                        int length = 0;                        int times = 0;                        for(int i = 0; i < nums.length;i++){                                     if(!map.containsKey(nums[i])){                                begin = i;                                length = 1;                                times = 1;                            }                            else{                                begin = map.get(nums[i]).begin;                                times = ++ map.get(nums[i]).times;                                length = i - begin + 1;                            }                               map.put(nums[i], new Beginlength(begin,length,times)); //传递实例化对象                        }                        int j = 0;                        int time = 0;                        for(Beginlength i:map.values()){                        if(i.times > time){                              time = i.times;                              j = i.length;                            }                        else if(i.times == time){                            j =(j>i.length)?i.length:j;                          }                        }                        return j;                    }}        class Beginlength{            int begin ;            int length;            int times;            Beginlength(int begin, int length,int times){                this.begin = begin;                this.length = length;                this.times = times;            }    }   

运行时间为49ms。

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 老板让写的报道没有当天写完怎么办 洛奇英雄传镶嵌有微章的时装怎么办 上午12点用24小时制怎么办 三分钟看懂捷信个人贷款怎么办 精神不正常的父亲到单位闹怎么办 母亲和父亲一直和我闹怎么办 儿子拿了父亲的钱买手机怎么办 电动车骑的慢的时候车头打漂怎么办 二晓啊相公太爱我了怎么办全文免费 车牌被套牌了又有违章怎么办 高中生只学好主课副科学不好怎么办 母泰迪怀孕23天了不吃饭怎么办 媳妇一再触碰我的底线怎么办 发票商品编码好多选错了分类怎么办 吃了用福尔马林泡的食物怎么办 没大没小说话不尊重人的孩子怎么办 升级安卓8.0后app闪退怎么办 业主装门占用消防通道物业怎么办? 想改名字派出所不给改怎么办 物业不给地热打压影响装修怎么办 pos机pin效码验证错误怎么办 苹果下载东西要发验证码怎么办 接口断在了丝扣里面怎么办 政府下了一张关停取缔单怎么办 微信运动数据不刷新了怎么办 邻居霸占我的土地不还我该怎么办 双层水浴式杀菌锅阀门坏了怎么办 通下水管的钢丝断水管里了怎么办 塑料水管与水阀连接处漏水怎么办 介质流向标识管道温度太高怎么办 雪碧里气体太多瓶子涨得要炸怎么办 餐厅不清洗油烟管道物业该怎么办 用完权健洁净宝后奇痒的厉害怎么办 老房子铸铁暖气管从外锈蚀了怎么办 德意壁挂炉补水多了水压过高怎么办 新空调的散热片铜管漏了怎么办 猪舍被别人推掉漏电电到人怎么办? 网上买的燃气灶连接用软管怎么办 哈弗h3后减振器上支架坏了怎么办 汽车用收录机电机皮带断了怎么办 大便太粗把马桶堵了怎么办