Skip to content

Commit

Permalink
2020-03-15
Browse files Browse the repository at this point in the history
  • Loading branch information
JiayangWu committed Mar 16, 2020
1 parent 990ebdd commit 480be5b
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions 面试题01.06.字符串压缩/面试题01.06-字符串压缩.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution(object):
def compressString(self, S):
"""
:type S: str
:rtype: str
"""
pre = None
cnt = 0
res = ""
for ch in S:
if not pre:
cnt += 1
pre = ch
else:
if ch == pre:
cnt += 1
else:
res += pre + str(cnt)
cnt = 1
pre = ch
if S:
res += pre + str(cnt)
return res if len(res) < len(S) else S

0 comments on commit 480be5b

Please sign in to comment.