394. 字符串解码
This commit is contained in:
+25
-12
@@ -6,23 +6,36 @@ mod tree;
|
||||
|
||||
struct Solution {}
|
||||
impl Solution {
|
||||
pub fn training_plan(head: Option<Box<ListNode>>, cnt: i32) -> Option<Box<ListNode>> {
|
||||
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())
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user