394. 字符串解码
This commit is contained in:
+25
-12
@@ -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;
|
||||||
|
}
|
||||||
|
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;
|
local
|
||||||
while fast.is_some() {
|
|
||||||
slow = &slow.as_ref()?.next;
|
|
||||||
fast = &fast.as_ref()?.next;
|
|
||||||
}
|
|
||||||
slow.clone()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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())
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user