Skip to content

Commit 45e8112

Browse files
yanglbmeacbin
authored andcommitted
feat: add solutions to lc problems: No.271,276
* No.0271.Encode and Decode Strings * No.0276.Paint Fence
1 parent 51b23e9 commit 45e8112

File tree

17 files changed

+364
-186
lines changed

17 files changed

+364
-186
lines changed

solution/0200-0299/0266.Palindrome Permutation/README.md

-8
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,4 @@ var canPermutePalindrome = function (s) {
131131

132132
<!-- tabs:end -->
133133

134-
### 方法二:哈希表
135-
136-
利用哈希表来维护元素。遍历字符串每个字母 $s[i]$,若 $s[i]$ 在哈希表中,则将 $s[i]$ 从哈希表中删除,否则将 $s[i]$ 加入哈希表。
137-
138-
遍历结束,若哈希表中元素个数不超过 $1$,则返回 $true$,否则返回 $false$。
139-
140-
时间复杂度 $O(n)$,空间复杂度 $O(|\Sigma|)$。其中 $n$ 是字符串的长度,而 $|\Sigma|$ 是字符集的大小,本题中字符集为小写字母,因此 $|\Sigma|=26$。
141-
142134
<!-- end -->

solution/0200-0299/0271.Encode and Decode Strings/README.md

+16-42
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,11 @@
4949

5050
## 解法
5151

52-
### 方法一:使用非 ASCII 码的分隔符
52+
### 方法一:编码字符串长度
5353

54-
Python 中可以直接 `chr(257)` 作为字符串的分隔符,这样就可以实现字符串的编码和解码。
54+
编码时,将字符串的长度转成固定 $4$ 位的字符串,加上字符串本身,依次拼接到结果字符串。
55+
56+
解码时,先取前四位字符串,得到长度,再通过长度截取后面的字符串。依次截取,最终得到字符串列表。
5557

5658
时间复杂度 $O(n)$。
5759

@@ -61,11 +63,21 @@ Python 中可以直接 `chr(257)` 作为字符串的分隔符,这样就可以
6163
class Codec:
6264
def encode(self, strs: List[str]) -> str:
6365
"""Encodes a list of strings to a single string."""
64-
return chr(257).join(strs)
66+
ans = []
67+
for s in strs:
68+
ans.append('{:4}'.format(len(s)) + s)
69+
return ''.join(ans)
6570

6671
def decode(self, s: str) -> List[str]:
6772
"""Decodes a single string to a list of strings."""
68-
return s.split(chr(257))
73+
ans = []
74+
i, n = 0, len(s)
75+
while i < n:
76+
size = int(s[i : i + 4])
77+
i += 4
78+
ans.append(s[i : i + size])
79+
i += size
80+
return ans
6981

7082

7183
# Your Codec object will be instantiated and called as such:
@@ -173,42 +185,4 @@ func (codec *Codec) Decode(strs string) []string {
173185

174186
<!-- tabs:end -->
175187

176-
### 方法二:编码字符串长度
177-
178-
编码时,将字符串的长度转成固定 $4$ 位的字符串,加上字符串本身,依次拼接到结果字符串。
179-
180-
解码时,先取前四位字符串,得到长度,再通过长度截取后面的字符串。依次截取,最终得到字符串列表。
181-
182-
时间复杂度 $O(n)$。
183-
184-
<!-- tabs:start -->
185-
186-
```python
187-
class Codec:
188-
def encode(self, strs: List[str]) -> str:
189-
"""Encodes a list of strings to a single string."""
190-
ans = []
191-
for s in strs:
192-
ans.append('{:4}'.format(len(s)) + s)
193-
return ''.join(ans)
194-
195-
def decode(self, s: str) -> List[str]:
196-
"""Decodes a single string to a list of strings."""
197-
ans = []
198-
i, n = 0, len(s)
199-
while i < n:
200-
size = int(s[i : i + 4])
201-
i += 4
202-
ans.append(s[i : i + size])
203-
i += size
204-
return ans
205-
206-
207-
# Your Codec object will be instantiated and called as such:
208-
# codec = Codec()
209-
# codec.decode(codec.encode(strs))
210-
```
211-
212-
<!-- tabs:end -->
213-
214188
<!-- end -->

solution/0200-0299/0271.Encode and Decode Strings/README_EN.md

+19-35
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,35 @@ String[] strs = decoder.decode(msg);
8181

8282
## Solutions
8383

84-
### Solution 1
84+
### Solution 1: Encode String Length
85+
86+
During encoding, we convert the length of the string into a fixed 4-digit string, add the string itself, and append it to the result string in sequence.
87+
88+
During decoding, we first take the first four digits of the string to get the length, and then cut the following string according to the length. We cut it in sequence until we get the list of strings.
89+
90+
The time complexity is $O(n)$.
8591

8692
<!-- tabs:start -->
8793

8894
```python
8995
class Codec:
9096
def encode(self, strs: List[str]) -> str:
9197
"""Encodes a list of strings to a single string."""
92-
return chr(257).join(strs)
98+
ans = []
99+
for s in strs:
100+
ans.append('{:4}'.format(len(s)) + s)
101+
return ''.join(ans)
93102

94103
def decode(self, s: str) -> List[str]:
95104
"""Decodes a single string to a list of strings."""
96-
return s.split(chr(257))
105+
ans = []
106+
i, n = 0, len(s)
107+
while i < n:
108+
size = int(s[i : i + 4])
109+
i += 4
110+
ans.append(s[i : i + size])
111+
i += size
112+
return ans
97113

98114

99115
# Your Codec object will be instantiated and called as such:
@@ -201,36 +217,4 @@ func (codec *Codec) Decode(strs string) []string {
201217

202218
<!-- tabs:end -->
203219

204-
### Solution 2
205-
206-
<!-- tabs:start -->
207-
208-
```python
209-
class Codec:
210-
def encode(self, strs: List[str]) -> str:
211-
"""Encodes a list of strings to a single string."""
212-
ans = []
213-
for s in strs:
214-
ans.append('{:4}'.format(len(s)) + s)
215-
return ''.join(ans)
216-
217-
def decode(self, s: str) -> List[str]:
218-
"""Decodes a single string to a list of strings."""
219-
ans = []
220-
i, n = 0, len(s)
221-
while i < n:
222-
size = int(s[i : i + 4])
223-
i += 4
224-
ans.append(s[i : i + size])
225-
i += size
226-
return ans
227-
228-
229-
# Your Codec object will be instantiated and called as such:
230-
# codec = Codec()
231-
# codec.decode(codec.encode(strs))
232-
```
233-
234-
<!-- tabs:end -->
235-
236220
<!-- end -->

solution/0200-0299/0271.Encode and Decode Strings/Solution.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
class Codec:
22
def encode(self, strs: List[str]) -> str:
33
"""Encodes a list of strings to a single string."""
4-
return chr(257).join(strs)
4+
ans = []
5+
for s in strs:
6+
ans.append("{:4}".format(len(s)) + s)
7+
return "".join(ans)
58

69
def decode(self, s: str) -> List[str]:
710
"""Decodes a single string to a list of strings."""
8-
return s.split(chr(257))
11+
ans = []
12+
i, n = 0, len(s)
13+
while i < n:
14+
size = int(s[i : i + 4])
15+
i += 4
16+
ans.append(s[i : i + size])
17+
i += size
18+
return ans
919

1020

1121
# Your Codec object will be instantiated and called as such:

solution/0200-0299/0271.Encode and Decode Strings/Solution2.py

-23
This file was deleted.

0 commit comments

Comments
 (0)