题解 | #和为S的连续正数序列#
和为S的连续正数序列
http://www.nowcoder.com/practice/c451a3fd84b64cb19485dad758a55ebe
import java.util.ArrayList; public class Solution { public ArrayList<ArrayList<Integer> > FindContinuousSequence(int sum) { ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>(); int left = 1, right = 2; while(left < right){ int s = (right + left)*(right - left + 1)/2; if(s > sum){ left++; }else if(s < sum){ right++; }else{ ArrayList<Integer> list = new ArrayList<>(); for(int i = left; i <= right; ++i){ list.add(i); } res.add(list); right++; } } return res; } }