1. 程式人生 > >LeetCode-594. Longest Harmonious Subsequence(Java)

LeetCode-594. Longest Harmonious Subsequence(Java)

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:
5 Explanation: 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提示超時

相關推薦

LeetCode-594. Longest Harmonious Subsequence(Java)

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

leetcode-594-Longest Harmonious Subsequence

ont note val find 其他 size define XP () 題目描述: We define a harmonious array is an array where the difference between its maximum value and

[LeetCode] 594. Longest Harmonious Subsequence

題:https://leetcode.com/problems/longest-harmonious-subsequence/description/ 題目 We define a harmonious array is an array where the difference b

LeetCode 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, y

【python3】leetcode 594. Longest Harmonious Subsequence(easy)

594. Longest Harmonious Subsequence(easy) We define a harmonious array is an array where the difference between its maximum value and its mini

594. Longest Harmonious Subsequence - LeetCode

子序列 leetcode 圖片 iou desc 保存 return moni get Question 594. Longest Harmonious Subsequence Solution 題目大意:找一個最長子序列,要求子序列中最大值和最小值的差是1。 思路:構造

[LeetCode&Python] Problem 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

594. Longest Harmonious Subsequence(python+cpp)

題目: We define a harmonious array is an array where the difference between its maximum value and its

[LeetCode] Longest Harmonious Subsequence 最長和諧子序列

wiki ray note maximum mon ren enc imu max We define a harmonious array is an array where the difference between its maximum value and

LeetCode 300. Longest Increasing Subsequence —— 最長上升子序列(Java)

什麽 || 序列 無法 tput while 多少 需要 con Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Input:

Longest Harmonious Subsequence

efi new amp length con imu code ray style We define a harmonious array is an array where the difference between its maximum value and its

[leetcode-300-Longest Increasing Subsequence]

imp run target creat ble exit only lis there Given an unsorted array of integers, find the length of longest increasing subsequence. For

Leetcode 516. Longest Palindromic Subsequence

ems 字符 解題思路 desc pos stp i+1 class 長度 問題鏈接 Leetcode 516 題目解析 求最長回文子序列。 解題思路 子序列和子串有區別的,子序列不需要連續,相對位置遞增即可。 動態規劃。對於任意字符串,如果頭尾字符相同,那麽字符串的最長回

[leetcode]300. Longest Increasing Subsequence最長遞增子序列

rip imp complex sorted nec 序列 lan ted pla Given an unsorted array of integers, find the length of longest increasing subsequence. Examp

Leetcode-300. Longest Increasing Subsequence

https://leetcode.com/problems/longest-increasing-subsequence/description/ Given an unsorted array of integers, find the length of longest increasing

Leetcode 300 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 l

[LeetCode] 522. Longest Uncommon Subsequence II

ott longest pre order vat 特殊字符串 lean bool you Given a list of strings, you need to find the longest uncommon subsequence among them.

leetcode 516. Longest Palindromic Subsequence 動態規劃優化問題

0 這個問題是求一個串中,迴文子串的最長的長度。 1 分析 首先分析是否能分解為子問題,s[0,i]與s[0,i+1]是否有關聯?有關聯,因為對s[0,i]後面加上一個字元x後,字元x可能是一個迴文串的最後一個字元,從而造成迴文串的增長。在串s[0,i]後面加上一個字元x後,挨個與之前的串進

LeetCode:521. Longest Uncommon Subsequence I(找出特殊的子字串)

      Given a group of two strings, you need to find the longest uncommon subsequence of this group of two strings. The longest uncommo

[leetcode] 300. Longest Increasing Subsequence (Medium)

題意: 求最長增長的子序列的長度。 思路: 利用DP存取以i作為最大點的子序列長度。 Runtime: 20 ms, faster than 35.21% of C++ online submissions for Longest Increasing Subsequence. cl