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)

原文地址:https://www.cnblogs.com/zywscq/p/10504034.html

时间: 2024-10-08 04:46:17

Leetcode 443 String Compression的相关文章

443. String Compression - LeetCode

Question 443.?String Compression Solution 题目大意:把一个有序数组压缩, 思路:遍历数组 Java实现: public int compress(char[] chars) { if (chars.length == 0) return 0; StringBuilder sb = new StringBuilder(); char cur = chars[0]; int sum = 1; for (int i = 1; i <= chars.length

【leetcode】443. String Compression

题目如下: Given an array of characters, compress it in-place. The length after compression must always be smaller than or equal to the original array. Every element of the array should be a character (not int) of length 1. After you are done modifying th

443. String Compression 字符串压缩

Given an array of characters, compress it in-place. The length after compression must always be smaller than or equal to the original array. Every element of the array should be a character (not int) of length 1. After you are done modifying the inpu

443. String Compression

问题描述: Given an array of characters, compress it in-place. The length after compression must always be smaller than or equal to the original array. Every element of the array should be a character (not int) of length 1. After you are done modifying th

443 String Compression 压缩字符串

给定一组字符,使用原地算法将其压缩.压缩后的长度必须始终小于或等于原数组长度.数组的每个元素应该是长度为1 的字符(不是 int 整数类型).在完成原地修改输入数组后,返回数组的新长度.进阶:你能否仅使用O(1) 空间解决问题?示例 1:输入:["a","a","b","b","c","c","c"]输出:返回6,输入数组的前6个字符应该是:["a"

[LC] 443. String Compression

Given an array of characters, compress it in-place. The length after compression must always be smaller than or equal to the original array. Every element of the array should be a character (not int) of length 1. After you are done modifying the inpu

LeetCode_443. String Compression

443. String Compression Easy Given an array of characters, compress it in-place. The length after compression must always be smaller than or equal to the original array. Every element of the array should be a character (not int) of length 1. After yo

LeetCode:String to Integer (atoi)

1.题目名称 String to Integer (atoi) (字符串到数字的转换) 2.题目地址 https://leetcode.com/problems/string-to-integer-atoi/ 3.题目内容 英文:Implement atoi to convert a string to an integer. 中文:实现atoi函数,将输入的字符串(String类型)转换为整型数据(Integer类型) 提示:实现的atoi函数需要满足以下特征 忽略字符串第一个非空格字符前的所

Leetcode 数 String to Integer (atoi)

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie String to Integer (atoi) Total Accepted: 9862 Total Submissions: 67880 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge,