394. 字符串解码

This commit is contained in:
2026-08-14 20:16:06 +08:00
parent d716101d77
commit 7b7fdf87c3
+24 -11
View File
@@ -6,23 +6,36 @@ mod tree;
struct Solution {} struct Solution {}
impl Solution { impl Solution {
pub fn training_plan(head: Option<Box<ListNode>>, cnt: i32) -> Option<Box<ListNode>> { pub fn decode_string(s: String) -> String {
let mut fast = &head; let mut st = vec![];
let mut local = "".to_string();
for i in 0..cnt { let mut cnt = 0;
fast = &fast.as_ref()?.next; // let mut layer = 0;
for c in s.chars() {
if c.is_ascii_digit() {
cnt = cnt * 10 + c.to_digit(10).unwrap() as usize;
continue;
} }
let mut slow = &head; if c == '[' {
while fast.is_some() { st.push((local,cnt));
slow = &slow.as_ref()?.next; cnt = 0;
fast = &fast.as_ref()?.next; local = "".to_string();
continue;
} }
slow.clone() if c == ']' {
let (mut str, cnt) = st.pop().unwrap();
str += &local.repeat( cnt);
local = str;
continue;
}
local.push(c);
}
local
} }
} }
fn main() { fn main() {
println!( println!(
"{:?}", "{:?}",
Solution::training_plan(make_list(vec![2, 4, 7, 8]), 2) Solution::decode_string("3[a2[c]]".to_string())
); );
} }