剑指Offer之和为S的连续正数序列
题目描述
1 import java.util.ArrayList; 2 public class Solution { 3 public ArrayList<ArrayList<Integer> > FindContinuousSequence(int sum) { 4 ArrayList<ArrayList<Integer>> array=new ArrayList<>(); 5 int plow=1,phigh=2; 6 while(phigh>plow) { 7 int psum=(phigh+plow)*(phigh-plow+1)/2; 8 if(psum==sum) { 9 ArrayList<Integer> list=new ArrayList<>(); 10 for(int i=plow;i<=phigh;i++) 11 list.add(i); 12 array.add(list); 13 plow++; 14 } 15 else if(psum<sum) 16 phigh++; 17 else 18 plow++; 19 } 20 return array; 21 } 22 }