| 4 | using namespace std; |
| 5 | |
| 6 | bool isPossible(vector<int>& nums) |
| 7 | { |
| 8 | unordered_map<int, int> nc, tail; |
| 9 | // 遍历vector |
| 10 | for(int & num : nums) { |
| 11 | nc[num] ++; |
| 12 | } |
| 13 | // 遍历unordered_map |
| 14 | for(auto & it : nc) { |
| 15 | cout << "nc.first: " << it.first << " nc.second: " << it.second << endl; |
| 16 | } |
| 17 | |
| 18 | // LeetCode 659:https://leetcode-cn.com/problems/split-array-into-consecutive-subsequences/ |
| 19 | for(int & num : nums) { |
| 20 | if (nc[num] == 0) { |
| 21 | continue; |
| 22 | } |
| 23 | // 这个else if顺序不能调换 |
| 24 | else if (nc[num] > 0 && nc[num+1] > 0 && nc[num+2] > 0) { |
| 25 | nc[num] --; |
| 26 | nc[num+1] --; |
| 27 | nc[num+2] --; |
| 28 | tail[num+2] ++; |
| 29 | } else if (nc[num] > 0 && tail[num-1] > 0) { |
| 30 | tail[num-1] --; |
| 31 | tail[num] ++; |
| 32 | nc[num] --; |
| 33 | } else { |
| 34 | return false; |
| 35 | } |
| 36 | } |
| 37 | // 遍历unordered_map |
| 38 | for(auto & it : tail) { |
| 39 | cout << "tail.first: " << it.first << " tail.second: " << it.second << endl; |
| 40 | } |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | int main() |
| 45 | { |