题解 | #和为S的连续正数序列#
和为S的连续正数序列
http://www.nowcoder.com/practice/c451a3fd84b64cb19485dad758a55ebe
import java.util.ArrayList; public class Solution { public ArrayList<ArrayList > FindContinuousSequence(int sum) { ArrayList<ArrayList> result=new ArrayList<>(); if(sum==0) return result; for(int j=1;j<sum;j++){ int zc=0; ArrayList data = new ArrayList<>(); for(int i=j;i<sum;i++){ // System.out.print("i="+i+" "); zc+=i; if(zc<sum){ // System.out.print("<"+" "); continue; }
if(zc==sum){
for(int k=j;k<=i;k++){
data.add(k);
// System.out.print("k="+k); } } if(zc>sum){ // System.out.println(">"+" "); break; }
}
if(!data.isEmpty())
result.add(data);
}
return result;
}
}