We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 939af48 commit 06016c7Copy full SHA for 06016c7
rust/167-Two-Sum-II.rs
@@ -1,19 +1,15 @@
1
+use std::cmp::Ordering::{Equal, Greater, Less};
2
+
3
impl Solution {
4
pub fn two_sum(numbers: Vec<i32>, target: i32) -> Vec<i32> {
5
let (mut l, mut r) = (0, numbers.len() - 1);
-
- while l < r{
6
- let sum = numbers[l] + numbers[r];
7
8
- if sum > target{
9
- r-=1;
10
- }else if sum < target{
11
- l+=1;
12
- }else{
13
- return vec![(l + 1) as i32, (r + 1) as i32];
+ while l < r {
+ match (numbers[l] + numbers[r]).cmp(&target) {
+ Less => l += 1,
+ Greater => r -= 1,
+ Equal => return vec![l as i32 + 1, r as i32 + 1],
14
}
15
16
17
- unreachable!()
+ unreachable!("Test did not follow the constraints!")
18
19
-}
+}
0 commit comments