讨论/《画解剑指 Offer》 - 剑指 Offer 57 - II. 和为 s 的连续正数序列/
《画解剑指 Offer》 - 剑指 Offer 57 - II. 和为 s 的连续正数序列
class Solution {
    public int[][] findContinuousSequence(int target) {
        List<int[]> res = new ArrayList<>();
        int right = (target + 1) / 2;  // 右边界
        int fir = 1;
        while (fir < right) {
            int sum = fir;
            int temp = fir;
            for (int sec = fir + 1; sec <= right; sec++) {
                sum += sec;
                if (sum == target) {
                    int[] nums = new int[sec - fir + 1];
                    for (int i = 0;i < nums.length;i++) {
                        nums[i] = temp++;
                    }
                    res.add(nums);
                    break;
                } else if (sum < target) {
                    continue;
                } else if (sum > target) {
                    break;
                }
            }
            fir++;
        }
        return res.toArray(new int[res.size()][]);
    }
}
展开全部 3 讨论