Re: [閒聊] 每日leetcode已回收

看板Marginalman作者 (是oin的說)時間1年前 (2024/06/06 13:27), 編輯推噓2(200)
留言2則, 2人參與, 1年前最新討論串321/1548 (看更多)
※ 引述 《SecondRun (南爹摳打)》 之銘言: :   : 846. Hand of Straights :   : 給定一整數陣列和分組大小 : 回傳可否把數字分組,每一組的數字要連續 :   : Example 1: : Input: hand = [1,2,3,6,2,3,4,7,8], groupSize = 3 : Output: true : Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8] :   : Example 2: : Input: hand = [1,2,3,4,5], groupSize = 4 : Output: false : Explanation: Alice's hand can not be rearranged into groups of 4. : 思路 : 反正一定是要每一個數字都用來排他的卡組 先看能不能整除 然後就把每個數字都塞進map 因為要有序 所以就每一次都拿最小的數字組成卡組 然後刪除他們 如果可以組成的話就 可以 不行就return false class Solution { public: bool isNStraightHand(vector<int>& hand, int groupSize) { int len = hand.size(); if(len%groupSize != 0)return false; map<int,int> paper; for(int k : hand) { paper[k]++; } while(!paper.empty()) { vector<int> now; for(auto it = paper.begin() ; it != paper.end() ; it ++) { now.push_back(it->first); it->second--; if(it->second == 0)paper.erase(it); if(now.size() == groupSize)break; } if(now.size() != groupSize) return false; for(int i = 0 ; i < groupSize-1 ; i ++) { if(now[i] != now[i+1]-1)return false; } } return true; } }; -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 101.12.148.213 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1717651624.A.548.html

06/06 13:27, 1年前 , 1F
大師
06/06 13:27, 1F

06/06 13:30, 1年前 , 2F
別捲了
06/06 13:30, 2F
文章代碼(AID): #1cOKYeL8 (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1cOKYeL8 (Marginalman)