LeetCode剑指 Offer 57 - II. 和为s的连续正数序列

题目

输入一个正整数 target ,输出所有和为 target 的连续正整数序列(至少含有两个数)。
序列内的数字由小到大排列,不同序列按照首个数字从小到大排列。链接

思路

以前做了都忘了,滑动窗口的印象不深。。
滑动窗口右边界注意初始化为1或者2,别和二分的right搞混了。窗口始终向右移动,和较小右边界扩张,和较大右移左边界,直到左边界移动到阈值。

//牛客啰嗦版本,有一说一ArrayList<ArrayList<Integer>>比数组好多了
//但也好不到哪去。。
public class Solution {
    
    
    public ArrayList<ArrayList<Integer>> FindContinuousSequence(int sum) {
    
    
        ArrayList<ArrayList<Integer>> res = new ArrayList<>();
        int left = 1, right = 2;
        int limit = sum / 2;
        while(left <= limit){
    
    
            int temp = getSum(left,right);
            if(temp > sum){
    
    
                left++;
            }else if(temp < sum){
    
    
                right++;
            }else{
    
    
                addRes(res,left,right);
                left++;
            }
        }
        return res;
    }
    int getSum(int left, int right){
    
    
        return (left + right) * (right - left + 1) / 2;
    }
    void addRes(ArrayList<ArrayList<Integer>> res, int left, int right){
    
    
        ArrayList<Integer>add = new ArrayList<>();
        for(int i = left;i <= right;i++){
    
    
            add.add(i);
        }
        res.add(add);
    }
}
//LeetCode版本
class Solution {
    
    
    public int[][] findContinuousSequence(int target) {
    
    
        //窗口左边界
        int left = 1;
        //窗口右边界
        int right = 1;
        //求和
        int sum = 0;
        //奇数情况,窗口只有两个值,left 可取最大为target/2
        //left+(left+1)==target
        int limit = target / 2;
        List<int[]> l = new ArrayList<>();
        while(left <= limit){
    
    
            //sum小,right右移增大区间
            if(sum < target){
    
    
                sum += right;
                right++;
            }//sum大,left右移减小区间
            else if(sum > target){
    
    
                sum -= left;
                left++;
            }else{
    
    
                //滑动窗口左闭右开,t的长度为right-left
                int[]t = new int[right - left];
                for(int i = 0, j = left; i < t.length; i++, j++){
    
    
                    t[i] = j;
                }
                l.add(t);
                //左边界右移,寻找下一个满足的区间
                sum -= left;
                left++;
            }
        }
        return l.toArray(new int[l.size()][]);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_42007742/article/details/106958380