题解 | #和为S的连续正数序列#
和为S的连续正数序列
https://www.nowcoder.com/practice/c451a3fd84b64cb19485dad758a55ebe
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param sum int整型 * @return int整型ArrayList<ArrayList<>> */ public ArrayList<ArrayList<Integer>> FindContinuousSequence (int sum) { // write code here ArrayList<ArrayList<Integer>> res = new ArrayList<>(); if (sum == 0) return res; ArrayList<Integer> win = new ArrayList<>(); int right = 1, left = 1; int t = 0; while (right < sum) { win.add(right); t += right; right ++ ; while (t > sum) { win.remove(0); t -= left; left ++ ; } if (t == sum) res.add(new ArrayList(win)); } return res; } }