[LeetCode] 954. Array of Doubled Pairs 涓ゅ€嶆暟瀵瑰効鏁扮粍

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LeetCode] 954. Array of Doubled Pairs 涓ゅ€嶆暟瀵瑰効鏁扮粍相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/ems' title='ems'>ems   issue   oss   als   code   log   https   example   NPU   


Given an array of integers?A?with even length, return?true?if and only if it is possible to reorder it such that?A[2 * i + 1] = 2 * A[2 * i]?for every?0 <=?i < len(A) / 2.

Example 1:

Input: [3,1,3,6]
Output: false

Example 2:

Input: [2,1,2,6]
Output: false

Example 3:

Input: [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].

Example 4:

Input: [1,2,4,16,8,4]
Output: false

Note:

  1. 0 <= A.length <= 30000
  2. A.length?is even
  3. -100000 <= A[i] <= 100000

杩欓亾棰樿鏄粰浜嗕竴涓伓鏁伴暱搴︾殑鏁扮粍锛岄棶鑳戒笉鑳介噸鏂版帓搴忥紝浣垮緱浠庡紑澶磋捣锛屾瘡涓や釜缁勬垚涓€涓锛屽叾涓悗闈㈢殑鏁板瓧鏄墠闈㈢殑鏁板瓧鐨勪袱鍊嶃€傞鐩腑鐨勫潗鏍囧暐鐨勪笉鐢ㄧ锛屾湰璐ㄥ氨鏄皢鎵€鏈夌殑鏁板瓧涓や袱鍒嗙粍锛屼竴涓暟鏄彟涓€涓暟瀛楃殑涓ゅ€嶅氨琛屼簡銆傚崥涓绘渶寮€濮嬬殑鏂规硶鏄敤涓€涓?HashSet锛屽浜庢瘡涓暟瀛楋紝鏌ユ壘鏄叾2鍊嶇殑鏁板瓧鍜屾槸鍏朵簩鍒嗕箣涓€鐨勬暟瀛楋紙蹇呴』鏁撮櫎锛夛紝鑻ュ湪 HashSet 涓瓨鍦紝鍒欏皢鍏剁Щ闄わ紝鍚﹀垯灏嗛亶鍘嗘暟瀛楀姞鍏?HashSet锛屾渶鍚庣湅 HashSet 鏄惁涓虹┖銆傝繖绉嶆柟娉曚笉閫傜敤浜庢湁閲嶅鏁板瓧鐨勬儏鍐碉紝鎵€浠ラ渶瑕佹崲鎴?HashMap 鏉ュ仛锛岀劧鍚庡缓绔嬫瘡涓暟瀛椾笌鍏跺嚭鐜版鏁颁箣闂寸殑鏄犲皠銆備负浜嗙畝渚挎悳绱㈢殑杩囩▼锛屽彲浠ョ粰鏁板瓧鎺掑簭锛岃繖鏍峰氨鍙互浠庢渶灏忕殑鏁板瓧寮€濮嬪鐞嗭紝鍙鏌ユ壘鍏?鍊嶇殑鏁板瓧鍗冲彲銆備絾鏄綋鏁扮粍涓瓨鍦ㄨ礋鏁扮殑鏃跺€欙紝鐩存帴鎺掑簭杩樻槸浼氬嚭闂锛屾瘮濡?-8 浼氭帓鍦?-4 鍓嶉潰锛屼絾鍏跺疄鏄渶瑕佸厛澶勭悊 -4 鐨勶紝鍥犱负 -4 x 2 = -8銆傛墍浠ラ渶瑕佽嚜瀹氫箟鎺掑簭鐨勮鍒欙紝鎸夌収鏁板瓧鐨勭粷瀵瑰€煎ぇ灏忔帓搴忥紝杩欐牱 -4 灏卞彲浠ユ帓鍦?-8 鍓嶉潰銆傛帓濂藉簭浜嗕箣鍚庡氨鍙互浠庡紑澶村鐞嗘暟瀛椾簡锛屽浜庢瘡涓暟瀛?key锛屽亣濡傚叾鍑虹幇娆℃暟澶т簬 key 鐨?鍊嶇殑鏁板瓧鍑虹幇涓暟锛屽垯璇存槑涓€瀹氬浣欑殑 key 鏃犳硶鍖归厤锛岀洿鎺ヨ繑鍥?false銆傚惁鍒?key 鐨?鍊嶆暟瀛楃殑鏄犲皠鍊煎噺鍘?key 鐨勬槧灏勫€硷紝骞舵帴缁亶鍘嗗嵆鍙紝鍙傝浠g爜濡備笅锛?/p>

