Skip to content

Commit 1972ac4

Browse files
committed
feat: add solutions to lc problem: No.0385
No.0385.Mini Parser
1 parent c221a70 commit 1972ac4

File tree

8 files changed

+1181
-74
lines changed

8 files changed

+1181
-74
lines changed

solution/0300-0399/0385.Mini Parser/README.md

+487-13
Large diffs are not rendered by default.

solution/0300-0399/0385.Mini Parser/README_EN.md

+487-13
Large diffs are not rendered by default.

solution/0300-0399/0385.Mini Parser/Solution.cpp

+29-14
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,35 @@
3030
class Solution {
3131
public:
3232
NestedInteger deserialize(string s) {
33-
if (s.empty()) return NestedInteger();
34-
if (s[0] != '[') return NestedInteger(stoi(s));
35-
if (s.size() <= 2) return NestedInteger();
36-
NestedInteger ans;
37-
int depth = 0;
38-
for (int i = 1, j = 1; i < s.size(); ++i) {
39-
if (depth == 0 && (s[i] == ',' || i == s.size() - 1)) {
40-
ans.add(deserialize(s.substr(j, i - j)));
41-
j = i + 1;
42-
} else if (s[i] == '[')
43-
++depth;
44-
else if (s[i] == ']')
45-
--depth;
33+
if (s[0] != '[') {
34+
return NestedInteger(stoi(s));
4635
}
47-
return ans;
36+
stack<NestedInteger> stk;
37+
int x = 0;
38+
bool neg = false;
39+
for (int i = 0; i < s.size(); ++i) {
40+
if (s[i] == '-') {
41+
neg = true;
42+
} else if (isdigit(s[i])) {
43+
x = x * 10 + s[i] - '0';
44+
} else if (s[i] == '[') {
45+
stk.push(NestedInteger());
46+
} else if (s[i] == ',' || s[i] == ']') {
47+
if (isdigit(s[i - 1])) {
48+
if (neg) {
49+
x = -x;
50+
}
51+
stk.top().add(NestedInteger(x));
52+
}
53+
x = 0;
54+
neg = false;
55+
if (s[i] == ']' && stk.size() > 1) {
56+
auto t = stk.top();
57+
stk.pop();
58+
stk.top().add(t);
59+
}
60+
}
61+
}
62+
return stk.top();
4863
}
4964
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* // This is the interface that allows for creating nested lists.
3+
* // You should not implement it, or speculate about its implementation
4+
* type NestedInteger struct {
5+
* }
6+
*
7+
* // Return true if this NestedInteger holds a single integer, rather than a nested list.
8+
* func (n NestedInteger) IsInteger() bool {}
9+
*
10+
* // Return the single integer that this NestedInteger holds, if it holds a single integer
11+
* // The result is undefined if this NestedInteger holds a nested list
12+
* // So before calling this method, you should have a check
13+
* func (n NestedInteger) GetInteger() int {}
14+
*
15+
* // Set this NestedInteger to hold a single integer.
16+
* func (n *NestedInteger) SetInteger(value int) {}
17+
*
18+
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
19+
* func (n *NestedInteger) Add(elem NestedInteger) {}
20+
*
21+
* // Return the nested list that this NestedInteger holds, if it holds a nested list
22+
* // The list length is zero if this NestedInteger holds a single integer
23+
* // You can access NestedInteger's List element directly if you want to modify it
24+
* func (n NestedInteger) GetList() []*NestedInteger {}
25+
*/
26+
func deserialize(s string) *NestedInteger {
27+
if s[0] != '[' {
28+
v, _ := strconv.Atoi(s)
29+
ans := NestedInteger{}
30+
ans.SetInteger(v)
31+
return &ans
32+
}
33+
stk := []*NestedInteger{}
34+
x := 0
35+
neg := false
36+
for i, c := range s {
37+
if c == '-' {
38+
neg = true
39+
} else if c >= '0' && c <= '9' {
40+
x = x*10 + int(c-'0')
41+
} else if c == '[' {
42+
stk = append(stk, &NestedInteger{})
43+
} else if c == ',' || c == ']' {
44+
if s[i-1] >= '0' && s[i-1] <= '9' {
45+
if neg {
46+
x = -x
47+
}
48+
t := NestedInteger{}
49+
t.SetInteger(x)
50+
stk[len(stk)-1].Add(t)
51+
}
52+
x = 0
53+
neg = false
54+
if c == ']' && len(stk) > 1 {
55+
t := stk[len(stk)-1]
56+
stk = stk[:len(stk)-1]
57+
stk[len(stk)-1].Add(*t)
58+
}
59+
}
60+
}
61+
return stk[0]
62+
}

solution/0300-0399/0385.Mini Parser/Solution.java

+25-17
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,35 @@
2828
*/
2929
class Solution {
3030
public NestedInteger deserialize(String s) {
31-
if ("".equals(s)) {
32-
return new NestedInteger();
33-
}
3431
if (s.charAt(0) != '[') {
3532
return new NestedInteger(Integer.parseInt(s));
3633
}
37-
if (s.length() <= 2) {
38-
return new NestedInteger();
39-
}
40-
NestedInteger ans = new NestedInteger();
41-
int depth = 0;
42-
for (int i = 1, j = 1; i < s.length(); ++i) {
43-
if (depth == 0 && (s.charAt(i) == ',' || i == s.length() - 1)) {
44-
ans.add(deserialize(s.substring(j, i)));
45-
j = i + 1;
46-
} else if (s.charAt(i) == '[') {
47-
++depth;
48-
} else if (s.charAt(i) == ']') {
49-
--depth;
34+
Deque<NestedInteger> stk = new ArrayDeque<>();
35+
int x = 0;
36+
boolean neg = false;
37+
for (int i = 0; i < s.length(); ++i) {
38+
char c = s.charAt(i);
39+
if (c == '-') {
40+
neg = true;
41+
} else if (Character.isDigit(c)) {
42+
x = x * 10 + c - '0';
43+
} else if (c == '[') {
44+
stk.push(new NestedInteger());
45+
} else if (c == ',' || c == ']') {
46+
if (Character.isDigit(s.charAt(i - 1))) {
47+
if (neg) {
48+
x = -x;
49+
}
50+
stk.peek().add(new NestedInteger(x));
51+
}
52+
x = 0;
53+
neg = false;
54+
if (c == ']' && stk.size() > 1) {
55+
NestedInteger t = stk.pop();
56+
stk.peek().add(t);
57+
}
5058
}
5159
}
52-
return ans;
60+
return stk.peek();
5361
}
5462
}

solution/0300-0399/0385.Mini Parser/Solution.py

+18-15
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,23 @@
4242
# """
4343
class Solution:
4444
def deserialize(self, s: str) -> NestedInteger:
45-
if not s:
46-
return NestedInteger()
4745
if s[0] != '[':
4846
return NestedInteger(int(s))
49-
if len(s) <= 2:
50-
return NestedInteger()
51-
ans = NestedInteger()
52-
depth, j = 0, 1
53-
for i in range(1, len(s)):
54-
if depth == 0 and (s[i] == ',' or i == len(s) - 1):
55-
ans.add(self.deserialize(s[j:i]))
56-
j = i + 1
57-
elif s[i] == '[':
58-
depth += 1
59-
elif s[i] == ']':
60-
depth -= 1
61-
return ans
47+
stk, x, neg = [], 0, False
48+
for i, c in enumerate(s):
49+
if c == '-':
50+
neg = True
51+
elif c.isdigit():
52+
x = x * 10 + int(c)
53+
elif c == '[':
54+
stk.append(NestedInteger())
55+
elif c in ',]':
56+
if s[i - 1].isdigit():
57+
if neg:
58+
x = -x
59+
stk[-1].add(NestedInteger(x))
60+
x, neg = 0, False
61+
if c == ']' and len(stk) > 1:
62+
t = stk.pop()
63+
stk[-1].add(t)
64+
return stk.pop()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* // This is the interface that allows for creating nested lists.
3+
* // You should not implement it, or speculate about its implementation
4+
* class NestedInteger {
5+
* If value is provided, then it holds a single integer
6+
* Otherwise it holds an empty nested list
7+
* constructor(value?: number) {
8+
* ...
9+
* };
10+
*
11+
* Return true if this NestedInteger holds a single integer, rather than a nested list.
12+
* isInteger(): boolean {
13+
* ...
14+
* };
15+
*
16+
* Return the single integer that this NestedInteger holds, if it holds a single integer
17+
* Return null if this NestedInteger holds a nested list
18+
* getInteger(): number | null {
19+
* ...
20+
* };
21+
*
22+
* Set this NestedInteger to hold a single integer equal to value.
23+
* setInteger(value: number) {
24+
* ...
25+
* };
26+
*
27+
* Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
28+
* add(elem: NestedInteger) {
29+
* ...
30+
* };
31+
*
32+
* Return the nested list that this NestedInteger holds,
33+
* or an empty list if this NestedInteger holds a single integer
34+
* getList(): NestedInteger[] {
35+
* ...
36+
* };
37+
* };
38+
*/
39+
40+
function deserialize(s: string): NestedInteger {
41+
if (s[0] !== '[') {
42+
return new NestedInteger(+s);
43+
}
44+
const stk: NestedInteger[] = [];
45+
let x = 0;
46+
let neg = false;
47+
for (let i = 0; i < s.length; ++i) {
48+
if (s[i] === '-') {
49+
neg = true;
50+
} else if (s[i] === '[') {
51+
stk.push(new NestedInteger());
52+
} else if (s[i] >= '0' && s[i] <= '9') {
53+
x = x * 10 + s[i].charCodeAt(0) - '0'.charCodeAt(0);
54+
} else if (s[i] === ',' || s[i] === ']') {
55+
if (s[i - 1] >= '0' && s[i - 1] <= '9') {
56+
stk[stk.length - 1].add(new NestedInteger(neg ? -x : x));
57+
}
58+
x = 0;
59+
neg = false;
60+
if (s[i] === ']' && stk.length > 1) {
61+
const t = stk.pop()!;
62+
stk[stk.length - 1].add(t);
63+
}
64+
}
65+
}
66+
return stk[0];
67+
}

solution/1000-1099/1027.Longest Arithmetic Subsequence/README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,15 @@
5656

5757
我们定义 $f[i][j]$ 表示以 $nums[i]$ 结尾且公差为 $j$ 的等差数列的最大长度。初始时 $f[i][j]=1$,即每个元素自身都是一个长度为 $1$ 的等差数列。
5858

59-
考虑 $f[i][j]$,我们可以枚举 $nums[i]$ 的前一个元素 $nums[k]$,那么 $d=nums[i]-nums[k]+500$,此时有 $f[i][j]=\max(f[i][j], f[k][j]+1)$,然后我们更新答案 $ans=\max(ans, f[i][j])$。
59+
> 由于公差可能为负数,且最大差值为 $500$,因此,我们可以将统一将公差加上 $500$,这样公差的范围就变成了 $[0, 1000]$。
60+
61+
考虑 $f[i]$,我们可以枚举 $nums[i]$ 的前一个元素 $nums[k]$,那么公差 $j=nums[i]-nums[k]+500$,此时有 $f[i][j]=\max(f[i][j], f[k][j]+1)$,然后我们更新答案 $ans=\max(ans, f[i][j])$。
6062

6163
最后返回答案即可。
6264

63-
时间复杂度 $O(n \times d)$,空间复杂度 $O(n \times d)$。其中 $n$ 和 $d$ 分别是数组 $nums$ 的长度以及数组 $nums$ 中元素的最大值与最小值的差值。
65+
> 如果初始时 $f[i][j]=0$,那么我们需要在最后返回答案时加上 $1$。
66+
67+
时间复杂度 $O(n \times (d + n))$,空间复杂度 $O(n \times d)$。其中 $n$ 和 $d$ 分别是数组 $nums$ 的长度以及数组 $nums$ 中元素的最大值与最小值的差值。
6468

6569
<!-- tabs:start -->
6670

0 commit comments

Comments
 (0)