Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何就地压缩字符串_Arrays_Python 3.x_String - Fatal编程技术网

Arrays 如何就地压缩字符串

Arrays 如何就地压缩字符串,arrays,python-3.x,string,Arrays,Python 3.x,String,我一直在leetcode上研究这个问题 给定一个字符数组,将其压缩到位 压缩后的长度必须始终小于或等于原始数组 数组的每个元素都应该是长度为1的字符(不是int) 在适当地修改输入数组后,返回数组的新长度 我几乎有了一个解决方案,但我似乎无法计算字符串中的最后一个字符,而且如果数组中只有一个字符的数量没有显示为1,我也不确定如何做到这一点 我觉得我已经很接近了,如果可能的话,我想尽量保持现有的解决方案,而不做太多改变 这就是我目前所拥有的字符是字符列表 def压缩(字符): 字符=0 cur

我一直在leetcode上研究这个问题


给定一个字符数组,将其压缩到位

压缩后的长度必须始终小于或等于原始数组

数组的每个元素都应该是长度为1的字符(不是int)

在适当地修改输入数组后,返回数组的新长度


我几乎有了一个解决方案,但我似乎无法计算字符串中的最后一个字符,而且如果数组中只有一个字符的数量没有显示为1,我也不确定如何做到这一点

我觉得我已经很接近了,如果可能的话,我想尽量保持现有的解决方案,而不做太多改变

这就是我目前所拥有的<代码>字符是字符列表

def压缩(字符):
字符=0
curr=0
计数=0
当电流
我无法格式化您的代码以获得您所寻求的答案。根据您的回答,我能够整理代码和解释,以帮助您:

def compress(chars):

    count = 1
    current_position = 0

    # if it's a single character, just return a 
    # a basic array with count
    if len(chars) == 1:
        chars.append("1")
        return chars

    # loop till the 2nd last character is analyzed
    while current_position < len(chars) - 1:

        # assume that we haven't reached the 2nd last character
        # if next character is the same as the current one, delete
        # the current one and increase our count
        while current_position < len(chars) - 1 and \
                chars[current_position] == chars[current_position + 1]:
            del chars[current_position]
            count += 1

        # if next character isn't the same, time to add the count to
        # the list. Split the number into 
        # character list (e.g. 12 will become ["1", "2"]
        # insert those numbers behind the character and increment position
        for x in str(count):
            chars.insert(current_position + 1, str(x))
            current_position += 1

        # great, on to the next character
        current_position += 1

        # if we are now at the last character, it's a lonely character
        # give it a counter of 1 and exit the looping
        if current_position == len(chars) - 1:
            chars.append("1")
            break

        count = 1

    return chars

mylist = ["a","b","b","b","b","b","b","b","b","b","b","b","b"]
print(compress(mylist))
mylist = ["a","b","b","b","b","b","b","b","b","b","b","b","b"]
['a', '1', 'b', '1', '2']

mylist = ["a","a","a","a","a","a","a","a","a","a","b","b","b","b","b","b","b","b","b","b","b","b"]
['a', '1', '0', 'b', '1', '2']

mylist = ["a"]
['a', '1']

mylist = ["a","b"]
['a', '1', 'b', '1']

mylist = ["a","a","b","b","c","c","c"]
['a', '2', 'b', '2', 'c', '3']