File tree 1 file changed +30
-0
lines changed
1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -235,6 +235,8 @@ function twoSum(nums: number[], target: number): number[] {
235
235
236
236
### ** Rust**
237
237
238
+ 双指针:
239
+
238
240
``` rust
239
241
use std :: cmp :: Ordering ;
240
242
@@ -253,6 +255,34 @@ impl Solution {
253
255
}
254
256
```
255
257
258
+ 二分查找:
259
+
260
+ ``` rust
261
+ use std :: cmp :: Ordering ;
262
+
263
+ impl Solution {
264
+ pub fn two_sum (nums : Vec <i32 >, target : i32 ) -> Vec <i32 > {
265
+ let n = nums . len () - 1 ;
266
+ let mut l : usize = 0 ;
267
+ let mut r : usize = n ;
268
+ for i in 0 .. n {
269
+ l = i + 1 ;
270
+ r = n ;
271
+ let target = target - nums [i ];
272
+ while l <= r {
273
+ let mid = l + r >> 1 ;
274
+ match target . cmp (& nums [mid ]) {
275
+ Ordering :: Less => r = mid - 1 ,
276
+ Ordering :: Greater => l = mid + 1 ,
277
+ Ordering :: Equal => return vec! [nums [i ], nums [mid ]],
278
+ }
279
+ }
280
+ }
281
+ vec! [nums [l ], nums [r ]]
282
+ }
283
+ }
284
+ ```
285
+
256
286
### ** ...**
257
287
258
288
```
You can’t perform that action at this time.
0 commit comments