Skip to content

Commit 43232bd

Browse files
committed
7-25-2020
1 parent 46949e3 commit 43232bd

6 files changed

+183
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
struct Solution;
2+
impl Solution {
3+
pub fn first_uniq_char(s: String) -> i32 {
4+
let mut h_char_index: Vec<i32> = vec![-1; 26];
5+
for (i, v) in s.chars().enumerate() {
6+
if h_char_index[v as usize - 'a' as usize] >= 0 {
7+
h_char_index[v as usize - 'a' as usize] = -2;
8+
} else if h_char_index[v as usize - 'a' as usize] == -1 {
9+
h_char_index[v as usize - 'a' as usize] = i as i32;
10+
}
11+
}
12+
let mut result: i32 = s.len() as i32;
13+
for v in h_char_index {
14+
if v >= 0 {
15+
result = result.min(v);
16+
}
17+
}
18+
if result == s.len() as i32 {
19+
return -1;
20+
} else {
21+
return result;
22+
}
23+
}
24+
}
25+
26+
#[test]
27+
fn test() {
28+
assert_eq!(Solution::first_uniq_char("leetcode".to_string()), 0);
29+
assert_eq!(Solution::first_uniq_char("loveleetcode".to_string()), 2);
30+
}

src/_0678_valid_parenthesis_string.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn check_valid_string(s: String) -> bool {
5+
let mut h_low: i32 = 0;
6+
let mut h_high: i32 = 0;
7+
for v in s.chars() {
8+
if v == '(' {
9+
h_low += 1;
10+
} else {
11+
h_low -= 1;
12+
}
13+
14+
if v != ')' {
15+
h_high += 1;
16+
} else {
17+
h_high -= 1;
18+
}
19+
20+
if h_high < 0 {
21+
return false;
22+
}
23+
h_low = h_low.max(0);
24+
}
25+
h_low == 0
26+
}
27+
}
28+
29+
#[test]
30+
fn test() {
31+
assert_eq!(Solution::check_valid_string("()".to_string()), true);
32+
assert_eq!(Solution::check_valid_string("(*)".to_string()), true);
33+
assert_eq!(Solution::check_valid_string("(*))".to_string()), true);
34+
}

src/_0922_sort_array_by_parity_ii.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn sort_array_by_parity_ii(mut a: Vec<i32>) -> Vec<i32> {
5+
let mut h_even_index: usize = 0;
6+
let mut h_odd_index: usize = 1;
7+
let n: usize = a.len();
8+
while h_even_index < n && h_odd_index < n {
9+
while h_even_index < n && a[h_even_index] % 2 == 0 {
10+
h_even_index += 2;
11+
}
12+
while h_odd_index < n && a[h_odd_index] % 2 == 1 {
13+
h_odd_index += 2;
14+
}
15+
16+
if h_even_index < n && h_odd_index < n {
17+
a.swap(h_even_index, h_odd_index);
18+
}
19+
}
20+
a
21+
}
22+
}
23+
24+
#[test]
25+
fn test() {
26+
assert_eq!(
27+
Solution::sort_array_by_parity_ii(vec![4, 2, 5, 7]),
28+
vec![4, 5, 2, 7]
29+
);
30+
}
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn remove_outer_parentheses(s: String) -> String {
5+
let mut result: String = "".to_string();
6+
let mut count: i32 = 0;
7+
for v in s.chars() {
8+
match v {
9+
'(' => {
10+
count += 1;
11+
if count > 1 {
12+
result.push(v);
13+
}
14+
}
15+
')' => {
16+
count -= 1;
17+
if count != 0 {
18+
result.push(v);
19+
}
20+
}
21+
_ => {}
22+
}
23+
}
24+
result
25+
}
26+
}
27+
28+
#[test]
29+
fn test() {
30+
assert_eq!(
31+
Solution::remove_outer_parentheses("(()())(())".to_string(),),
32+
"()()()".to_string()
33+
);
34+
assert_eq!(
35+
Solution::remove_outer_parentheses("(()())(())(()(()))".to_string(),),
36+
"()()()()(())".to_string()
37+
);
38+
assert_eq!(
39+
Solution::remove_outer_parentheses("()()".to_string(),),
40+
"".to_string()
41+
);
42+
}

src/_1528_shuffle_string.rs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn restore_string(s: String, indices: Vec<i32>) -> String {
5+
let s_char: Vec<char> = s.chars().collect();
6+
let mut result: Vec<char> = vec!['a'; s.len()];
7+
8+
for (i, v) in indices.into_iter().enumerate() {
9+
result[v as usize] = s_char[i];
10+
}
11+
result.into_iter().collect()
12+
}
13+
}
14+
15+
#[test]
16+
fn test() {
17+
assert_eq!(
18+
Solution::restore_string("abc".to_string(), vec![0, 1, 2]),
19+
"abc".to_string()
20+
);
21+
assert_eq!(
22+
Solution::restore_string("codeleet".to_string(), vec![4, 5, 6, 7, 0, 2, 1, 3]),
23+
"leetcode".to_string()
24+
);
25+
assert_eq!(
26+
Solution::restore_string("aiohn".to_string(), vec![3, 1, 4, 2, 0]),
27+
"nihao".to_string()
28+
);
29+
assert_eq!(
30+
Solution::restore_string("aaiougrt".to_string(), vec![4, 0, 2, 6, 7, 3, 1, 5]),
31+
"arigatou".to_string()
32+
);
33+
assert_eq!(
34+
Solution::restore_string("art".to_string(), vec![1, 0, 2]),
35+
"rat".to_string()
36+
);
37+
}

src/lib.rs

+10
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ mod _0371_sum_of_two_integers;
100100
//
101101
mod _0383_ransom_note;
102102
//
103+
mod _0387_first_unique_character_in_a_string;
104+
//
103105
mod _0412_fizz_buzz;
104106
//
105107
mod _0415_add_strings;
@@ -124,6 +126,8 @@ mod _0561_array_partition_i;
124126
//
125127
mod _0605_can_place_flowers;
126128
//
129+
mod _0678_valid_parenthesis_string;
130+
//
127131
mod _0680_valid_palindrome_ii;
128132
//
129133
mod _0724_find_pivot_index;
@@ -142,12 +146,16 @@ mod _0844_backspace_string_compare;
142146
//
143147
mod _0905_sort_array_by_parity;
144148
//
149+
mod _0922_sort_array_by_parity_ii;
150+
//
145151
mod _0953_verifying_an_alien_dictionary;
146152
//
147153
mod _0977_squares_of_a_sorted_array;
148154
//
149155
mod _1013_partition_array_into_three_parts_with_equal_sum;
150156
//
157+
mod _1021_remove_outermost_parentheses;
158+
//
151159
mod _1029_two_city_scheduling;
152160
//
153161
mod _1249_minimum_remove_to_make_valid_parentheses;
@@ -163,3 +171,5 @@ mod _1299_replace_elements_with_greatest_element_on_right_side;
163171
mod _1304_find_n_unique_integers_sum_up_to_zero;
164172
//
165173
mod _1486_xor_operation_in_an_array;
174+
//
175+
mod _1528_shuffle_string;

0 commit comments

Comments
 (0)