leetcode659 Split Array into Consecutive Subsequences

Posted 王宜鸣

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 };

 

以上是关于leetcode659 Split Array into Consecutive Subsequences的主要内容,如果未能解决你的问题,请参考以下文章

**Leetcode 659. Split Array into Consecutive Subsequences

LeetCode 659: Split Array into Consecutive Subsequence

[LeetCode] 659. Split Array into Consecutive Subsequences 将数组分割成连续子序列

leetcode410 Split Array Largest Sum

[LeetCode] Split Array with Equal Sum 分割数组成和相同的子数组

LeetCode 548. Split Array with Equal Sum (分割数组使得子数组的和都相同)$