-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path15-mini-parser.rs
29 lines (28 loc) · 935 Bytes
/
15-mini-parser.rs
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
// https://leetcode-cn.com/problems/mini-parser/
// #[derive(Debug, PartialEq, Eq)]
// pub enum NestedInteger {
// Int(i32),
// List(Vec<NestedInteger>)
// }
use crate::NestedInteger::{Int, List};
impl Solution {
pub fn deserialize(s: String) -> NestedInteger {
if s.is_empty() {
return List(vec![]);
}
if !s.starts_with("[") {
return Int(s.parse().unwrap());
}
if s.len() <= 2 { return List(vec![]); }
let mut res = (vec![]);
let (mut start, mut cnt) = (1, 0);
let bytes = s.as_bytes();
for i in 1..s.len() {
if cnt == 0 && (bytes[i] == b',' || i == s.len() - 1) {
res.push(Self::deserialize((&s[start..i]).to_owned()));
start=i+1;
} else if bytes[i] == b'[' { cnt += 1 } else if bytes[i] == b']' { cnt -= 1 }
}
List(res)
}
}