JZ41
和为S的连续正数序列
http://www.nowcoder.com/questionTerminal/c451a3fd84b64cb19485dad758a55ebe
思路
使用双指针进行求解
Code
import java.util.ArrayList; public class Solution { public ArrayList<ArrayList<Integer> > FindContinuousSequence(int sum) { ArrayList<ArrayList<Integer> > ret = new ArrayList<>(); if(sum < 3) return ret; int lp = 1, rp = 2; int curNum = 3; ArrayList<Integer> tmp; while(lp < rp) { if(curNum == sum) { tmp = new ArrayList<>(); for(int i = lp ; i <= rp; i++) { tmp.add(i); } ret.add(tmp); curNum -= lp; lp++; }else if(curNum < sum) { rp++; curNum += rp; } else if(curNum > sum) { curNum -= lp; lp++; } } return ret; } }