题解 | 和为S的连续正数序列

和为S的连续正数序列

https://www.nowcoder.com/practice/c451a3fd84b64cb19485dad758a55ebe

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param sum int整型 
     * @return int整型vector<vector<>>
     */
    vector<vector<int> > FindContinuousSequence(int sum) {
        int left = 1;
        int right = 2;
        vector<vector<int>> res;
        while(left < right){
            int cur = (left + right) *(right - left + 1) / 2;
            if(sum == cur){
                vector<int> tmp;
                for(int i = left; i <= right; i++){
                    tmp.push_back(i);
                }
                res.push_back(tmp);
                right++;
            }
            else if(sum > cur){
                right++;
            }
            else{
                left++;
            }
        }
        return res;
        
        // write code here
    }
};

全部评论
直接滑动窗口,因为测试数据很小
点赞 回复 分享
发布于 04-14 12:47 上海

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务