Skip to content

Commit 06016c7

Browse files
author
zim0369
committed
Change 167-Two-Sum-II.rs
1 parent 939af48 commit 06016c7

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

rust/167-Two-Sum-II.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1+
use std::cmp::Ordering::{Equal, Greater, Less};
2+
13
impl Solution {
24
pub fn two_sum(numbers: Vec<i32>, target: i32) -> Vec<i32> {
35
let (mut l, mut r) = (0, numbers.len() - 1);
4-
5-
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];
6+
while l < r {
7+
match (numbers[l] + numbers[r]).cmp(&target) {
8+
Less => l += 1,
9+
Greater => r -= 1,
10+
Equal => return vec![l as i32 + 1, r as i32 + 1],
1411
}
1512
}
16-
17-
unreachable!()
13+
unreachable!("Test did not follow the constraints!")
1814
}
19-
}
15+
}

0 commit comments

Comments
 (0)