Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1296 from zim0369/two-sum-two
Browse files Browse the repository at this point in the history
Update 167-Two-Sum-II.rs
  • Loading branch information
Ahmad-A0 authored Oct 21, 2022
2 parents 0d1c388 + 06016c7 commit ad0c70e
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions rust/167-Two-Sum-II.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
use std::cmp::Ordering::{Equal, Greater, Less};

impl Solution {
pub fn two_sum(numbers: Vec<i32>, target: i32) -> Vec<i32> {
let (mut l, mut r) = (0, numbers.len() - 1);

while l < r{
let sum = numbers[l] + numbers[r];

if sum > target{
r-=1;
}else if sum < target{
l+=1;
}else{
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],
}
}

unreachable!()
unreachable!("Test did not follow the constraints!")
}
}
}

0 comments on commit ad0c70e

Please sign in to comment.