• 和为S的连续正整数序列(双指针法)


    题目描述

    小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100。但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数)。没多久,他就得到另一组连续正数和为100的序列:18,19,20,21,22。现在把问题交给你,你能不能也很快的找出所有和为S的连续正数序列? Good Luck!

    输出描述:

    输出所有和为S的连续正数序列。序列内按照从小至大的顺序,序列间按照开始数字从小到大的顺序

    解题:双指针法,(Tcp中的滑动窗口技巧)

    设有两个类似指针的东西指向和为S序列开头和结尾的值,所以两个指针最初所指向的是plow=1,phigh=2;

    之后分为3种情况:

    1.目前的和小于所要求的和(即cur<sum),此时高处的指针应该向右滑动,phigh++;

    2.目前的和大于所要求的和(即cur>sum),此时低处的指针应该向右滑动,plow++;

    3.目前和都等于所要求的和(即cur==sum),此时先将值push_back到res中,之后再将res push_back到allRes中,plow++,寻找新的序列

    class Solution {
    public:
        vector<vector<int> > FindContinuousSequence(int sum) {
            vector<vector<int> > allRes;
            int phigh = 2,plow = 1;
             
            while(phigh > plow){
                int cur = (phigh + plow) * (phigh - plow + 1) / 2;
                if( cur < sum)
                    phigh++;
                 
                if( cur == sum){
                    vector<int> res;
                    for(int i = plow; i<=phigh; i++)
                        res.push_back(i);
                    allRes.push_back(res);
                    plow++;
                }
                 
                if(cur > sum)
                    plow++;
            }
             
            return allRes;
        }
    };

     用vector之后,如果输出不好控制,所以用二维数组进行替换,详见:https://www.cnblogs.com/cstdio1/p/11243441.html

    java代码:

    class Solution {
        public int[][] findContinuousSequence(int target) {
        List<int[]> res = new ArrayList<>();
       
        for(int l=1,r=2;l<r;){
            int sum=(r-l+1)*(l+r)/2;//sum=n*(a1+an)/2
             if(sum>target) l++;
             else if(sum<target) r++; 
             else{
                int []t = new int[r-l+1];
                for(int i=l;i<=r;i++){
                 t[i-l]=i;
                }
                res.add(t);
                l++;
            }
        }
    
    
        return res.toArray(new int[0][]);
    }
    
    }
    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()][]);
    }
    不一样的烟火
  • 相关阅读:
    sql server 总结一下堆和索引的东西
    公开课笔记:美联储与经济危机(一:金本位,大萧条)
    关于一个springcloud项目中学到的(二:分布式集群,Maven,JDKJRE,编译和解释)
    关于一个springcloud项目中学到的(一:感受)
    sql 经典查询50题 思路(一)
    信息熵的定义和简单计算
    使用Mulesoft建立webservice, jax-ws方式, wsdl first
    使用Mulesoft建立webservice, simple方式,POJO
    编写高质量代码 改善Python程序的91个建议
    23 种编程学习资料免费分享,你想要的都有
  • 原文地址:https://www.cnblogs.com/cstdio1/p/11242623.html
Copyright © 2020-2023  润新知