leetcode 954. Array of Doubled Pairs
Given an integer array of even length?arr
, return?true
?if it is possible to reorder?arr
?such that?arr[2 * i + 1] = 2 * arr[2 * i]
?for every?0 <= i < len(arr) / 2
, or?false
?otherwise.
?
Example 1:
Input: arr = [3,1,3,6]Output: false
Example 2:
Input: arr = [2,1,2,6]Output: false
Example 3:
Input: arr = [4,-2,2,-4]Output: trueExplanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
?
Constraints:
2 <= arr.length <= 3 * 104
arr.length
?is even.-105?<= arr[i] <= 105
第一次用treemap.
treemap實現(xiàn)了sort的接口,所以可以直接排序完成,對于小的數(shù)(>0),去判斷他的2倍數(shù)是否也在map中,而且小的數(shù)的value值應(yīng)該要小于等于2倍數(shù)的value值,依次loop即可;
對于負數(shù)(<0)則判斷它一半大小的值是否在map中,同時value值小于等于一半大小的value值。loop 結(jié)果;
Runtime:?42 ms, faster than?90.76%?of?Java?online submissions for?Array of Doubled Pairs.
Memory Usage:?50.1 MB, less than?81.79%?of?Java?online submissions for?Array of Doubled Pairs.
Next challenges:
Find Original Array From Doubled Array