Skip to content

Instantly share code, notes, and snippets.

@alex-bezverkhniy
Forked from rust-play/playground.rs
Last active August 2, 2021 14:15
Show Gist options
  • Save alex-bezverkhniy/1b64d69d0a33b8aee5888b9c50e4146b to your computer and use it in GitHub Desktop.
Save alex-bezverkhniy/1b64d69d0a33b8aee5888b9c50e4146b 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 test1() {
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);
}
#[test]
fn test2() {
let expected = vec![1, 2];
let nums = vec![3, 4, 2];
let target = 6;
let actual = Solution::two_sum(nums, target);
assert_eq!(expected, actual);
}
#[test]
fn test3() {
let expected = vec![0, 1];
let nums = vec![3, 4];
let target = 7;
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