Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created August 2, 2021 14:06
Show Gist options
  • Select an option

  • Save rust-play/936e2c105386bd0e0070c1b8df104937 to your computer and use it in GitHub Desktop.

Select an option

Save rust-play/936e2c105386bd0e0070c1b8df104937 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
struct Solution;
impl Solution {
// [2, 7, 11, 15] t = 9
// [3, 4, 2] t = 6
fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
let mut j = 0;
for (i, &num) in nums.iter().enumerate() {
if i > 0 {
if nums[j] + num == target {
return vec![j as i32, i as i32]
}
j = i;
}
}
vec![]
}
}
#[test]
fn test() {
let expected = vec![0, 1];
let nums = vec![2, 7, 11, 15];
let target = 9;
let actual = Solution::two_sum(nums, target);
assert_eq!(expected, actual);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment