[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;
    }
}
时间: 2024-09-28 22:45:44

[LeetCode] 594. Longest Harmonious Subsequence的相关文章

594. Longest Harmonious Subsequence 最长的和谐子序列

Total Accepted: 5540 Total Submissions: 14066 Difficulty: Easy Contributors:love_Fawn 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

594. Longest Harmonious Subsequence

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 subseque

[LeetCode] Longest Harmonious Subsequence 最长和谐子序列

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 subseque

Longest Harmonious Subsequence

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 subseque

[LeetCode][JavaScript]Longest Increasing Subsequence

Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest increasing subsequence. For example,Given [10, 9, 2, 5, 3, 7, 101, 18],The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Not

【Leetcode】Longest Increasing Subsequence

题目链接:https://leetcode.com/problems/longest-increasing-subsequence/ 题目: Given an unsorted array of integers, find the length of longest increasing subsequence. For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The longest increasing subsequence is [2,

LeetCode 1143. Longest Common Subsequence

原题链接在这里:https://leetcode.com/problems/longest-common-subsequence/ 题目: Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some cha

LeetCode 516. Longest Palindromic Subsequence

516. Longest Palindromic Subsequence Add to List Description Submission Solutions Total Accepted: 2159 Total Submissions: 5216 Difficulty: Medium Contributors: Stomach_ache Given a string s, find the longest palindromic subsequence's length in s. You

LeetCode 300. Longest Increasing Subsequence

300. Longest Increasing Subsequence Description Submission Solutions Add to List Total Accepted: 64115 Total Submissions: 170859 Difficulty: Medium Contributors: Admin Given an unsorted array of integers, find the length of longest increasing subsequ