题目:

思路:
定义两个指针:read和write
read用来遍历chars,write是用来记录压缩后的字符的存入位置
代码:
class Solution:
def compress(self, chars: List[str]) -> int:
#定义两个指针
read=write=0
n = len(chars)
while read < n:
cur = chars[read]
count = 0
while read < n and chars[read] == cur:
read += 1
count += 1
#记录当前的字符
chars[write]=cur
write += 1
if count > 1:
# 把数字转成字符串,逐位写入
for s in str(count):
chars[write]=s
write += 1
return write