From 7b7fdf87c3f21fa113db8260b248962a530fa40b Mon Sep 17 00:00:00 2001 From: li-chx Date: Fri, 14 Aug 2026 20:16:06 +0800 Subject: [PATCH] =?UTF-8?q?394.=20=E5=AD=97=E7=AC=A6=E4=B8=B2=E8=A7=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index f1b8450..972a2b6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,23 +6,36 @@ mod tree; struct Solution {} impl Solution { - pub fn training_plan(head: Option>, cnt: i32) -> Option> { - let mut fast = &head; - - for i in 0..cnt { - fast = &fast.as_ref()?.next; + pub fn decode_string(s: String) -> String { + let mut st = vec![]; + let mut local = "".to_string(); + let mut cnt = 0; + // 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; + } + if c == '[' { + st.push((local,cnt)); + cnt = 0; + local = "".to_string(); + continue; + } + if c == ']' { + let (mut str, cnt) = st.pop().unwrap(); + str += &local.repeat( cnt); + local = str; + continue; + } + local.push(c); } - let mut slow = &head; - while fast.is_some() { - slow = &slow.as_ref()?.next; - fast = &fast.as_ref()?.next; - } - slow.clone() + local } } fn main() { println!( "{:?}", - Solution::training_plan(make_list(vec![2, 4, 7, 8]), 2) + Solution::decode_string("3[a2[c]]".to_string()) ); }