443. 压缩字符串

爷,独闯天下 提交于 2020-04-12 12:08:59
给定一组字符,使用原地算法将其压缩。
压缩后的长度必须始终小于或等于原数组长度。
数组的每个元素应该是长度为1 的字符(不是 int 整数类型)。
在完成原地修改输入数组后,返回数组的新长度。

示例 1:
输入:["a","a","b","b","c","c","c"]
输出:返回6,输入数组的前6个字符应该是:["a","2","b","2","c","3"]
说明:
"aa"被"a2"替代。"bb"被"b2"替代。"ccc"被"c3"替代。












思路:
遍历list:
如果下一个元素和当前元素一样,ans不变,计数器加一;
如果下一个元素和当前元素不一样,将ans和计数器转型后拼接到结果串后面;然后 ans重置,计数器重置;

  1 class Solution(object):
  2     def compress3(self, chars):
  3         # 设定记录元素的指针 和 写指针
  4         anchor = write = 0
  5         # 遍历原list
  6         for read, c in enumerate(chars):
  7             # 如果读到了原list末尾,或者当前字符与后一个字符不同,说明读到了相同字符的末尾
  8             if read + 1 == len(chars) or chars[read + 1] != c:
  9                 # 将记录的元素写到新list中
 10                 chars[write] = chars[anchor]
 11                 # 写指针后移
 12                 write += 1
 13                 # 如果读指针比记录指针走得远
 14                 if read > anchor:
 15                     # 将计数器字符化并拼接到新list后
 16                     for digit in str(read - anchor + 1):
 17                         chars[write] = digit
 18                         write += 1
 19                 # 记录指针赋新值
 20                 anchor = read + 1
 21         res = [0]*write
 22         for k in range(write):
 23             if k<write:
 24                 res[k] = chars[k]
 25         print(res)
 26         # 返回写指针的位置,即是新list的长度
 27         return write
 28 
 29     def compress2(self, chars):
 30         """
 31         :type chars: List[str]
 32         :rtype: int
 33         """
 34         # 写指针
 35         ans = 0
 36         # 记录指针
 37         index = 0
 38         # 遍历原list
 39         for i, ch in enumerate(chars):
 40             # 要是遍历到了list的最后或者当前字符与下一个字符不同,则相同字符读取结束
 41             if i == len(chars) - 1 or chars[i + 1] != ch:
 42                 # 将记录指针指向的元素赋给新list
 43                 chars[ans] = chars[index]
 44                 # 写指针后移
 45                 ans += 1
 46                 # 要是读指针走在记录指针的前面
 47                 if i > index:
 48                     # 计算相同字符的长度
 49                     length = i - index + 1
 50                     for j in str(length):
 51                         chars[ans] = j
 52                         ans += 1
 53                 index = i + 1
 54         result = [0] * ans
 55         for k in range(ans):
 56             if k < ans:
 57                 result[k] = chars[k]
 58         print(result)
 59         return ans
 60 
 61     """
 62     将整数index按位拼接到集合后面
 63     """
 64 
 65     def intToList(self, result, index):
 66         """
 67         :type result: List[str]
 68         :type index: int
 69         :rtype: List[str]
 70         """
 71         temp = str(index)
 72         for i in range(len(temp)):
 73             result.append(temp[i])
 74         return result
 75 
 76     def compress(self, chars):
 77         """
 78         :type chars: List[str]
 79         :rtype: int
 80         """
 81         print("集合的长度:", len(chars))
 82         # 计数器初始化
 83         index = 1
 84         # 结果list的长度
 85         listLen = 1
 86         for i in range(1, len(chars)):
 87             # print(chars[i])
 88             if chars[i] == chars[i - 1]:
 89                 index += 1
 90             elif chars[i] != chars[i - 1]:
 91                 chars[listLen] = str(index)
 92                 listLen += 1
 93                 chars[listLen] = chars[i]
 94                 listLen += 1
 95                 index = 1
 96         print(chars)
 97         return listLen
 98 
 99 
100 if __name__ == '__main__':
101     solution = Solution()
102     print(solution.compress2(["a"]))
103     print(solution.compress2(["a", "a", "b", "b", "c", "c", "c"]))
104     print(solution.compress2(["a", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b"]))

 

 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!