左闭右开区间。start=1,end=1,终止条件:start<=target/2;
图解:来源:https://leetcode-cn.com/problems/he-wei-sde-lian-xu-zheng-shu-xu-lie-lcof/solution/shi-yao-shi-hua-dong-chuang-kou-yi-ji-ru-he-yong-h/
/**
* 输出所有和为S的连续正数序列。序列内按照从小至大的顺序,
* 序列间按照开始数字从小到大的顺序
* @param sum 和为S
* @return 连续正数序列
*/
public ArrayList<ArrayList<Integer> > FindContinuousSequence(int sum) {
ArrayList<ArrayList<Integer>> res = new ArrayList<>();
int start = 1, end = 1, nSum = 0;
while (start <= sum / 2 ) {
if (nSum < sum) {
nSum += end;
end++;
} else if (nSum > sum) {
nSum -= start;
start++;
} else {
ArrayList<Integer> one = new ArrayList<>();
for (int i = start; i < end; i++) {
one.add(i);
}
res.add(one);
nSum -= start;
start++;
}
}
return res;
}