Skip to content

Commit 591080f

Browse files
committed
missed a couple files
1 parent a152b6f commit 591080f

File tree

2 files changed

+221
-0
lines changed

2 files changed

+221
-0
lines changed

src/solution/s0001_two_sum.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
use std::convert::{TryFrom, TryInto};
2+
3+
/**
4+
* [1] Two Sum
5+
*
6+
* Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
7+
* You may assume that each input would have exactly one solution, and you may not use the same element twice.
8+
* You can return the answer in any order.
9+
*
10+
* <strong class="example">Example 1:
11+
*
12+
* Input: nums = [2,7,11,15], target = 9
13+
* Output: [0,1]
14+
* Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
15+
*
16+
* <strong class="example">Example 2:
17+
*
18+
* Input: nums = [3,2,4], target = 6
19+
* Output: [1,2]
20+
*
21+
* <strong class="example">Example 3:
22+
*
23+
* Input: nums = [3,3], target = 6
24+
* Output: [0,1]
25+
*
26+
*
27+
* Constraints:
28+
*
29+
* 2 <= nums.length <= 10^4
30+
* -10^9 <= nums[i] <= 10^9
31+
* -10^9 <= target <= 10^9
32+
* Only one valid answer exists.
33+
*
34+
*
35+
* Follow-up: Can you come up with an algorithm that is less than O(n^2)<font face="monospace"> </font>time complexity?
36+
*/
37+
pub struct Solution {}
38+
39+
// problem: https://leetcode.com/problems/two-sum/
40+
// discuss: https://leetcode.com/problems/two-sum/discuss/?currentPage=1&orderBy=most_votes&query=
41+
42+
// submission codes start here
43+
44+
impl Solution {
45+
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
46+
for i in 0..nums.len() {
47+
let n = nums[i];
48+
let potential_match = nums.iter().position(|&x| x == (target - n));
49+
match potential_match {
50+
Some (i2) if i != i2 => return vec![i.try_into().unwrap(),i2.try_into().unwrap()],
51+
_ => continue,
52+
};
53+
}
54+
panic!("Oh no, no solution")
55+
}
56+
}
57+
58+
// submission codes end
59+
60+
#[cfg(test)]
61+
mod tests {
62+
use super::*;
63+
64+
#[test]
65+
fn test_1() {
66+
let input_list = vec![2,7,11,15];
67+
let target = 9;
68+
69+
let res = Solution::two_sum(input_list, target);
70+
71+
assert_eq!(vec![0,1], res);
72+
}
73+
74+
#[test]
75+
fn test_2() {
76+
let input_list = vec![3,2,4];
77+
let target = 6;
78+
79+
let res = Solution::two_sum(input_list, target);
80+
81+
assert_eq!(vec![1,2], res);
82+
}
83+
84+
#[test]
85+
fn test_3() {
86+
let input_list = vec![3,3];
87+
let target = 6;
88+
89+
let res = Solution::two_sum(input_list, target);
90+
91+
assert_eq!(vec![0,1], res);
92+
}
93+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
use std::cmp::{max, min};
2+
use std::convert::TryInto;
3+
4+
/**
5+
* [167] Two Sum II - Input Array Is Sorted
6+
*
7+
* Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 < numbers.length.
8+
* Return the indices of the two numbers, index1 and index2, added by one as an integer array [index1, index2] of length 2.
9+
* The tests are generated such that there is exactly one solution. You may not use the same element twice.
10+
* Your solution must use only constant extra space.
11+
*
12+
* <strong class="example">Example 1:
13+
*
14+
* Input: numbers = [<u>2</u>,<u>7</u>,11,15], target = 9
15+
* Output: [1,2]
16+
* Explanation: The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 = 2. We return [1, 2].
17+
*
18+
* <strong class="example">Example 2:
19+
*
20+
* Input: numbers = [<u>2</u>,3,<u>4</u>], target = 6
21+
* Output: [1,3]
22+
* Explanation: The sum of 2 and 4 is 6. Therefore index1 = 1, index2 = 3. We return [1, 3].
23+
*
24+
* <strong class="example">Example 3:
25+
*
26+
* Input: numbers = [<u>-1</u>,<u>0</u>], target = -1
27+
* Output: [1,2]
28+
* Explanation: The sum of -1 and 0 is -1. Therefore index1 = 1, index2 = 2. We return [1, 2].
29+
*
30+
*
31+
* Constraints:
32+
*
33+
* 2 <= numbers.length <= 3 * 10^4
34+
* -1000 <= numbers[i] <= 1000
35+
* numbers is sorted in non-decreasing order.
36+
* -1000 <= target <= 1000
37+
* The tests are generated such that there is exactly one solution.
38+
*
39+
*/
40+
pub struct Solution {}
41+
42+
// problem: https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/
43+
// discuss: https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/discuss/?currentPage=1&orderBy=most_votes&query=
44+
45+
// submission codes start here
46+
47+
impl Solution {
48+
49+
fn binary_find_index(numbers: &Vec<i32>, target: i32) -> Option<usize> {
50+
let mut mindex = 0;
51+
let mut maxdex = numbers.len() - 1;
52+
while mindex <= maxdex {
53+
let potential_index = (maxdex + mindex) / 2;
54+
if numbers[potential_index] == target {
55+
return Some(potential_index);
56+
} else if numbers[potential_index] < target {
57+
mindex = potential_index + 1;
58+
} else {
59+
maxdex = potential_index - 1;
60+
}
61+
}
62+
return None;
63+
}
64+
65+
pub fn two_sum(numbers: Vec<i32>, target: i32) -> Vec<i32> {
66+
for i in 0..numbers.len() {
67+
let n = numbers[i];
68+
let potential_match = Solution::binary_find_index(&numbers, target - n);
69+
match potential_match {
70+
Some (i2) if i != i2 => {
71+
let res1: i32 = i.try_into().unwrap();
72+
let res2: i32 = i2.try_into().unwrap();
73+
74+
return vec![min(res1,res2) + 1, max(res1,res2) + 1];
75+
},
76+
_ => continue,
77+
};
78+
}
79+
panic!("Oh no, no solution")
80+
}
81+
}
82+
83+
// submission codes end
84+
85+
#[cfg(test)]
86+
mod tests {
87+
use super::*;
88+
89+
#[test]
90+
fn test_1() {
91+
let input_list = vec![2,7,11,15];
92+
let target = 9;
93+
94+
let res = Solution::two_sum(input_list, target);
95+
96+
assert_eq!(vec![1,2], res);
97+
}
98+
99+
#[test]
100+
fn test_2() {
101+
let input_list = vec![2,3,4];
102+
let target = 6;
103+
104+
let res = Solution::two_sum(input_list, target);
105+
106+
assert_eq!(vec![1,3], res);
107+
}
108+
109+
#[test]
110+
fn test_3() {
111+
let input_list = vec![-1,0];
112+
let target = -1;
113+
114+
let res = Solution::two_sum(input_list, target);
115+
116+
assert_eq!(vec![1,2], res);
117+
}
118+
119+
#[test]
120+
fn test_4() {
121+
let input_list = vec![1,2,3,4,4,9,56,90];
122+
let target = 8;
123+
124+
let res = Solution::two_sum(input_list, target);
125+
126+
assert_eq!(vec![4,5], res);
127+
}
128+
}

0 commit comments

Comments
 (0)