Skip to content

Commit 4f8bc26

Browse files
committed
feat: add solutions to lc problems: No.0394, 0461
1 parent 12cda6d commit 4f8bc26

File tree

8 files changed

+216
-84
lines changed

8 files changed

+216
-84
lines changed

solution/0300-0399/0394.Decode String/README.md

+54-3
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,78 @@
4040
<strong>输出:</strong>&quot;abccdcdcdxyz&quot;
4141
</pre>
4242

43-
4443
## 解法
4544

4645
<!-- 这里可写通用的实现逻辑 -->
4746

47+
用栈 s1 存储左括号前的数字 num,栈 s2 存储左括号前的字符串 res。
48+
49+
遍历字符串 s 中每个字符 c:
50+
51+
- 若 c 是数字,则累乘数字 num
52+
-`c == '['`,则将左括号前的数字 num 存入 s1,左括号前的字符串 res 存入 s2,并将 num 重新置为 0,res 置为空串
53+
-`c == ']'`,则 `res = s2.pop() + res * s1.pop()`
54+
- 若 c 是字符,则累加字符串 res
55+
56+
最后返回 res 即可。
57+
4858
<!-- tabs:start -->
4959

5060
### **Python3**
5161

5262
<!-- 这里可写当前语言的特殊实现逻辑 -->
5363

5464
```python
55-
65+
class Solution:
66+
def decodeString(self, s: str) -> str:
67+
s1, s2 = [], []
68+
num, res = 0, ''
69+
for c in s:
70+
if c.isdigit():
71+
num = num * 10 + int(c)
72+
elif c == '[':
73+
s1.append(num)
74+
s2.append(res)
75+
num, res = 0, ''
76+
elif c == ']':
77+
res = s2.pop() + res * s1.pop()
78+
else:
79+
res += c
80+
return res
5681
```
5782

5883
### **Java**
5984

6085
<!-- 这里可写当前语言的特殊实现逻辑 -->
6186

6287
```java
63-
88+
class Solution {
89+
public String decodeString(String s) {
90+
Deque<Integer> s1 = new ArrayDeque<>();
91+
Deque<String> s2 = new ArrayDeque<>();
92+
int num = 0;
93+
String res = "";
94+
for (char c : s.toCharArray()) {
95+
if ('0' <= c && c <= '9') {
96+
num = num * 10 + c - '0';
97+
} else if (c == '[') {
98+
s1.push(num);
99+
s2.push(res);
100+
num = 0;
101+
res = "";
102+
} else if (c == ']') {
103+
StringBuilder t = new StringBuilder();
104+
for (int i = 0, n = s1.pop(); i < n; ++i) {
105+
t.append(res);
106+
}
107+
res = s2.pop() + t.toString();
108+
} else {
109+
res += String.valueOf(c);
110+
}
111+
}
112+
return res;
113+
}
114+
}
64115
```
65116

66117
### **...**

solution/0300-0399/0394.Decode String/README_EN.md

+43-2
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,54 @@
4444
### **Python3**
4545

4646
```python
47-
47+
class Solution:
48+
def decodeString(self, s: str) -> str:
49+
s1, s2 = [], []
50+
num, res = 0, ''
51+
for c in s:
52+
if c.isdigit():
53+
num = num * 10 + int(c)
54+
elif c == '[':
55+
s1.append(num)
56+
s2.append(res)
57+
num, res = 0, ''
58+
elif c == ']':
59+
res = s2.pop() + res * s1.pop()
60+
else:
61+
res += c
62+
return res
4863
```
4964

5065
### **Java**
5166

5267
```java
53-
68+
class Solution {
69+
public String decodeString(String s) {
70+
Deque<Integer> s1 = new ArrayDeque<>();
71+
Deque<String> s2 = new ArrayDeque<>();
72+
int num = 0;
73+
String res = "";
74+
for (char c : s.toCharArray()) {
75+
if ('0' <= c && c <= '9') {
76+
num = num * 10 + c - '0';
77+
} else if (c == '[') {
78+
s1.push(num);
79+
s2.push(res);
80+
num = 0;
81+
res = "";
82+
} else if (c == ']') {
83+
StringBuilder t = new StringBuilder();
84+
for (int i = 0, n = s1.pop(); i < n; ++i) {
85+
t.append(res);
86+
}
87+
res = s2.pop() + t.toString();
88+
} else {
89+
res += String.valueOf(c);
90+
}
91+
}
92+
return res;
93+
}
94+
}
5495
```
5596

