-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0394_decode_string.rs
More file actions
56 lines (52 loc) · 1.62 KB
/
s0394_decode_string.rs
File metadata and controls
56 lines (52 loc) · 1.62 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#![allow(unused)]
pub struct Solution {}
impl Solution {
// Time O(N), Space O(N)
// 对称性考虑用栈 此题用 [ 和] 实现对称性,]是弹栈的时机
pub fn decode_string(s: String) -> String {
let mut stack: Vec<(String, usize)> = Vec::new();
let mut cur_num: usize = 0;
let mut cur_string: String = String::new();
for l in s.chars() {
if l == '[' {
stack.push((cur_string, cur_num));
cur_string = String::new();
cur_num = 0;
} else if l == ']' {
let (mut string, nums) = stack.pop().unwrap();
cur_string = cur_string.repeat(nums);
string.push_str(cur_string.as_str());
cur_string = string
} else if l.is_digit(10) {
cur_num = cur_num * 10 + (l.to_digit(10).unwrap() as usize);
} else {
cur_string.push(l)
}
}
cur_string
// stack.into_iter().collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_394() {
assert_eq!(
Solution::decode_string("3[a]2[bc]".to_string()),
"aaabcbc".to_string()
);
assert_eq!(
Solution::decode_string("3[a2[c]]".to_string()),
"accaccacc".to_string()
);
assert_eq!(
Solution::decode_string("2[abc]3[cd]ef".to_string()),
"abcabccdcdcdef".to_string()
);
assert_eq!(
Solution::decode_string("abc3[cd]xyz".to_string()),
"abccdcdcdxyz".to_string()
);
}
}