瑙f硶涓€锛?/p>

class Solution {
public:
    bool canReorderDoubled(vector<int>& A) {
        unordered_map<int, int> m;
        for (int num : A) ++m[num];
        vector<int> keys;
        for (auto &a : m) keys.push_back(a.first);
        sort(keys.begin(), keys.end(), [](int i, int j) {return abs(i) < abs(j);});
        for (int key : keys) {
            if (m[key] > m[2 * key]) return false;
            m[2 * key] -= m[key];
        }
        return true;
    }
};

鎴戜滑涔熷彲浠ヤ娇鐢?TreeMap 鏉ュ缓绔嬫槧灏勶紝鍒╃敤鍏惰嚜鍔ㄦ帓搴忕殑鐗规€с€備絾鏄繖閲岃繕鏄瓨鍦ㄤ笂闈㈡彁鍒扮殑璐熸暟鐨勯棶棰橈紝鍒欓渶瑕佺壒娈婄殑澶勭悊涓€涓嬨€備粠鏈€灏忕殑鏁板瓧寮€濮嬪鐞嗭紝鐢变簬鍙兘鍏堝鐞?-8锛岃€屼笉鏄?-4锛屾墍浠ュ湪鎵剧洰鏍囨暟瀛楃殑鏃跺€欓渶瑕佸垽鏂繖涓暟瀛楃殑姝h礋锛岃嫢鏄礋鏁帮紝鍒欓櫎浠?锛岃嫢鏄鏁帮紝鍒欎箻浠?銆傜劧鍚庡湪鍒ゆ柇鍋囧褰撳墠鏁板瓧鏄礋鏁帮紝涓旇繕鏄鏁帮紝鍒欑洿鎺ヨ繑鍥?false锛屽洜涓烘病鏈夋瘮璇ユ暟杩樺皬鐨勬暟瀛楋紝鎵€浠ヤ笉鑳戒箻浠?锛屽張鍥犱负鍏舵槸濂囨暟锛屼笉鑳介櫎浠?锛屾墍浠ユ敞瀛ょ敓銆傝繕鏈夊氨鏄垽鏂嫢褰撳墠鏁板瓧鐨勬槧灏勫€煎ぇ浜庣洰鏍囨暟瀛楃殑鏄犲皠鍊硷紝涔熺洿鎺ヨ繑鍥?false锛岃繖涓湪涓婇潰鐨勮В娉曚腑瑙i噴杩囦簡銆備箣鍚庣洰鏍囩殑鏄犲皠鍊煎噺鍘诲綋鍓嶆暟瀛楃殑鏄犲皠鍊硷紝褰撻亶鍘嗙粨鏉熶箣鍚庤繑鍥?true锛屽弬瑙佷唬鐮佸涓嬶細


瑙f硶浜岋細

class Solution {
public:
    bool canReorderDoubled(vector<int>& A) {
        map<int, int> m;
        for (int num : A) ++m[num];
        for (auto &a : m) {
            if (a.second == 0) continue;
            int want = a.first < 0 ? a.first / 2 : a.first * 2;
            if ((a.first < 0 && a.first % 2 != 0) || a.second > m[want]) return false;
            m[want] -= a.second;
        }
        return true;
    }
};

Github 鍚屾鍦板潃:

https://github.com/grandyang/leetcode/issues/954


鍙傝€冭祫鏂欙細

https://leetcode.com/problems/array-of-doubled-pairs/

https://leetcode.com/problems/array-of-doubled-pairs/discuss/209564/Java-Heap-Concise

https://leetcode.com/problems/array-of-doubled-pairs/discuss/203183/JavaC%2B%2BPython-Match-from-the-Smallest-or-Biggest-100


LeetCode All in One 棰樼洰璁茶В姹囨€?鎸佺画鏇存柊涓?..)









以上是关于[LeetCode] 954. Array of Doubled Pairs 涓ゅ€嶆暟瀵瑰効鏁扮粍的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode Degree of an Array

[leetcode]Array-697. Degree of an Array

LeetCode OJ 238. Product of Array Except Self 解题报告

#leetcode#Product of Array Except Self

LeetCode: Product of Array Except Self

LeetCode 977. Squares of a Sorted Array