※ 引述《oin1104 (是oin的说)》之铭言:
: ※ 引述 《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