Skip to content

Commit 0bb9216

Browse files
ChunelFengjunfeng.fj
and
junfeng.fj
authored
更新了[lcof-57.和为s的连续正整数序列]的cpp解法 (doocs#347)
* 增加了[lcof-18.删除链表的节点]和[lcof-40.最小的k个数]的cpp解法,基本双优 * 优化[lcof-40.最小的k个数]格式 * 更新了[lcof-57.和为s的连续正整数序列]的cpp版本解法 Co-authored-by: junfeng.fj <junfeng.fj@alibaba-inc.com>
1 parent acdb137 commit 0bb9216

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

lcof/面试题57 - II. 和为s的连续正数序列/README.md

+49
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,55 @@ var findContinuousSequence = function (target) {
113113
};
114114
```
115115

116+
### **C++**
117+
118+
```cpp
119+
class Solution {
120+
public:
121+
vector<int> build(int small, int big) {
122+
vector<int> ret;
123+
for (int i = small; i <= big; i++) {
124+
ret.push_back(i);
125+
}
126+
127+
return ret;
128+
}
129+
130+
vector<vector<int>> findContinuousSequence(int target) {
131+
vector<vector<int>> ret;
132+
int small = 1;
133+
int big = 2;
134+
int mid = (target + 1) / 2;
135+
int curSum = small + big;
136+
137+
if (target < 3) {
138+
ret;
139+
}
140+
141+
while(small < mid) {
142+
if (curSum == target) {
143+
ret.push_back(build(small, big));
144+
}
145+
146+
while (curSum > target && small < mid) {
147+
// 一直减去,减去到比target小停止
148+
curSum -= small;
149+
small++;
150+
151+
if (curSum == target && small < mid) {
152+
ret.push_back(build(small, big));
153+
}
154+
}
155+
156+
big++;
157+
curSum += big;
158+
}
159+
160+
return ret;
161+
}
162+
};
163+
```
164+
116165
### **...**
117166
118167
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Solution {
2+
public:
3+
vector<int> build(int small, int big) {
4+
vector<int> ret;
5+
for (int i = small; i <= big; i++) {
6+
ret.push_back(i);
7+
}
8+
9+
return ret;
10+
}
11+
12+
vector<vector<int>> findContinuousSequence(int target) {
13+
vector<vector<int>> ret;
14+
int small = 1;
15+
int big = 2;
16+
int mid = (target + 1) / 2;
17+
int curSum = small + big;
18+
19+
if (target < 3) {
20+
ret;
21+
}
22+
23+
while(small < mid) {
24+
if (curSum == target) {
25+
ret.push_back(build(small, big));
26+
}
27+
28+
while (curSum > target && small < mid) {
29+
// 一直减去,减去到比target小停止
30+
curSum -= small;
31+
small++;
32+
33+
if (curSum == target && small < mid) {
34+
ret.push_back(build(small, big));
35+
}
36+
}
37+
38+
big++;
39+
curSum += big;
40+
}
41+
42+
return ret;
43+
}
44+
};

0 commit comments

Comments
 (0)