leetcode659 Split Array into Consecutive Subsequences

思路:

对于每个数,尽量放在已有子序列的后面;如果不能,就创建一个新的子序列。

实现:

 1 class Solution 
 2 {
 3 public:
 4     bool isPossible(vector<int>& nums) 
 5     {
 6         unordered_map<int, int> cnt, end;
 7         for (auto i : nums) cnt[i]++;
 8         for (auto i : nums)
 9         {
10             if (cnt[i] == 0) continue;
11             else if (end[i - 1] > 0)
12             {
13                 end[i - 1]--;
14                 end[i]++;
15             }
16             else if (cnt[i + 1] > 0 && cnt[i + 2] > 0)
17             {
18                 cnt[i + 1]--;
19                 cnt[i + 2]--;
20                 end[i + 2]++;
21             }
22             else return false;
23             cnt[i]--;
24         }
25         return true;
26     }
27 };
原文地址:https://www.cnblogs.com/wangyiming/p/7406148.html