-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathincreasing-decreasing-string.rs
40 lines (35 loc) · 1.05 KB
/
increasing-decreasing-string.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#![allow(dead_code, unused, unused_variables, non_snake_case)]
fn main() {}
struct Solution;
impl Solution {
pub fn sort_string(mut s: String) -> String {
let mut count = [0; 26];
for &i in s.as_bytes() {
count[(i - b'a') as usize] += 1;
}
let mut slice = unsafe { s.as_bytes_mut() };
let mut start = 0;
let mut flag = false; // true为选最大,false为选最小
while start < slice.len() {
if !flag {
for i in 0..26 {
if count[i] > 0 {
slice[start] = i as u8 + b'a';
count[i] -= 1;
start += 1;
}
}
} else {
for i in (0..26).rev() {
if count[i] > 0 {
slice[start] = i as u8 + b'a';
count[i] -= 1;
start += 1;
}
}
}
flag = !flag;
}
s
}
}