Skip to content

Commit 80b6ff6

Browse files
committed
6-27-2020
1 parent 48f4e17 commit 80b6ff6

10 files changed

+275
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
struct Solution;
2+
use crate::util::*;
3+
4+
impl Solution {
5+
pub fn delete_duplicates(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
6+
let mut p = head.as_mut();
7+
while let Some(n) = p {
8+
while let Some(m) = n.next.as_mut() {
9+
if n.val == m.val {
10+
n.next = m.next.take();
11+
} else {
12+
break;
13+
}
14+
}
15+
p = n.next.as_mut();
16+
}
17+
head
18+
}
19+
}
20+
21+
#[test]
22+
fn test() {
23+
let p = ListNode::list(vec![1, 1, 2, 3, 3]);
24+
let q = ListNode::list(vec![1, 2, 3]);
25+
assert_eq!(Solution::delete_duplicates(p), q);
26+
}

src/_0169_majority_element.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn majority_element(nums: Vec<i32>) -> i32 {
5+
let mut current: i32 = nums[0];
6+
let mut occur: i32 = 0;
7+
8+
for i in 0..nums.len() {
9+
if nums[i] == current {
10+
occur += 1;
11+
} else {
12+
occur -= 1;
13+
14+
if occur == 0 {
15+
current = nums[i];
16+
occur = 1;
17+
}
18+
}
19+
}
20+
current
21+
}
22+
}
23+
24+
#[test]
25+
fn test() {
26+
assert_eq!(Solution::majority_element(vec![3, 2, 3]), 3);
27+
assert_eq!(Solution::majority_element(vec![2, 2, 1, 1, 1, 2, 2]), 2);
28+
}

src/_0189_rotate_array.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
fn rotate(nums: &mut Vec<i32>, k: i32) {
5+
let k = k as usize % nums.len();
6+
nums[..].reverse();
7+
nums[0..k].reverse();
8+
nums[k..].reverse();
9+
}
10+
}
11+
12+
#[test]
13+
fn test() {
14+
let mut nums = vec![1, 2, 3, 4, 5, 6, 7];
15+
Solution::rotate(&mut nums, 3);
16+
assert_eq!(nums, vec![5, 6, 7, 1, 2, 3, 4]);
17+
}

src/_0205_isomorphic_strings.rs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn is_isomorphic(s: String, t: String) -> bool {
5+
let s = s.as_bytes();
6+
let t = t.as_bytes();
7+
8+
let mut s_to_t = vec![None; 256];
9+
let mut t_seen = vec![false; 256];
10+
11+
for i in 0..s.len() {
12+
if s_to_t[s[i] as usize].is_none() {
13+
if t_seen[t[i] as usize] {
14+
return false;
15+
} else {
16+
t_seen[t[i] as usize] = true;
17+
}
18+
s_to_t[s[i] as usize] = Some(t[i]);
19+
} else if s_to_t[s[i] as usize] != Some(t[i]) {
20+
return false;
21+
}
22+
}
23+
true
24+
}
25+
}
26+
27+
#[test]
28+
fn test() {
29+
assert_eq!(
30+
Solution::is_isomorphic("egg".to_string(), "add".to_string()),
31+
true
32+
);
33+
assert_eq!(
34+
Solution::is_isomorphic("foo".to_string(), "bar".to_string()),
35+
false
36+
);
37+
assert_eq!(
38+
Solution::is_isomorphic("paper".to_string(), "title".to_string()),
39+
true
40+
);
41+
}

src/_0243_shortest_word_distance.rs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn shortest_distance(words: Vec<String>, word1: String, word2: String) -> i32 {
5+
let mut pos_1: i32 = words.len() as i32;
6+
let mut pos_2: i32 = words.len() as i32;
7+
let mut diff: i32 = words.len() as i32;
8+
9+
for (i, word) in words.iter().enumerate() {
10+
if word.eq(&word1) {
11+
pos_1 = i as i32;
12+
diff = (pos_2 - pos_1).abs().min(diff);
13+
} else if word.eq(&word2) {
14+
pos_2 = i as i32;
15+
diff = (pos_2 - pos_1).abs().min(diff);
16+
}
17+
}
18+
diff
19+
}
20+
}
21+
22+
#[test]
23+
fn test() {
24+
let words: Vec<String> = vec_string!["practice", "makes", "perfect", "coding", "makes"];
25+
let word1 = "coding".to_string();
26+
let word2 = "practice".to_string();
27+
assert_eq!(Solution::shortest_distance(words, word1, word2), 3);
28+
let words: Vec<String> = vec_string!["practice", "makes", "perfect", "coding", "makes"];
29+
let word1 = "makes".to_string();
30+
let word2 = "coding".to_string();
31+
assert_eq!(Solution::shortest_distance(words, word1, word2), 1);
32+
}

