基础的字符串操作,用两个指针扫一遍就行.
class Solution(object): def compress(self,chars): """ :type chars: List[str] :rtype: int """ if not chars: return 0 if len(chars) == 1: return 1 left, right, num = 0, 1, 1 while right <= len(chars): if right == len(chars): if num > 1: chars[left:right] = chars[left] + str(num) break if chars[right] == chars[left]: num += 1 right += 1 elif num > 1: # need modification chars[left:right] = chars[left] + str(num) left = left + len(chars[left] + str(num)) right = left + 1 num = 1 else: left = right right = left + 1 print(chars) return len(chars)
原文地址:https://www.cnblogs.com/zywscq/p/10504034.html
时间: 2024-10-08 04:46:17