Implemented problems 26, 27, 28 and 29
TODO: improve problem 29
This commit is contained in:
parent
e714a84a6d
commit
00dda7fc5c
5 changed files with 157 additions and 0 deletions
|
@ -25,3 +25,7 @@ pub mod p22_generate_parentheses;
|
|||
pub mod p23_merge_k_sorted_lists;
|
||||
pub mod p24_swap_nodes_in_pairs;
|
||||
pub mod p25_reverse_nodes_in_k_group;
|
||||
pub mod p26_remove_duplicates_from_sorted_array;
|
||||
pub mod p27_remove_element;
|
||||
pub mod p28_find_the_index_of_the_first_occurence_in_a_string;
|
||||
pub mod p29_divide_two_integers;
|
||||
|
|
37
src/p26_remove_duplicates_from_sorted_array.rs
Normal file
37
src/p26_remove_duplicates_from_sorted_array.rs
Normal file
|
@ -0,0 +1,37 @@
|
|||
pub struct Solution;
|
||||
impl Solution {
|
||||
pub fn remove_duplicates(nums: &mut Vec<i32>) -> i32 {
|
||||
let mut size = 1;
|
||||
for i in 1..nums.len() {
|
||||
if nums[i - 1] != nums[i] {
|
||||
nums[size] = nums[i];
|
||||
size += 1;
|
||||
}
|
||||
}
|
||||
nums.truncate(size);
|
||||
return size as i32;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::Solution;
|
||||
|
||||
#[test]
|
||||
fn test1() {
|
||||
let mut data = vec![1,1,2];
|
||||
let expected = vec![1,2];
|
||||
let result = Solution::remove_duplicates(&mut data);
|
||||
assert_eq!(result, expected.len() as i32);
|
||||
assert_eq!(data, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test2() {
|
||||
let mut data = vec![0,0,1,1,1,2,2,3,3,4];
|
||||
let expected = vec![0,1,2,3,4];
|
||||
let result = Solution::remove_duplicates(&mut data);
|
||||
assert_eq!(result, expected.len() as i32);
|
||||
assert_eq!(data, expected);
|
||||
}
|
||||
}
|
32
src/p27_remove_element.rs
Normal file
32
src/p27_remove_element.rs
Normal file
|
@ -0,0 +1,32 @@
|
|||
pub struct Solution;
|
||||
impl Solution {
|
||||
pub fn remove_element(nums: &mut Vec<i32>, val: i32) -> i32 {
|
||||
nums.retain(|x| *x != val);
|
||||
return nums.len() as i32;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::Solution;
|
||||
|
||||
#[test]
|
||||
fn test1() {
|
||||
let expected = vec![2,2];
|
||||
let mut data = vec![3,2,2,3];
|
||||
let result = Solution::remove_element(&mut data, 3);
|
||||
data.sort();
|
||||
assert_eq!(data, expected);
|
||||
assert_eq!(result, expected.len() as i32);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test2() {
|
||||
let expected = vec![0,0,1,3,4];
|
||||
let mut data = vec![0,1,2,2,3,0,4,2];
|
||||
let result = Solution::remove_element(&mut data, 2);
|
||||
data.sort();
|
||||
assert_eq!(data, expected);
|
||||
assert_eq!(result, expected.len() as i32);
|
||||
}
|
||||
}
|
21
src/p28_find_the_index_of_the_first_occurence_in_a_string.rs
Normal file
21
src/p28_find_the_index_of_the_first_occurence_in_a_string.rs
Normal file
|
@ -0,0 +1,21 @@
|
|||
pub struct Solution;
|
||||
impl Solution {
|
||||
pub fn str_str(haystack: String, needle: String) -> i32 {
|
||||
return haystack.find(&needle).map(|x| x as i32).unwrap_or(-1);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::Solution;
|
||||
|
||||
#[test]
|
||||
fn test1() {
|
||||
assert_eq!(Solution::str_str("sadbutsad".to_string(), "sad".to_string()), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test2() {
|
||||
assert_eq!(Solution::str_str("leetcode".to_string(), "leeto".to_string()), -1);
|
||||
}
|
||||
}
|
63
src/p29_divide_two_integers.rs
Normal file
63
src/p29_divide_two_integers.rs
Normal file
|
@ -0,0 +1,63 @@
|
|||
pub struct Solution;
|
||||
impl Solution {
|
||||
// terrible solutionE
|
||||
// should use approximation using shifts
|
||||
pub fn divide(mut dividend: i32, mut divisor: i32) -> i32 {
|
||||
if dividend == i32::MAX && divisor == i32::MIN {
|
||||
return 0;
|
||||
}
|
||||
if divisor == 1 {
|
||||
return dividend;
|
||||
}
|
||||
if divisor == -1 {
|
||||
if dividend == i32::MIN {
|
||||
return i32::MAX;
|
||||
}
|
||||
return -dividend;
|
||||
}
|
||||
let mut result = 0;
|
||||
let is_neg = (dividend < 0) ^ (divisor < 0);
|
||||
let mut one = false;
|
||||
dividend = if dividend == i32::MIN {
|
||||
one = true;
|
||||
i32::MAX
|
||||
} else { dividend.abs() };
|
||||
divisor = if divisor == i32::MIN { i32::MAX } else { divisor.abs() };
|
||||
while dividend >= divisor {
|
||||
dividend -= divisor;
|
||||
result += 1;
|
||||
}
|
||||
if one && dividend - divisor == -1 {
|
||||
result += 1;
|
||||
}
|
||||
if is_neg {
|
||||
result = -result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::Solution;
|
||||
|
||||
#[test]
|
||||
fn test1() {
|
||||
assert_eq!(Solution::divide(10, 3), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test2() {
|
||||
assert_eq!(Solution::divide(7, -3), -2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test3() {
|
||||
assert_eq!(Solution::divide(-2147483648, 2), -1073741824);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test4() {
|
||||
assert_eq!(Solution::divide(-2147483648, -1), 2147483647);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue