输入一个正整数 target ,输出所有和为 target 的连续正整数序列(至少含有两个数)。
序列内的数字由小到大排列,不同序列按照首个数字从小到大排列。
示例 1:
输入:target = 9
输出:[[2,3,4],[4,5]]
示例 2:
输入:target = 15
输出:[[1,2,3,4,5],[4,5,6],[7,8]]
限制:
1 <= target <= 10^5
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/he-wei-sde-lian-xu-zheng-shu-xu-lie-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23class Solution { public: vector<vector<int>> findContinuousSequence(int target) { int i=1,j=1,sum=1; vector<vector<int>> res; while(1){ sum+=++j; while(sum>target){ sum-=i++; } if(sum==target){ vector<int> cur; for(int c=i;c<=j;++c){ cur.push_back(c); } res.push_back(cur); } if(j>=target/2+1) break; } return res; } };
最后
以上就是玩命蜗牛最近收集整理的关于Leetcode 面试题57 - II. 和为s的连续正数序列的全部内容,更多相关Leetcode内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复