题解 | #和为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<>(); int slow = 1,fast = 2; while(fast > slow){ int curSum = (slow + fast)*(fast-slow+1)/2; if(curSum == sum){ ArrayList<Integer> save = new ArrayList<>(); for(int i = slow;i <= fast;i++){ save.add(i); } res.add(save); slow++; }else if(curSum < sum){ fast++; }else{ slow++; } } return res; } }