和为S的连续正数序列
import java.util.ArrayList;
public class Solution {
public ArrayList<ArrayList<Integer> > FindContinuousSequence(int sum) {
ArrayList<ArrayList<Integer> > res= new ArrayList<ArrayList<Integer> >();
if(sum<=0) return res;
int leftp=1;int rightp=2;
int target=leftp+rightp;
while(leftp<(sum+1)/2){
if(target<sum){
rightp++;
target+=rightp;
}else if(target>sum){
target-=leftp;
leftp++;
}else{
ArrayList<Integer> list = new ArrayList<>();
for(int i =leftp;i<=rightp;i++){
list.add(i);
}
res.add(list);
rightp++;
target+=rightp;
}
}
return res;
}}
import java.util.ArrayList;
public class Solution {
public ArrayList<ArrayList<Integer> > FindContinuousSequence(int sum) {
ArrayList<ArrayList<Integer> > res =new ArrayList<ArrayList<Integer> >();
ArrayList<Integer> list1 =new ArrayList<Integer>();
int leftp = 1 ;
int rightp = 2;
if(sum<=0) return res;
int target = leftp + rightp;
while(leftp<(sum+1)/2){
if(target==sum){
ArrayList<Integer> list =new ArrayList<Integer>();
for(int i = leftp ;i <= rightp ; i++){
list.add(i);
}
res.add(list);
rightp++;
target+=rightp;
}else if(target < sum){
rightp++;
target += rightp;
}else{
target -= leftp;
leftp++;
}
}
return res;
}
}