offer-57-2


输入一个正整数 target ,输出所有和为 target 的连续正整数序列(至少含有两个数)。

序列内的数字由小到大排列,不同序列按照首个数字从小到大排列。

示例 1:

输入:target = 9
输出:[[2,3,4],[4,5]]

示例 2:

输入:target = 15
输出:[[1,2,3,4,5],[4,5,6],[7,8]]

限制:

  • 1 <= target <= 10^5

//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int[][] findContinuousSequence(int target) {
        if (target == 1 || target == 2) {
            return new int[0][];
        }
        List<List<Integer>> resultList = new ArrayList<>();
        int curSum = 1;

        int start = 1, end = 1;

        while (start <= target / 2) {
            if (curSum == target) {
                List<Integer> oneSe = new ArrayList<>();
                oneSe.add(start);
                oneSe.add(end);
                resultList.add(oneSe);
                curSum -= start;
                start++;
            } else if (curSum < target) {
                end++;
                curSum += end;
            } else {
                curSum -= start;
                start++;
            }
        }

        if (resultList.size() == 0) {
            return new int[0][];
        }

        int[][] result = new int[resultList.size()][];
        int index = 0;

        for (List<Integer> oneSe : resultList) {
            int[] oneArr = new int[oneSe.get(1) - oneSe.get(0) + 1];
            for (int i = oneSe.get(0); i <= oneSe.get(1); i++) {
                oneArr[i - oneSe.get(0)] = i;
            }
            result[index++] = oneArr;
        }

        return result;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

文章作者: 倪春恩
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 倪春恩 !