src/_0256_paint_house.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn min_cost(mut costs: Vec<Vec<i32>>) -> i32 {
5+
if costs.len() == 0 {
6+
return 0;
7+
}
8+
9+
for i in 1..costs.len() {
10+
costs[i][0] += std::cmp::min(costs[i - 1][1], costs[i - 1][2]);
11+
costs[i][1] += std::cmp::min(costs[i - 1][0], costs[i - 1][2]);
12+
costs[i][2] += std::cmp::min(costs[i - 1][0], costs[i - 1][1]);
13+
}
14+
15+
*costs[costs.len() - 1].iter().min().unwrap()
16+
}
17+
}
18+
19+
#[test]
20+
fn test() {
21+
assert_eq!(
22+
Solution::min_cost(vec![vec![17, 2, 17], vec![16, 16, 5], vec![14, 3, 19]]),
23+
10
24+
);
25+
}

src/_0283_move_zeroes.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn move_zeroes(nums: &mut Vec<i32>) {
5+
let mut j = 0;
6+
for i in 0..nums.len() {
7+
let x = nums[i];
8+
if x != 0 {
9+
nums[i] = 0;
10+
nums[j] = x;
11+
j += 1;
12+
}
13+
}
14+
}
15+
}
16+
17+
#[test]
18+
fn test() {
19+
let mut nums = vec![0, 1, 0, 3, 12];
20+
Solution::move_zeroes(&mut nums);
21+
assert_eq!(nums, vec![1, 3, 12, 0, 0]);
22+
}

src/_0509_fibonacci_number.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn fib(n: i32) -> i32 {
5+
let mut num1: i32 = 0;
6+
let mut num2: i32 = 1;
7+
for _ in 0..n {
8+
num1 += num2;
9+
std::mem::swap(&mut num1, &mut num2);
10+
}
11+
num1
12+
}
13+
}
14+
15+
#[test]
16+
fn test() {
17+
assert_eq!(Solution::fib(2), 1);
18+
assert_eq!(Solution::fib(3), 2);
19+
assert_eq!(Solution::fib(4), 3);
20+
}
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn sorted_squares(a: Vec<i32>) -> Vec<i32> {
5+
let mut left: usize = 0;
6+
let mut right: usize = a.len() - 1;
7+
8+
let mut result: Vec<i32> = vec![0; a.len()];
9+
let mut i: usize = a.len() - 1;
10+
11+
let mut left_result: i32;
12+
let mut right_result: i32;
13+
14+
while left <= right {
15+
left_result = a[left] * a[left];
16+
right_result = a[right] * a[right];
17+
18+
if left_result >= right_result {
19+
result[i] = left_result;
20+
left += 1;
21+
} else {
22+
result[i] = right_result;
23+
right -= 1;
24+
}
25+
if i > 0 {
26+
i -= 1;
27+
}
28+
}
29+
result
30+
}
31+
}
32+
33+
#[test]
34+
fn test() {
35+
assert_eq!(
36+
Solution::sorted_squares(vec![-4, -1, 0, 3, 10]),
37+
vec![0, 1, 9, 16, 100]
38+
);
39+
assert_eq!(
40+
Solution::sorted_squares(vec![-7, -3, 2, 3, 11]),
41+
vec![4, 9, 9, 49, 121]
42+
);
43+
assert_eq!(Solution::sorted_squares(vec![1]), vec![1]);
44+
}

src/lib.rs

+20
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,36 @@ mod _0067_add_binary;
3030
//
3131
mod _0070_climbing_stairs;
3232
//
33+
mod _0083_remove_duplicates_from_sorted_list;
34+
//
3335
mod _0088_merge_sorted_array;
3436
//
3537
mod _0121_best_time_to_buy_and_sell_stock;
3638
//
3739
mod _0136_single_number;
3840
//
41+
mod _0169_majority_element;
42+
//
43+
mod _0189_rotate_array;
44+
//
3945
mod _0202_happy_number;
4046
//
4147
mod _0204_count_primes;
4248
//
49+
mod _0205_isomorphic_strings;
50+
//
51+
mod _0206_reverse_linked_list;
52+
//
4353
mod _0219_contains_duplicate_ii;
4454
//
55+
mod _0243_shortest_word_distance;
56+
//
57+
mod _0256_paint_house;
58+
//
4559
mod _0266_palindrome_permutation;
4660
//
61+
mod _0283_move_zeroes;
62+
//
4763
mod _0383_ransom_note;
4864
//
4965
mod _0412_fizz_buzz;
@@ -52,6 +68,8 @@ mod _0415_add_strings;
5268
//
5369
mod _0448_find_all_numbers_disappeared_in_an_array;
5470
//
71+
mod _0509_fibonacci_number;
72+
//
5573
mod _0557_reverse_words_in_a_string_iii;
5674
//
5775
mod _0605_can_place_flowers;
@@ -66,6 +84,8 @@ mod _0844_backspace_string_compare;
6684
//
6785
mod _0953_verifying_an_alien_dictionary;
6886
//
87+
mod _0977_squares_of_a_sorted_array;
88+
//
6989
mod _1281_subtract_the_product_and_sum_of_digits_of_an_integer;
7090
//
7191
mod _1287_element_appearing_more_than_25_in_sorted_array;

0 commit comments

Comments
 (0)