zoukankan      html  css  js  c++  java
  • 【Offer】[57-2] 【和为S的连续正数序列】

    题目描述

      输入一个正数s,打印出所有和为s的连续正数序列(至少含有两个数)。例如,输入15,由于1+2+3+4+5=4+5+6=7+8=15,所以打印出3个连续序列1~5、4~6和7~8。

    牛客网刷题地址

    思路分析

      我们也考虑用两个数small和big分别表示序列的最小值和最大值。首先把small初始化为1, big 初始化为2。如果从small到big的序列的和大于s,则可以从序列中去掉较小的值,也就是增大small的值。如果从small到big的序列的和小于s,则可以增大big,让这个序列包含更多的数字。因为这个序列至少要有两个数字,我们一直增加small到(1+s)/2为止。
      

    测试用例

    1. 功能测试:存在和为s的连续序列,如9、100等;不存在和为s的连续序列,如4、0等。
    2. 边界值测试:连续序列的最小和3。

    Java代码

    public class Offer057_02 {
        public static void main(String[] args) {
            test1();
            test2();
            test3();
    
        }
    
        public static ArrayList<ArrayList<Integer>> FindContinuousSequence(int sum) {
    
            return Solution1(sum);
        }
    
        private static ArrayList<ArrayList<Integer>> Solution1(int sum) {
    
            ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
            ArrayList<Integer> subList = new ArrayList<Integer>();
    
            if (sum < 3) {
                return list;
            }
            int small = 1;
            int big = 2;
            int middle = (sum + 1) >> 1;
            int curSum = small + big;
    
            while (small < middle) {
                if (curSum == sum) {
                    subList = add(small, big);
                    list.add(subList);
                }
                while (curSum > sum && small < middle) {
                    curSum -= sum;
                    small++;
                    if (curSum == sum) {
                        subList = add(small, big);
                        list.add(subList);
                    }
    
                }
                big++;
                curSum += big;
            }
    
            return list;
        }
    
        private static ArrayList<Integer> add(int start, int end) {
            ArrayList<Integer> subList = new ArrayList<Integer>();
            for (int i = start; i <= end; i++) {
                subList.add(i);
            }
            return subList;
        }
    
        private static void test1() {
    
        }
    
        private static void test2() {
    
        }
    
        private static void test3() {
    
        }
    }
    

    代码链接

    剑指Offer代码-Java

  • 相关阅读:
    不怕上不了 Android developers
    不花钱的可靠性设计
    linux2.6 内核特性配置
    Linux动态库的编译与使用 转载
    多线程使用互斥锁的C范例
    TSLIB 分析
    Notepad++中文版下载 以及HEX显示
    C程序实现在lcd 上全屏写 blue 色 及获取fb信息
    pthread_cond_wait的spurious wakeup问题
    查看linux内存条数的命令与清理内存使用
  • 原文地址:https://www.cnblogs.com/haoworld/p/offer572-he-weis-de-lian-xu-zheng-shu-xu-lie.html
Copyright © 2011-2022 走看看