|
| 1 | +// https://leetcode.com/problems/mini-parser |
| 2 | +// |
| 3 | +// Given a string s represents the serialization of a nested list, implement a parser to deserialize it and return _the deserialized_ `NestedInteger`. |
| 4 | +// |
| 5 | +// Each element is either an integer or a list whose elements may also be integers or other lists. |
| 6 | +// |
| 7 | +// **Example 1:** |
| 8 | +// |
| 9 | +// ``` |
| 10 | +// **Input:** s = "324" |
| 11 | +// **Output:** 324 |
| 12 | +// **Explanation:** You should return a NestedInteger object which contains a single integer 324. |
| 13 | +// ``` |
| 14 | +// |
| 15 | +// **Example 2:** |
| 16 | +// |
| 17 | +// ``` |
| 18 | +// **Input:** s = "[123,[456,[789]]]" |
| 19 | +// **Output:** [123,[456,[789]]] |
| 20 | +// **Explanation:** Return a NestedInteger object containing a nested list with 2 elements: |
| 21 | +// 1\. An integer containing value 123. |
| 22 | +// 2\. A nested list containing two elements: |
| 23 | +// i. An integer containing value 456. |
| 24 | +// ii. A nested list with one element: |
| 25 | +// a. An integer containing value 789 |
| 26 | +// ``` |
| 27 | +// |
| 28 | +// **Constraints:** |
| 29 | +// |
| 30 | +// * `1 <= s.length <= 5 * 10<sup>4</sup>` |
| 31 | +// * `s` consists of digits, square brackets `"[]"`, negative sign `'-'`, and commas `','`. |
| 32 | +// * `s` is the serialization of valid `NestedInteger`. |
| 33 | +// * All the values in the input are in the range `[-10<sup>6</sup>, 10<sup>6</sup>]`. |
| 34 | + |
| 35 | +#[derive(Debug, PartialEq, Eq)] |
| 36 | +pub enum NestedInteger { |
| 37 | + Int(i32), |
| 38 | + List(Vec<NestedInteger>), |
| 39 | +} |
| 40 | + |
| 41 | +pub fn deserialize(s: String) -> NestedInteger { |
| 42 | + let chars = s.chars().collect::<Vec<char>>(); |
| 43 | + if chars[0] != '[' { |
| 44 | + return NestedInteger::Int(s.parse::<i32>().unwrap()); |
| 45 | + } |
| 46 | + fn is_digit(c: char) -> bool { |
| 47 | + c >= '0' && c <= '9' |
| 48 | + } |
| 49 | + let mut stack = Vec::new(); |
| 50 | + let mut negative = false; |
| 51 | + let mut i = 0; |
| 52 | + while i < chars.len() { |
| 53 | + match chars[i] { |
| 54 | + '[' => { |
| 55 | + stack.push(NestedInteger::List(Vec::new())); |
| 56 | + } |
| 57 | + '-' => { |
| 58 | + negative = true; |
| 59 | + } |
| 60 | + ']' => { |
| 61 | + if stack.len() > 1 { |
| 62 | + let top = stack.pop().unwrap(); |
| 63 | + if let NestedInteger::List(v) = stack.last_mut().unwrap() { |
| 64 | + v.push(top) |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + _ => { |
| 69 | + if is_digit(chars[i]) { |
| 70 | + let mut num = String::new(); |
| 71 | + while is_digit(chars[i]) { |
| 72 | + num.push(chars[i]); |
| 73 | + i += 1; |
| 74 | + } |
| 75 | + let num = if negative { |
| 76 | + num.parse::<i32>().unwrap() * -1 |
| 77 | + } else { |
| 78 | + num.parse::<i32>().unwrap() |
| 79 | + }; |
| 80 | + if let NestedInteger::List(v) = stack.last_mut().unwrap() { |
| 81 | + v.push(NestedInteger::Int(num)) |
| 82 | + } |
| 83 | + // reset negative state |
| 84 | + negative = false; |
| 85 | + continue; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + i += 1; |
| 90 | + } |
| 91 | + return stack.pop().unwrap(); |
| 92 | +} |
| 93 | + |
| 94 | +#[test] |
| 95 | +pub fn t1() { |
| 96 | + assert_eq!(deserialize("324".to_string()), NestedInteger::Int(324)); |
| 97 | + assert_eq!( |
| 98 | + deserialize("[123,[456,[789]]]".to_string()), |
| 99 | + NestedInteger::List(vec![ |
| 100 | + NestedInteger::Int(123), |
| 101 | + NestedInteger::List(vec![ |
| 102 | + NestedInteger::Int(456), |
| 103 | + NestedInteger::List(vec![NestedInteger::Int(789)]) |
| 104 | + ]) |
| 105 | + ]) |
| 106 | + ); |
| 107 | + assert_eq!( |
| 108 | + deserialize("[123,456,[788,799,833],[[]],10,[]]".to_string()), |
| 109 | + NestedInteger::List(vec![ |
| 110 | + NestedInteger::Int(123), |
| 111 | + NestedInteger::Int(456), |
| 112 | + NestedInteger::List(vec![ |
| 113 | + NestedInteger::Int(788), |
| 114 | + NestedInteger::Int(799), |
| 115 | + NestedInteger::Int(833) |
| 116 | + ]), |
| 117 | + NestedInteger::List(vec![NestedInteger::List(vec![])]), |
| 118 | + NestedInteger::Int(10), |
| 119 | + NestedInteger::List(vec![]) |
| 120 | + ]) |
| 121 | + ); |
| 122 | +} |
0 commit comments