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. 游戏服务器学习笔记 3———— firefly 的代码结构,逻辑

    注:以下所有代码都是拿暗黑来举例,由于本人能力有限很多地方还没有看透彻,所以建议大家只是参考.有不对的地方非常欢迎指正. 一.结构     系统启动命令是,python statmaster.py,启 ...

  2. cookie设置在特定时间点过期的方法

    假设需求为:在当天晚上0:00过期. 方法: 得到当天晚上0:00这个时间点的一个时间. function getNextDate(){        var d = new Date(),      ...

  3. MySQL优化之SQL耗时瓶颈 SHOW profiles

    1.首先查看是否开启profiling功能 SHOW VARIABLES LIKE '%pro%'; 或者 SELECT @@profiling; 2.开启profiling SET profilin ...

  4. Android7.0 PowerManagerService 之亮灭屏(二) PMS 电源状态管理updatePowerStateLocked()

    本篇注意接着上篇[Android7.0 PowerManagerService 之亮灭屏(一)]继续分析量灭屏的流程,这篇主要分析PMS的状态计算和更新流程,也是PMS中最为重要和复杂的一部分电源状态 ...

  5. [NHibernate] Guid 作主键速度超慢的背后

    http://blog.csdn.net/educast/article/details/6602353 最近遇到了一个让人抓狂的性能问题.生产环境里有一张表的数据量目前达到了 70 万条.结果发现无 ...

  6. Spark2 broadcast广播变量

    A broadcast variable. Broadcast variables allow the programmer to keep a read-only variable cached o ...

  7. App开发如何制作测试数据

    OHHTTPStubs 使用第三方请求库模拟返回json数据 https://github.com/AliSoftware/OHHTTPStubs 使用青花瓷maplocal制造假数据 https:/ ...

  8. opengl学习笔记(五):组合变换,绘制一个简单的太阳系

    创建太阳系模型 描述的程序绘制一个简单的太阳系,其中有一颗行星和一颗太阳,用同一个函数绘制.需要使用glRotate*()函数让这颗行星绕太阳旋转,并且绕自身的轴旋转.还需要使用glTranslate ...

  9. 计蒜客 30990 - An Olympian Math Problem - [简单数学题][2018ICPC南京网络预赛A题]

    题目链接:https://nanti.jisuanke.com/t/30990 Alice, a student of grade 6, is thinking about an Olympian M ...

  10. POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]

    题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...