JZ45 扑克牌顺子
本文最后更新于:2022年4月9日 中午
Solution
| class Solution { public: bool IsContinuous( vector<int> numbers ) { if (numbers.size() < 5) return false; vector<int> record(14, 0); int minVal = 14, maxVal = -1; for (int num : numbers) { if (num == 0) continue; if (++record[num] > 1) return false; minVal = min(minVal, num); maxVal = max(maxVal, num); } if (maxVal - minVal < 5) return true; return false; } };
|
方法二
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| class Solution { public: bool IsContinuous( vector<int> numbers ) { if (numbers.size() < 5) return false; sort(numbers.begin(), numbers.end()); int zero = 0, cnt = 0; for (int i = 0; i < numbers.size(); ++i) { if (numbers[i] == 0) { zero++; continue; } else if (i > 0 && numbers[i-1] != 0) { cnt += numbers[i] - numbers[i-1] - 1; if (cnt < 0) return false; } } return zero >= cnt; } };
|