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 input array in-place, return the new length of the array.

Follow up:
Could you solve it using only O(1) extra space?

Example 1:

Input:
["a","a","b","b","c","c","c"] Output:
Return 6, and the first 6 characters of the input array should be: ["a","2","b","2","c","3"] Explanation:
"aa" is replaced by "a2". "bb" is replaced by "b2". "ccc" is replaced by "c3".

Example 2:

Input:
["a"] Output:
Return 1, and the first 1 characters of the input array should be: ["a"] Explanation:
Nothing is replaced.

Example 3:

Input:
["a","b","b","b","b","b","b","b","b","b","b","b","b"] Output:
Return 4, and the first 4 characters of the input array should be: ["a","b","1","2"]. Explanation:
Since the character "a" does not repeat, it is not compressed. "bbbbbbbbbbbb" is replaced by "b12".
Notice each digit has it's own entry in the array.

Note:

  1. All characters have an ASCII value in [35, 126].
  2. 1 <= len(chars) <= 1000.

这个题目不难, 就是edge case有点麻烦, 然后我们这里用当前和下一个char来判断, 会比较好, 如果count >1, 则需要进行相应的处理.

Code

class Solution:
def codeString(self, chars):
write, count = 0, 1
for index, c in enumerate(chars):
if index +1 == len(chars) or chars[index + 1] != c:
chars[write] = c
write += 1
if count >1:
num = str(count)
for i in range(len(num)):
chars[write] = num[i]
write += 1
count = 1
else:
count += 1
return write

[LeetCode] 443. String Compression_Easy tag:String的更多相关文章

  1. [LeetCode] 415. Add Strings_Easy tag: String

    Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...

  2. [LeetCode] 557. Reverse Words in a String III_Easy tag: String

    Given a string, you need to reverse the order of characters in each word within a sentence while sti ...

  3. [LeetCode] 67. Add Binary_Easy tag: String

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

  4. [LeetCode] Reverse Words in a String II 翻转字符串中的单词之二

    Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...

  5. [LeetCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

  6. LeetCode Reverse Words in a String II

    原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ...

  7. LeetCode: Reverse Words in a String && Rotate Array

    Title: Given an input string, reverse the string word by word. For example,Given s = "the sky i ...

  8. LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation

    LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...

  9. perl malformed JSON string, neither tag, array, object, number, string or atom, at character offset

    [root@wx03 ~]# cat a17.pl use JSON qw/encode_json decode_json/ ; use Encode; my $data = [ { 'name' = ...

随机推荐

  1. python错误 ImportError: No module named setuptools 解决方法[转]

    在python运行过程中出现如下错误: python错误:ImportError: No module named setuptools这句错误提示的表面意思是:没有setuptools的模块,说明p ...

  2. kafka---->kafka的使用(一)

    今天我们来学习一下kafka的简单的使用与配置.世上有可以挽回的和不可挽回的事,而时间经过就是一种不可挽回的事. kafka的安装配置 一.kafka的使用场景 活动跟踪:网站用户与前端应用程序发生交 ...

  3. 报错---“node install.js”

    如图 解决方案: 目录中执行 npm install chromedriver --chromedriver_cdnurl=http://cdn.npm.taobao.org/dist/chromed ...

  4. (转载)解决AndroidStudio导入项目在 Building gradle project info 一直卡住

    源地址http://blog.csdn.net/yyh352091626/article/details/51490976 Android Studio导入项目的时候,一直卡在Building gra ...

  5. 23种设计模式之装饰模式(Decorator)

    装饰模式是一种对象结构型模式,可动态地给一个对象增加一些额外的职责,就增加对象功能来说,装饰模式比生成子类实现更为灵活.通过装饰模式,可以在不影响其他对象的情况下,以动态.透明的方式给单个对象添加职责 ...

  6. redis -clock_gettime问题

    /home/wm/redis-/deps/jemalloc/src/nstime.c:: undefined reference to `clock_gettime' 这个错误 解决思路如下 .查找实 ...

  7. Spark2 jar包运行完成,退出spark,释放资源

    import org.apache.spark.sql.SparkSession import org.apache.spark.sql.Dataset import org.apache.spark ...

  8. vue之介绍

    vue的作者叫尤雨溪,中国人.自认为很牛逼的人物,也是我的崇拜之神. 关于他本人的认知,希望大家读一下这篇关于他的文章,或许你会对语言,技术,产生浓厚的兴趣.https://mp.weixin.qq. ...

  9. 访问php文件显示源码

    前天新装了个LAMP的环境,兴冲冲的clone下来代码,结果一访问乐子就大了,直接显现源码 面对这个问题,冥思苦想,四处找资料啊 让我改这改那的,最后终于找到症结 Ubuntu 16.04 系统 LA ...

  10. JQuery EasyUI 日期控件 怎样做到只显示年月,而不显示日

    标题问题的答案在OSChina中 http://www.oschina.net/question/2282354_224401?fromerr=lHJTcN89 我还是把这个记录下来 ======== ...