1. 程式人生 > >Leetcode 443 String Compression

Leetcode 443 String Compression

int code elif 用兩個 字符 etc self 兩個 left

基礎的字符串操作,用兩個指針掃一遍就行.

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)

Leetcode 443 String Compression