Leetcode 443 String Compression

Posted 周洋的Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 443 String Compression相关的知识,希望对你有一定的参考价值。

基础的字符串操作,用两个指针扫一遍就行.

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的主要内容,如果未能解决你的问题,请参考以下文章

443. String Compression - LeetCode

leetcode443. String Compression

leetcode 443. ???????????????(String Compression)

Leetcode 443 String Compression

LeetCode 443 压缩字符串[双指针] HERODING的LeetCode之路

LeetCode443-压缩字符串(双索引)