diff --git a/notes/57.2 和为 S 的连续正数序列.md b/notes/57.2 和为 S 的连续正数序列.md index f09420f2..5ab87287 100644 --- a/notes/57.2 和为 S 的连续正数序列.md +++ b/notes/57.2 和为 S 的连续正数序列.md @@ -1,8 +1,8 @@ # 57.2 和为 S 的连续正数序列 -## 题目描述 +## 题目链接 -[牛客网](https://www.nowcoder.com/practice/c451a3fd84b64cb19485dad758a55ebe?tpId=13&tqId=11194&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github) +[Leetcode](https://leetcode.cn/problems/he-wei-sde-lian-xu-zheng-shu-xu-lie-lcof/) ## 题目描述 @@ -16,28 +16,29 @@ ## 解题思路 ```java -public ArrayList> FindContinuousSequence(int sum) { - ArrayList> ret = new ArrayList<>(); +public int[][] fileCombination(int target) { + List ret = new ArrayList<>(); int start = 1, end = 2; int curSum = 3; - while (end < sum) { - if (curSum > sum) { + while (end < target) { + if (curSum > target) { curSum -= start; start++; - } else if (curSum < sum) { + } else if (curSum < target) { end++; curSum += end; } else { - ArrayList list = new ArrayList<>(); - for (int i = start; i <= end; i++) - list.add(i); - ret.add(list); + int[] arr = new int[end - start + 1]; + for (int i = start; i <= end; i++) { + arr[i - start] = i; + } + ret.add(arr); curSum -= start; start++; end++; curSum += end; } } - return ret; + return ret.toArray(new int[ret.size()][]); } ```