首页 > 其他 > 详细

leetcode659 Split Array into Consecutive Subsequences

时间:2017-08-21 19:18:06      阅读:343      评论:0      收藏:0      [点我收藏+]

思路:

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

实现:

 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

原文:http://www.cnblogs.com/wangyiming/p/7406148.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!