Skip to content

Commit b22c506

Browse files
committed
7-23-2020
1 parent 84d57bd commit b22c506

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

src/_0014_longest_common_prefix.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn longest_common_prefix(strs: Vec<String>) -> String {
5+
if strs.is_empty() {
6+
return "".to_string();
7+
}
8+
9+
let h_length_shortest: usize = strs.iter().map(|s| s.len()).min().unwrap_or(0);
10+
let mut index: usize = 0;
11+
'outer: for _i in 0..h_length_shortest {
12+
let h_character: u8 = strs[0].as_bytes()[index];
13+
for j in 1..strs.len() {
14+
if strs[j].as_bytes()[index] != h_character {
15+
break 'outer;
16+
}
17+
}
18+
index += 1;
19+
}
20+
strs[0][0..index].to_string()
21+
}
22+
}
23+
24+
#[test]
25+
fn test() {
26+
assert_eq!(
27+
Solution::longest_common_prefix(vec_string!["flower", "flow", "flight"]),
28+
"fl".to_string()
29+
);
30+
}

src/_0155_min_stack.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use std::cmp::min;
2+
struct MinStack {
3+
stack: Vec<(i32, i32)>,
4+
}
5+
6+
impl MinStack {
7+
/** initialize your data structure here. */
8+
fn new() -> Self {
9+
MinStack { stack: vec![] }
10+
}
11+
fn push(&mut self, x: i32) {
12+
if self.stack.is_empty() {
13+
self.stack.push((x, x));
14+
} else {
15+
self.stack.push((x, min(self.stack.last().unwrap().1, x)));
16+
}
17+
}
18+
fn pop(&mut self) {
19+
self.stack.pop();
20+
}
21+
fn top(&self) -> i32 {
22+
self.stack.last().unwrap().0
23+
}
24+
fn get_min(&self) -> i32 {
25+
self.stack.last().unwrap().1
26+
}
27+
}
28+
29+
#[test]
30+
fn test() {
31+
let mut min_stack = MinStack::new();
32+
min_stack.push(-2);
33+
min_stack.push(0);
34+
min_stack.push(-3);
35+
assert_eq!(min_stack.get_min(), -3);
36+
min_stack.pop();
37+
assert_eq!(min_stack.top(), 0);
38+
assert_eq!(min_stack.get_min(), -2);
39+
}

src/_0443_string_compression.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn compress(chars: &mut Vec<char>) -> i32 {
5+
if chars.is_empty() {
6+
return 0;
7+
}
8+
let mut h_count: i32 = 0;
9+
let mut h_char_current: char = chars[0];
10+
let mut h_index: usize = 0;
11+
let h_length: usize = chars.len();
12+
13+
for j in 0..=h_length {
14+
if j == h_length || chars[j] != h_char_current {
15+
chars[h_index] = h_char_current;
16+
h_index += 1;
17+
if j != h_length {
18+
h_char_current = chars[j];
19+
}
20+
21+
if h_count == 1 {
22+
continue;
23+
} else {
24+
let h_number_chars = h_count.to_string().chars().collect::<Vec<char>>();
25+
for v in h_number_chars.into_iter() {
26+
chars[h_index] = v;
27+
h_index += 1;
28+
}
29+
h_count = 1;
30+
}
31+
} else {
32+
h_count += 1;
33+
}
34+
}
35+
h_index as i32
36+
}
37+
}
38+
39+
#[test]
40+
fn test() {
41+
let mut input: Vec<char> = [
42+
"a", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b",
43+
]
44+
.iter()
45+
.map(|s| s.chars().next().unwrap())
46+
.collect();
47+
assert_eq!(Solution::compress(&mut input), 4);
48+
}

src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ mod _0007_reverse_integer;
1818
//
1919
mod _0009_palindrome_number;
2020
//
21+
mod _0014_longest_common_prefix;
22+
//
2123
mod _0015_3sum;
2224
//
2325
mod _0020_valid_parentheses;
@@ -54,6 +56,8 @@ mod _0127_word_ladder;
5456
//
5557
mod _0136_single_number;
5658
//
59+
mod _0155_min_stack;
60+
//
5761
mod _0169_majority_element;
5862
//
5963
mod _0189_rotate_array;
@@ -98,6 +102,8 @@ mod _0412_fizz_buzz;
98102
//
99103
mod _0415_add_strings;
100104
//
105+
mod _0443_string_compression;
106+
//
101107
mod _0448_find_all_numbers_disappeared_in_an_array;
102108
//
103109
mod _0496_next_greater_element_i;

0 commit comments

Comments
 (0)