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. springboot---->springboot中的类型转换(一)

    这里面我们简单的学习一下springboot中关于类型转换器的使用.人世间的事情莫过于此,用一个瞬间来喜欢一样东西,然后用多年的时间来慢慢拷问自己为什么会喜欢这样东西. springboot中的类型转 ...

  2. 【linux系列】yum安装报错 no mirrors to try

    执行以下命令去重新生成缓存 yum clean all yum makecache 更换源重新下载repo文件 重新生成缓存

  3. Android 逆向工具

    逆向分析工具 https://github.com/skylot/jadx/ https://github.com/google/android-classyshark https://github. ...

  4. angularjs笔记《二》

    小颖最近不知怎么了,老是犯困,也许是清明节出去玩,到现在还没缓过来吧,玩回来真的怕坐车了,报了个两日游得团,光坐车了,把人坐的难受得,去了也就是爬山,回来感觉都快瘫了,小颖去的时候还把我家仔仔抱着一起 ...

  5. Rails 添加新的运行环境

    Rails自带了development.test和production三个environments 我们可以添加Staging database.yml staging: adapter: mysql ...

  6. 常用AT指令集 (转)

    常 用 AT 命 令 手 册 .常用操作 1.1 AT 命令解释:检测 Module 与串口是否连通,能否接收 AT 命令: 命令格式:AT<CR> 命令返回:OK (与串口通信正常) ( ...

  7. 解决使用Foxmail客户端软件不能收取腾讯企业邮箱的全部邮件

    一般说来,使用Foxmail客户端软件收取邮箱时,需要作如下几步: 1.进入邮箱web界面授权开启POP3/SMTP服务.IMAP/SMTP等服务 2.在邮箱web界面配置收取选项,可选择收取全部邮件 ...

  8. 【C#】简单计算器源代码

    form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.D ...

  9. Zookeeper可以干什么

    在Zookeeper的官网上有这么一句话:ZooKeeper is a centralized service for maintaining configuration information, n ...

  10. Java-字符串加密

    1设计思想: 改程序是对小写的a到z进行加密,输入一段字符串str,输入加密的密匙k,判断录入的的字符与 ‘z’-k+1的大小,比其小的直接加上密匙转化为新的字符,大于的加(k-26)将最后几位字符转 ...