File tree Expand file tree Collapse file tree 3 files changed +61
-0
lines changed
solution/0100-0199/0167.Two Sum II - Input array is sorted Expand file tree Collapse file tree 3 files changed +61
-0
lines changed Original file line number Diff line number Diff line change @@ -177,6 +177,28 @@ var twoSum = function (numbers, target) {
177
177
};
178
178
```
179
179
180
+ ### ** Rust**
181
+
182
+ ``` rust
183
+ use std :: cmp :: Ordering ;
184
+
185
+ impl Solution {
186
+ pub fn two_sum (numbers : Vec <i32 >, target : i32 ) -> Vec <i32 > {
187
+ let n = numbers . len ();
188
+ let mut l = 0 ;
189
+ let mut r = n - 1 ;
190
+ loop {
191
+ match (numbers [l ] + numbers [r ]). cmp (& target ) {
192
+ Ordering :: Less => l += 1 ,
193
+ Ordering :: Greater => r -= 1 ,
194
+ Ordering :: Equal => break ,
195
+ }
196
+ }
197
+ vec! [l as i32 + 1 , r as i32 + 1 ]
198
+ }
199
+ }
200
+ ```
201
+
180
202
### ** ...**
181
203
182
204
```
Original file line number Diff line number Diff line change @@ -166,6 +166,28 @@ var twoSum = function (numbers, target) {
166
166
};
167
167
```
168
168
169
+ ### ** Rust**
170
+
171
+ ``` rust
172
+ use std :: cmp :: Ordering ;
173
+
174
+ impl Solution {
175
+ pub fn two_sum (numbers : Vec <i32 >, target : i32 ) -> Vec <i32 > {
176
+ let n = numbers . len ();
177
+ let mut l = 0 ;
178
+ let mut r = n - 1 ;
179
+ loop {
180
+ match (numbers [l ] + numbers [r ]). cmp (& target ) {
181
+ Ordering :: Less => l += 1 ,
182
+ Ordering :: Greater => r -= 1 ,
183
+ Ordering :: Equal => break ,
184
+ }
185
+ }
186
+ vec! [l as i32 + 1 , r as i32 + 1 ]
187
+ }
188
+ }
189
+ ```
190
+
169
191
### ** ...**
170
192
171
193
```
Original file line number Diff line number Diff line change
1
+ use std:: cmp:: Ordering ;
2
+
3
+ impl Solution {
4
+ pub fn two_sum ( numbers : Vec < i32 > , target : i32 ) -> Vec < i32 > {
5
+ let n = numbers. len ( ) ;
6
+ let mut l = 0 ;
7
+ let mut r = n - 1 ;
8
+ loop {
9
+ match ( numbers[ l] + numbers[ r] ) . cmp ( & target) {
10
+ Ordering :: Less => l += 1 ,
11
+ Ordering :: Greater => r -= 1 ,
12
+ Ordering :: Equal => break ,
13
+ }
14
+ }
15
+ vec ! [ l as i32 + 1 , r as i32 + 1 ]
16
+ }
17
+ }
You can’t perform that action at this time.
0 commit comments