5697
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,27 @@
11
class Solution {
22
public String decodeString(String s) {
3-
char[] chars = s.toCharArray();
4-
Stack<Character> stack = new Stack<>();
5-
for (int i = 0; i < chars.length; i++) {
6-
if (chars[i] != ']') {
7-
stack.push(chars[i]);
8-
} else {
9-
// 找[]内的内容
10-
String t = "";
11-
while (stack.peek() != '[') {
12-
t = stack.pop() + t;
13-
}
14-
// 弹出[
15-
stack.pop();
16-
// 找前面的数字
17-
String n = "";
18-
while (!stack.isEmpty() && stack.peek() >= '0' && stack.peek() <= '9') {
19-
n = stack.pop() + n;
20-
}
21-
int c = Integer.valueOf(n);
22-
23-
String tmpCombine = "";
24-
// 把字母重复c次
25-
for (int j = 0; j < c; j++) {
26-
tmpCombine += t;
27-
}
28-
29-
// 放回stack
30-
char[] tmp = tmpCombine.toCharArray();
31-
for (int j = 0; j < tmp.length; j++) {
32-
stack.push(tmp[j]);
3+
Deque<Integer> s1 = new ArrayDeque<>();
4+
Deque<String> s2 = new ArrayDeque<>();
5+
int num = 0;
6+
String res = "";
7+
for (char c : s.toCharArray()) {
8+
if ('0' <= c && c <= '9') {
9+
num = num * 10 + c - '0';
10+
} else if (c == '[') {
11+
s1.push(num);
12+
s2.push(res);
13+
num = 0;
14+
res = "";
15+
} else if (c == ']') {
16+
StringBuilder t = new StringBuilder();
17+
for (int i = 0, n = s1.pop(); i < n; ++i) {
18+
t.append(res);
3319
}
20+
res = s2.pop() + t.toString();
21+
} else {
22+
res += String.valueOf(c);
3423
}
3524
}
36-
37-
// stack即为结果
38-
String ans = "";
39-
while (!stack.isEmpty()) {
40-
ans = stack.pop() + ans;
41-
}
42-
return ans;
25+
return res;
4326
}
4427
}
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,16 @@
1-
class Solution(object):
2-
def decodeString(self, s):
3-
"""
4-
:type s: str
5-
:rtype: str
6-
"""
7-
def deco(s):
8-
if '[' not in s and ']' not in s:
9-
return s
10-
i = j = 0
11-
ans = ''
12-
count = ''
13-
while i < len(s):
14-
if s[i].isdigit():
15-
count += s[i]
16-
i += 1
17-
elif s[i].isalpha():
18-
ans += s[i]
19-
i += 1
20-
elif s[i] == '[':
21-
j = i + 1
22-
zuo = 0
23-
while j < len(s):
24-
if s[j] == '[':
25-
zuo += 1
26-
j += 1
27-
elif s[j] == ']':
28-
if zuo != 0:
29-
zuo -= 1
30-
j += 1
31-
else:
32-
if not count:
33-
ans += deco(s[i + 1:j])
34-
else:
35-
ans += int(count) * deco(s[i + 1:j])
36-
count = ''
37-
i = j + 1
38-
break
39-
else:
40-
j += 1
41-
return ans
42-
return deco(s)
1+
class Solution:
2+
def decodeString(self, s: str) -> str:
3+
s1, s2 = [], []
4+
num, res = 0, ''
5+
for c in s:
6+
if c.isdigit():
7+
num = num * 10 + int(c)
8+
elif c == '[':
9+
s1.append(num)
10+
s2.append(res)
11+
num, res = 0, ''
12+
elif c == ']':
13+
res = s2.pop() + res * s1.pop()
14+
else:
15+
res += c
16+
return res

solution/0400-0499/0461.Hamming Distance/README.md

+31
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,37 @@ var hammingDistance = function(x, y) {
103103
};
104104
```
105105

106+
### **C++**
107+
108+
```cpp
109+
class Solution {
110+
public:
111+
int hammingDistance(int x, int y) {
112+
x ^= y;
113+
int count = 0;
114+
while (x) {
115+
++count;
116+
x &= (x - 1);
117+
}
118+
return count;
119+
}
120+
};
121+
```
122+
123+
### **Go**
124+
125+
```go
126+
func hammingDistance(x int, y int) int {
127+
x ^= y
128+
count := 0
129+
for x != 0 {
130+
count++
131+
x &= (x - 1)
132+
}
133+
return count
134+
}
135+
```
136+
106137
### **...**
107138

108139
```

solution/0400-0499/0461.Hamming Distance/README_EN.md

+31
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,37 @@ var hammingDistance = function(x, y) {
104104
};
105105
```
106106

107+
### **C++**
108+
109+
```cpp
110+
class Solution {
111+
public:
112+
int hammingDistance(int x, int y) {
113+
x ^= y;
114+
int count = 0;
115+
while (x) {
116+
++count;
117+
x &= (x - 1);
118+
}
119+
return count;
120+
}
121+
};
122+
```
123+
124+
### **Go**
125+
126+
```go
127+
func hammingDistance(x int, y int) int {
128+
x ^= y
129+
count := 0
130+
for x != 0 {
131+
count++
132+
x &= (x - 1)
133+
}
134+
return count
135+
}
136+
```
137+
107138
### **...**
108139

109140
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
int hammingDistance(int x, int y) {
4+
x ^= y;
5+
int count = 0;
6+
while (x) {
7+
++count;
8+
x &= (x - 1);
9+
}
10+
return count;
11+
}
12+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
func hammingDistance(x int, y int) int {
2+
x ^= y
3+
count := 0
4+
for x != 0 {
5+
count++
6+
x &= (x - 1)
7+
}
8+
return count
9+
}

0 commit comments

Comments
 (0)