zoukankan      html  css  js  c++  java
  • LeetCode——面试题57

    输入一个正整数 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)
    链接:https://leetcode-cn.com/problems/he-wei-sde-lian-xu-zheng-shu-xu-lie-lcof

    滑动窗口

    c++

    vector<vector<int>> findContinuousSequence(int target) {
        int i = 1; // 滑动窗口的左边界
        int j = 1; // 滑动窗口的右边界
        int sum = 0; // 滑动窗口中数字的和
        vector<vector<int>> res;
    
        while (i <= target / 2) {
            if (sum < target) {
                // 右边界向右移动
                sum += j;
                j++;
            } else if (sum > target) {
                // 左边界向右移动
                sum -= i;
                i++;
            } else {
                // 记录结果
                vector<int> arr;
                for (int k = i; k < j; k++) {
                    arr.push_back(k);
                }
                res.push_back(arr);
                // 右边界向右移动
                sum -= i;
                i++;
            }
        }
    
        return res;
    }
    

    java

    public int[][] findContinuousSequence(int target) {
        int i = 1; // 滑动窗口的左边界
        int j = 1; // 滑动窗口的右边界
        int sum = 0; // 滑动窗口中数字的和
        List<int[]> res = new ArrayList<>();
    
        while (i <= target / 2) {
            if (sum < target) {
                // 右边界向右移动
                sum += j;
                j++;
            } else if (sum > target) {
                // 左边界向右移动
                sum -= i;
                i++;
            } else {
                // 记录结果
                int[] arr = new int[j-i];
                for (int k = i; k < j; k++) {
                    arr[k-i] = k;
                }
                res.add(arr);
                // 右边界向右移动
                sum -= i;
                i++;
            }
        }
        return res.toArray(new int[res.size()][]);
    }
    

    python

    def findContinuousSequence(self, target: int) -> List[List[int]]:
        i = 1 # 滑动窗口的左边界
        j = 1 # 滑动窗口的右边界
        sum = 0 # 滑动窗口中数字的和
        res = []
    
        while i <= target // 2:
            if sum < target:
                # 右边界向右移动
                sum += j
                j += 1
            elif sum > target:
                # 左边界向右移动
                sum -= i
                i += 1
            else:
                # 记录结果
                arr = list(range(i, j))
                res.append(arr)
                # 右边界向右移动
                sum -= i
                i += 1
    
        return res
    

    双指针

    滑动窗口+项数和

    c++

    class Solution {
    public:
        vector<vector<int>> findContinuousSequence(int target) {
            vector<vector<int>>vec;
            vector<int> res;
            for (int l = 1, r = 2; l < r;){
                int sum = (l + r) * (r - l + 1) / 2;
                if (sum == target){
                    res.clear();
                    for (int i = l; i <= r; ++i) res.emplace_back(i);
                    vec.emplace_back(res);
                    l++;
                }
                else if (sum < target) r++;
                else l++;
            }
            return vec;
        }
    };
    

    数学方法,确定项数以及首末项之和

    python

    class Solution:
        def findContinuousSequence(self, target: int) -> List[List[int]]:
            ans=[]
            for i in range(target//2,1,-1):
                # i是项数. i个连续正数的首项与末项和为2*target//i。联立方程得到a_1和a_n的表达式。
                if (2*target)%i==0 and (2*target//i+i)%2 and 2*target//i>=i:
                    ans.append(list(range((2*target//i-i+1)//2,(2*target//i+i+1)//2)))
    
            return ans
    
  • 相关阅读:
    cogs 826. [Tyvj Feb11] GF打dota 次短路详细原创讲解! dijkstra
    cogs 2450. 距离 树链剖分求LCA最近公共祖先 快速求树上两点距离 详细讲解 带注释!
    cogs 647. [Youdao2010] 有道搜索框 Trie树 字典树
    cogs 293. [NOI 2000] 单词查找树 Trie树字典树
    P4550 收集邮票
    P1850 换教室 期望dp
    洛谷 UVA11021 Tribles
    P3802 小魔女帕琪 概率与期望
    P3369 【模板】普通平衡树 01Trie树
    P2765 魔术球问题
  • 原文地址:https://www.cnblogs.com/wwj99/p/12424988.html
Copyright © 2011-2022 走看看