File tree Expand file tree Collapse file tree 2 files changed +93
-0
lines changed
lcof/面试题57 - II. 和为s的连续正数序列 Expand file tree Collapse file tree 2 files changed +93
-0
lines changed Original file line number Diff line number Diff 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 number Diff line number Diff line change 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+ };
You can’t perform that action at this time.
0 commit comments