Skip to content

Commit a54e097

Browse files
committed
[feat ]: leetcode 402
1 parent ce30bf4 commit a54e097

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

leetcode/0402_Remove-K-Digits.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
impl Solution {
2+
pub fn remove_kdigits(num: String, k: i32) -> String {
3+
use std::collections::VecDeque;
4+
let mut k = k;
5+
let mut mstack:Vec<char> = Vec::new();
6+
// Monolitic stack
7+
for n in num.chars() {
8+
// insert to stack
9+
while !mstack.is_empty() && k > 0 && *mstack.last().unwrap() > n {
10+
mstack.pop();
11+
k -= 1;
12+
}
13+
mstack.push(n);
14+
}
15+
16+
for _ in 0..k {
17+
mstack.pop();
18+
}
19+
20+
let ret = mstack.into_iter().collect::<String>().trim_start_matches('0').to_string();
21+
22+
if ret.len() == 0 {
23+
"0".to_string()
24+
} else {
25+
ret
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)