rustgym/leetcode/
_167_two_sum_2.rs

1struct Solution;
2
3impl Solution {
4    fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
5        let mut left: usize = 0;
6        let mut right: usize = nums.len() - 1;
7        while left < right {
8            if nums[left] + nums[right] == target {
9                return vec![(left + 1) as i32, (right + 1) as i32];
10            }
11            if nums[left] + nums[right] < target {
12                left += 1;
13                continue;
14            }
15            if nums[left] + nums[right] > target {
16                right -= 1;
17                continue;
18            }
19        }
20        vec![0, 0]
21    }
22}
23
24#[test]
25fn test() {
26    assert_eq!(Solution::two_sum(vec![2, 7, 11, 15], 9), vec![1, 2]);
27}