Skip to content

Commit 82010de

Browse files
committed
剑指Offer–041-和为S的连续正数序列--http://blog.csdn.net/gatieme/article/details/51357308
1 parent 5823f96 commit 82010de

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

041-和为S的连续正数序列/README.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
11
#链接
2-
-------
2+
-------
33

44

55
>牛客OJ:[和为S的连续正数序列](http://www.nowcoder.com/practice/c451a3fd84b64cb19485dad758a55ebe?tpId=13&tqId=11194&rp=2&ru=%2Fta%2Fcoding-interviews&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking)
6-
>
6+
>
77
>九度OJ:http://ac.jobdu.com/problem.php?pid=1354
8-
>
8+
>
99
>GitHub代码: [041-和为S的连续正数序列](https://github.com/gatieme/CodingInterviews/tree/master/041-和为S的连续正数序列)
1010
>
1111
>CSDN题解:[剑指Offer--041-和为S的连续正数序列](http://blog.csdn.net/gatieme/article/details/51357308)
1212
1313

14-
| 牛客OJ | 九度OJ | CSDN题解 | GitHub代码 |
15-
| ------------- |:-------------:| -----:|
14+
| 牛客OJ | 九度OJ | CSDN题解 | GitHub代码 |
15+
| ------ |:------:| --------:|:----------:|
1616
|[041-和为S的连续正数序列](http://www.nowcoder.com/practice/c451a3fd84b64cb19485dad758a55ebe?tpId=13&tqId=11194&rp=2&ru=%2Fta%2Fcoding-interviews&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking) | [1354-和为S的连续正数序列](http://ac.jobdu.com/problem.php?pid=1354) | [剑指Offer--041-和为S的连续正数序列](http://blog.csdn.net/gatieme/article/details/51357308) | [041-和为S的连续正数序列](https://github.com/gatieme/CodingInterviews/tree/master/041-和为S的连续正数序列) |
1717

1818

1919

20+
<br>**您也可以选择[回到目录-剑指Offer--题集目录索引](http://blog.csdn.net/gatieme/article/details/51916802)**
21+
22+
23+
2024
#题意
2125
-------
2226

2327

2428
**题目描述**
2529

2630

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

3034
>输出所有和为S的连续正数序列。序列内按照从小至大的顺序,序列间按照开始数字从小到大的顺序
@@ -50,12 +54,12 @@
5054

5155
我们先考虑个简单的
5256

53-
>参见
57+
>参见
5458
>
5559
>[LeetCode题解--1. Two Sum](http://blog.csdn.net/gatieme/article/details/50596965)
5660
5761

58-
| 牛客OJ | 九度OJ | CSDN题解 | GitHub代码 |
62+
| 牛客OJ | 九度OJ | CSDN题解 | GitHub代码 |
5963
| ------------- |:-------------:| -----:|
6064
|[041-和为S的两个数字](http://www.nowcoder.com/practice/390da4f7a00f44bea7c2f3d19491311b?tpId=13&tqId=11195&rp=2&ru=%2Fta%2Fcoding-interviews&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking) | [1352-和为S的连续正数序列](http://ac.jobdu.com/problem.php?pid=1352) | [剑指Offer--041-和为S的连续正数序列](http://blog.csdn.net/gatieme/article/details/51357308) | [041-和为S的两个数字](https://github.com/gatieme/CodingInterviews/tree/master/041-和为S的两个数字) |
6165

@@ -67,7 +71,7 @@
6771
>
6872
>举例说明
6973
>
70-
>例如输入数组{1 、2 、4、7 、11 、15 }和数字15.
74+
>例如输入数组{1 、2 、4、7 、11 、15 }和数字15.
7175
>
7276
>由于4+ 11 = 15 ,因此输出4 和11 。
7377
@@ -99,7 +103,7 @@
99103

100104
>比如
101105
> 1 2 3 4 5 6
102-
>
106+
>
103107
>相隔距离越远。乘积也越大
104108
> $1*6 < 2 * 5 < 3 * 4$
105109
@@ -132,7 +136,7 @@ public:
132136
{
133137
/// 左右夹逼
134138
/// 同时能保证乘积最小的
135-
/// 和为sum的最大的两个数最接近sqrt(sum)
139+
/// 和为sum的最大的两个数最接近sqrt(sum)
136140
res.push_back(array[start]);
137141
res.push_back(array[end]);
138142

@@ -158,7 +162,7 @@ public:
158162

159163
**解题思路**
160164

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

163167
以求和为9 的所有连续序列为例,我们先把start初始化为1, end初始化为2。此时介于start和end之间的序列是{1,2},序列的和为3,小于9,所以我们下一步要让序列包含更多的数字。我们把end增加1 变成3,此时序列为{ I, 2,坷。由于序列的和是6,仍然小于9,我们接下来再增加end变成4,介于start和end之间的序列也随之变成{ l, 2, 3, 4}。由于列的和10 大于9,我们要删去去序列中的一些数字, 于是我们增加start变成2,此时得到的序列是{2, 3, 4}, 序列的和E好是9。我们找到了第一个和为9 的连续序列,把它打印出来。接下来我们再增加big,重复前面的过程,可以找到第二个和为9 的连续序列{4,5}。
164168

@@ -250,4 +254,4 @@ int __tmain( )
250254

251255
return 0;
252256
}
253-
```
257+
```

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ COMMIT_38="剑指Offer--038-数字在排序数组中出现的次数--http://blog
3939
COMMIT_39_1="剑指Offer–039-二叉树的深度--http://blog.csdn.net/gatieme/article/details/51339884"
4040
COMMIT_39_2="剑指Offer–039-平衡二叉树--http://blog.csdn.net/gatieme/article/details/51346422"
4141
COMMIT_40="剑指Offer--040-数组中只出现一次的数字--http://blog.csdn.net/gatieme/article/details/51352156"
42-
42+
COMMIT_41="剑指Offer–041-和为S的连续正数序列--http://blog.csdn.net/gatieme/article/details/51357308"
4343

4444
RETURN_TOP ="<br>**您也可以选择[回到目录-剑指Offer--题集目录索引](http://blog.csdn.net/gatieme/article/details/51916802)**"
4545

4646

4747

48-
GITHUB_COMMIT=$(COMMIT_40)
48+
GITHUB_COMMIT=$(COMMIT_41)
4949

5050

5151
all:github

0 commit comments

Comments
 (0)