Skip to content

Commit c5d7f88

Browse files
committed
31, 217, 231, 402
1 parent 4587330 commit c5d7f88

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed

src/_0031_next_permutation.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn next_permutation(nums: &mut Vec<i32>) {
5+
let (mut i, mut switched) = (nums.len() - 1, false);
6+
while i > 0 && nums[i - 1] >= nums[i] {
7+
i -= 1;
8+
}
9+
if i != 0 {
10+
let (pivot, mut smallest_larger_index) = (nums[i - 1], i);
11+
for j in i..nums.len() {
12+
if nums[j] > pivot && nums[j] <= nums[smallest_larger_index] {
13+
smallest_larger_index = j;
14+
}
15+
}
16+
nums.swap(i - 1, smallest_larger_index);
17+
nums[i..].sort()
18+
} else {
19+
nums.sort()
20+
}
21+
}
22+
}
23+
24+
#[test]
25+
fn test() {
26+
assert_eq!(Solution::next_permutation(vec![1, 2, 3]), vec![1, 3, 2]);
27+
assert_eq!(Solution::next_permutation(vec![3, 2, 1]), vec![1, 2, 3]);
28+
assert_eq!(Solution::next_permutation(vec![1, 1, 5]), vec![1, 5, 1]);
29+
}

src/_0217_contains_duplicate.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use std::collections::HashSet;
2+
3+
struct Solution;
4+
5+
impl Solution {
6+
pub fn contains_duplicate(nums: Vec<i32>) -> bool {
7+
let mut counter: HashSet<i32> = HashSet::new();
8+
for num in &nums {
9+
if counter.contains(num) {
10+
return true;
11+
}
12+
counter.insert(*num);
13+
}
14+
false
15+
}
16+
}
17+
18+
#[test]
19+
fn test() {
20+
assert_eq!(Solution::contains_duplicate(vec![1, 2, 3, 1]), true);
21+
assert_eq!(Solution::contains_duplicate(vec![1, 2, 3, 4]), false);
22+
assert_eq!(
23+
Solution::contains_duplicate(vec![1, 1, 1, 3, 3, 4, 3, 2, 4, 2]),
24+
true
25+
);
26+
}

src/_0231_power_of_two.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn is_power_of_two(n: i32) -> bool {
5+
n > 0 && n & (n - 1) == 0
6+
}
7+
}
8+
9+
#[test]
10+
fn test() {
11+
assert_eq!(Solution::is_power_of_two(1), true);
12+
assert_eq!(Solution::is_power_of_two(16), true);
13+
assert_eq!(Solution::is_power_of_two(3), false);
14+
}

src/_0402_remove_k_digits.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn remove_kdigits(num: String, k: i32) -> String {
5+
let mut stack = vec![];
6+
let mut k = k as usize;
7+
for (i, c) in num.chars().enumerate() {
8+
if k > 0 {
9+
while k > 0 {
10+
if let Some(&last) = stack.last() {
11+
if c < last {
12+
stack.pop();
13+
k -= 1;
14+
} else {
15+
break;
16+
}
17+
} else {
18+
break;
19+
}
20+
}
21+
}
22+
stack.push(c);
23+
}
24+
let result = stack
25+
.split_at(stack.len() - k as usize)
26+
.0
27+
.into_iter()
28+
.collect::<String>()
29+
.trim_start_matches('0')
30+
.to_string();
31+
if result == "" {
32+
"0".to_string()
33+
} else {
34+
result
35+
}
36+
}
37+
}
38+
39+
#[test]
40+
fn test() {
41+
assert_eq!(Solution::remove_kdigits("1432219".to_string(), 3), "1219");
42+
assert_eq!(Solution::remove_kdigits("10200".to_string(), 1), "200");
43+
assert_eq!(Solution::remove_kdigits("10".to_string(), 2), "0");
44+
}

src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ mod _0021_merge_two_sorted_lists;
3838
//
3939
mod _0022_generate_parentheses;
4040
//
41+
mod _0031_next_permutation;
42+
//
4143
mod _0034_find_first_and_last_position_of_element_in_sorted_array;
4244
//
4345
mod _0036_valid_sudoku;
@@ -136,10 +138,14 @@ mod _0213_house_robber_ii;
136138
//
137139
mod _0215_kth_largest_element_in_an_array;
138140
//
141+
mod _0217_contains_duplicate;
142+
//
139143
mod _0219_contains_duplicate_ii;
140144
//
141145
mod _0221_maximal_square;
142146
//
147+
mod _0231_power_of_two;
148+
//
143149
mod _0234_palindrome_linked_list;
144150
//
145151
mod _0238_product_of_array_except_self;
@@ -190,6 +196,8 @@ mod _0387_first_unique_character_in_a_string;
190196
//
191197
mod _0394_decode_string;
192198
//
199+
mod _0402_remove_k_digits;
200+
//
193201
mod _0404_sum_of_left_leaves;
194202
//
195203
mod _0412_fizz_buzz;

0 commit comments

Comments
 (0)