思路: 不想用一些容器,sort后每次都从最小且值不是-1的开始找,找到的纪录-1,size不是groupSize整数倍的early return,performance意外好分享一下
class Solution {
public:
bool isNStraightHand(vector<int>& hand, int groupSize) {
sort(hand.begin(),hand.end());
if(hand.size()%groupSize)return false;
for(int i = 0;i<hand.size();i++){
if(hand[i]!=-1){
int k = 0;
for(int j = 1;j<groupSize;j++){
while(k<hand.size()-i){
if((hand[i]+j)!=hand[i+k])k++;
else{
hand[i+k] = -1;
break;
}
if(k == hand.size()-i)return false;
}
}
hand[i] = -1;
}
}
for(int i = 0;i<hand.size();i++){
if(hand[i]!= -1)return false;
}
return true;
}
};