Skip to content

Commit 21ca8e2

Browse files
committedJan 24, 2023
Create: 0075-sort-colors.rs
1 parent d88e61e commit 21ca8e2

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
 

‎rust/0075-sort-colors.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
impl Solution {
2+
pub fn sort_colors(nums: &mut Vec<i32>) {
3+
for i in 1..nums.len() {
4+
let mut j = i;
5+
while j > 0 && nums[j] < nums[j - 1] {
6+
nums.swap(j, j - 1);
7+
j = j - 1;
8+
}
9+
}
10+
}
11+
}
12+
13+
/*
14+
* Solution using counting sort
15+
*/
16+
impl Solution {
17+
pub fn sort_colors(nums: &mut Vec<i32>) {
18+
let mut count = [0; 3];
19+
for num in nums.iter() {
20+
count[*num as usize] += 1;
21+
}
22+
let mut i = 0;
23+
for (num, c) in count.iter().enumerate() {
24+
for _ in 0..*c {
25+
nums[i] = num as i32;
26+
i += 1;
27+
}
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)
Please sign in to comment.