LeetCode-594. Longest Harmonious Subsequence(Java)

来源:互联网 发布:姓氏排序软件下载 编辑:程序博客网 时间:2024/06/15 09:13

We define a harmonious array is an array where the difference between its maximum value and its minimum value is exactly 1.

Now, given an integer array, you need to find the length of its longest harmonious subsequence among all its possible subsequences.

Example 1:

Input: [1,3,2,2,5,2,3,7]Output: 5Explanation: The longest harmonious subsequence is [3,2,2,2,3].
------------------------------------------------------------------------------------------------------------------------------------------------------------------------

题意

从一个数组中找到一个harmonious array,这个harmonious array定义为数组的最大值和最小值之差等于1,现在需要找到最长的这种数组。

思路

找到最长的就是要找到出现次数最多的,然后可以利用hashmap来统计某个数字的出现次数,然后再循环数组,找每个数字n的次数,以及每个数字加1(n+1)后的数字出现次数,

然后比较结果,找到最大的即可。不需要找n-1的出现次数。

代码

public static int findLHS(int[] nums){Map<Long, Integer> map = new HashMap<>();   for (long num : nums) {            map.put(num, map.getOrDefault(num, 0) + 1);    }    int result = 0;    for (long key : map.keySet()) {           if (map.containsKey(key + 1)) {            result = Math.max(result, map.get(key + 1) + map.get(key));            }    }    return result;}

这里有用到map.getOrDefault(args,default)方法,这个方法的作用是在map中找key等于args,如果找到返回value,如果没找到则返回默认值default。

相似的方法putIfAbsent方法,这个方法也是在map中根据key找,如果找到,返回value,如果没找到,插入这个key,返回null;

putIfAbsent

If the specified key is not already associated with a value (or is mapped to null) associates it with the given value and returns null, else returns the current value.


getOrDefault

Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.


If your goal is only to retrieve the value, then use getOrDefault. Else, if you want to set the value when it does not exist, use putIfAbsent.

我写的另外一个方法

public static int findLHS(int[] nums) {        int length = nums.length;        if(nums == null || nums.length <=0)        return 0;        int firstValue = nums[0];        int i=1;        int maxCount=0;        while(i<length){        int count=0;        int j=0;        boolean flag = false;        while(j<length){        if( nums[j] == firstValue+1){                        flag = true;        count++;        }        if(nums[j] == firstValue){        count++;        }        j++;        }        firstValue = nums[i];        if(count > maxCount && flag){        maxCount = count;        }                i++;                }        return maxCount;                    }
运行了几个测试用例,返回值都是对的,但是leetCode提示超时

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 肾绞疼引起的呕吐怎么办 肾绞痛肚子胀气怎么办 iga肾炎肉眼血尿怎么办 结石疼怎么办怎么缓解 肾有问题严重怎么办 肾结石突然很疼怎么办 输尿管结石肉眼血尿该怎么办 结石引起肾绞痛怎么办 尿结石支架后尿里老有血怎么办? 结石堵在输尿管怎么办 尿路结石痛怎么办 怀孕了有肾结石怎么办 怀孕有肾结石怎么办啊 肾结石无疼血尿怎么办 胆囊胆管都结石怎么办 肾里面有肿瘤怎么办 肾癌手术后发烧怎么办 尿结石堵住尿道怎么办 尿结石不能排尿怎么办 肾癌小便有血怎么办 膀胱癌膀胱全切怎么办 怀孕了有阑尾炎怎么办 食物堵塞在食管怎么办 食物卡在食管怎么办 小孩食道卡异物怎么办 八十岁老人得了膀胱癌怎么办 肾结石引起吐血尿血怎么办 肾结石引起的尿血怎么办 食道感觉有异物怎么办 膀胱出血有血块怎么办 肾小球滤过率20怎么办 膀胱癌术后有血尿怎么办 肾病贫血怎么办吃什么 低蛋白血症怎么办 慢性肾炎患者感冒了怎么办 透析病人磷高怎么办 尿毒症透析磷高怎么办 怀孕了有膀胱炎怎么办 宝宝拉肚子尿少怎么办 猫尿血怎么办吃什么药 肝癌小便不出来怎么办