topic
Input a positive integer target, and output all consecutive positive integer sequences (containing at least two numbers) whose sum is target.
The numbers in the sequence are arranged from small to large, and different sequences are arranged from small to large according to the first number. link
Ideas
I forgot to do it before, the sliding window is not very impressive. .
Note that the right boundary of the sliding window is initialized to 1 or 2. Don't right
confuse it with the binary . The window always moves to the right, expands with a smaller right border, and moves a larger right to the left border, until the left border moves to the threshold.
//牛客啰嗦版本,有一说一ArrayList<ArrayList<Integer>>比数组好多了
//但也好不到哪去。。
public class Solution {
public ArrayList<ArrayList<Integer>> FindContinuousSequence(int sum) {
ArrayList<ArrayList<Integer>> res = new ArrayList<>();
int left = 1, right = 2;
int limit = sum / 2;
while(left <= limit){
int temp = getSum(left,right);
if(temp > sum){
left++;
}else if(temp < sum){
right++;
}else{
addRes(res,left,right);
left++;
}
}
return res;
}
int getSum(int left, int right){
return (left + right) * (right - left + 1) / 2;
}
void addRes(ArrayList<ArrayList<Integer>> res, int left, int right){
ArrayList<Integer>add = new ArrayList<>();
for(int i = left;i <= right;i++){
add.add(i);
}
res.add(add);
}
}
//LeetCode版本
class Solution {
public int[][] findContinuousSequence(int target) {
//窗口左边界
int left = 1;
//窗口右边界
int right = 1;
//求和
int sum = 0;
//奇数情况,窗口只有两个值,left 可取最大为target/2
//left+(left+1)==target
int limit = target / 2;
List<int[]> l = new ArrayList<>();
while(left <= limit){
//sum小,right右移增大区间
if(sum < target){
sum += right;
right++;
}//sum大,left右移减小区间
else if(sum > target){
sum -= left;
left++;
}else{
//滑动窗口左闭右开,t的长度为right-left
int[]t = new int[right - left];
for(int i = 0, j = left; i < t.length; i++, j++){
t[i] = j;
}
l.add(t);
//左边界右移,寻找下一个满足的区间
sum -= left;
left++;
}
}
return l.toArray(new int[l.size()][]);
}
}