[LeetCode] 594. Longest Harmonious Subsequence

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LeetCode] 594. Longest Harmonious Subsequence相关的知识,希望对你有一定的参考价值。

 https://leetcode.com/problems/longest-harmonious-subsequence

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

 

以上是关于[LeetCode] 594. Longest Harmonious Subsequence的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 594. Longest Harmonious Subsequence

[LeetCode] 594. Longest Harmonious Subsequence

leetcode-594-Longest Harmonious Subsequence

594. Longest Harmonious Subsequence - LeetCode

LeetCode 594. 最长和谐子序列 Longest Harmonious Subsequence (Easy)

594. Longest Harmonious Subsequence