闲鱼自动化工具完全指南:让手机自动完成所有日常运营任务
2026/1/3 22:34:20
Problem: 846. Hand of Straights 一手顺子
耗时97%,首先判断数组长度是否被gS整除,以及gS是否==1,然后排序的,初始化状态数组status,初始化变量,pre初始化到hand[0]-1,然后判断是否hand[j] == pre + 1且status[j]==false,若前后数字相同,则重置起始点start且j++,下一次从start开始,若没有重置起始点也就是没有相同的数字,则起始点j+1,然后pre = hand[start] - 1,若找遍了数组,累计cnt!=groupSize则返回false
class Solution { public: bool isNStraightHand(vector<int>& hand, int groupSize) { int n = hand.size(); if(n % groupSize != 0) return false; if(groupSize == 1) return true; sort( hand.begin(), hand.end() ); int len = n / groupSize, start = 0, pre = hand[0]-1, cnt, j; bool findstart; vector<bool> status(n, false); for(int i = 0; i < len; i++) { cnt = 0; findstart = false; for(j = start; j < n; j++) { if(status[j]==false && hand[j] == pre + 1) { pre = hand[j]; cnt++; status[j] = true; if(findstart == false && j+1<n && hand[j]==hand[j+1]) { findstart = true; start = j+1; j++; } } if(cnt==groupSize) break; } if(cnt != groupSize) return false; if(findstart == false) { start = j + 1; } if(start < n) { pre = hand[start] - 1; } } return true; } };