443. 压缩字符串
为保证权益,题目请参考 443. 压缩字符串(From LeetCode).
解决方案1
Python
python
# 443. 压缩字符串
# https://leetcode-cn.com/problems/string-compression/
from typing import List
class Solution:
def compress(self, chars: List[str]) -> int:
beforeChar = chars[0]
beforeNum = 0
left = 0
chars.append("@@@")
for j, c in enumerate(chars):
if c == beforeChar:
beforeNum = beforeNum + 1
else:
if beforeNum != 1:
chars[left] = beforeChar
beforeChar = c
left += 1
beforeNumStr = str(beforeNum)
for i in range(len(beforeNumStr)):
chars[left + i] = beforeNumStr[i]
left += len(beforeNumStr)
beforeNum = 1
else:
chars[left] = beforeChar
left += 1
beforeChar = c
while left + 1 <= len(chars):
chars.pop()
# print(chars)
return len(chars)
if __name__ == "__main__":
solution = Solution()
print(solution.compress(["a", "b", "b"]))
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
30
31
32
33
34
35
36
37
38
39
40
41
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
30
31
32
33
34
35
36
37
38
39
40
41