File tree Expand file tree Collapse file tree 2 files changed +68
-0
lines changed
lcof2/剑指 Offer II 006. 排序数组中两个数字之和 Expand file tree Collapse file tree 2 files changed +68
-0
lines changed Original file line number Diff line number Diff line change @@ -113,10 +113,47 @@ func twoSum(numbers []int, target int) []int {
113
113
}
114
114
```
115
115
116
+ ### ** C++**
117
+
118
+ ``` cpp
119
+ class Solution
120
+ {
121
+ public:
122
+ vector<int> twoSum( vector<int> & numbers, int target )
123
+ {
124
+ int i = 0;
125
+ int j = numbers.size() - 1;
126
+ vector<int> res;
127
+
128
+ while (i < j)
129
+ {
130
+ int sum = numbers[i] + numbers[j];
131
+ if(sum < target)
132
+ {
133
+ i++;
134
+ }
135
+ else if (sum > target)
136
+ {
137
+ j--;
138
+ }
139
+ else
140
+ {
141
+ res.push_back(i);
142
+ res.push_back(j);
143
+ break;
144
+ }
145
+ }
146
+
147
+ return res;
148
+ }
149
+ };
150
+ ```
151
+
116
152
### ** ...**
117
153
118
154
```
119
155
120
156
```
121
157
122
158
<!-- tabs:end -->
159
+
Original file line number Diff line number Diff line change
1
+ class Solution
2
+ {
3
+ public:
4
+ vector<int > twoSum ( vector<int > & numbers, int target )
5
+ {
6
+ int i = 0 ;
7
+ int j = numbers.size () - 1 ;
8
+ vector<int > res;
9
+
10
+ while (i < j)
11
+ {
12
+ int sum = numbers[i] + numbers[j];
13
+ if (sum < target)
14
+ {
15
+ i++;
16
+ }
17
+ else if (sum > target)
18
+ {
19
+ j--;
20
+ }
21
+ else
22
+ {
23
+ res.push_back (i);
24
+ res.push_back (j);
25
+ break ;
26
+ }
27
+ }
28
+
29
+ return res;
30
+ }
31
+ };
You can’t perform that action at this time.
0 commit comments