[LeetCode] 443. String Compression_Easy tag:String
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:
- All characters have an ASCII value in
[35, 126]. 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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- LeetCode Reverse Words in a String II
原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ...
- 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 ...
- 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 ...
- 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' = ...
随机推荐
- 文件系统的挂载(2)---挂载rootfs文件系统
一.目的 本文主要讲述linux内核rootfs文件系统的挂载过程,内核版本为3.10. rootfs是基于内存的文件系统,没有实际的存储设备,所有操作都在内存中完成.为了保证linux内核的精简性, ...
- java.util.concurrent.Future Basics
Hereby I am starting a series of articles about future concept in programming languages (also known ...
- requests源码分析
0.前言 (1) 拆部分reques中感兴趣t的轮子 (2)对一些感兴趣的pythonic写法做一些归纳 1.用object.__setattr__来初始化构造函数 反正我之前就是直接实例对象时把所有 ...
- 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验十八:SDRAM模块① — 单字读写
实验十八:SDRAM模块① — 单字读写 笔者与SDRAM有段不短的孽缘,它作为冤魂日夜不断纠缠笔者.笔者尝试过许多方法将其退散,不过屡试屡败的笔者,最终心情像橘子一样橙.<整合篇>之际, ...
- 调用TerminateProcess是无法触发DLL_PROCESS_DETACH的
当应用程序中调用TerminateProcess函数,对于在DllMain函数中处理DLL_PROCESS_DETACH的额外代码操作是无法被执行的.比如:释放资源.数据持久化等.
- Linux(CentOS)安装JDK(.tar.gz)并配置
本文思路转自http://blog.sina.com.cn/s/blog_81631744010137iy.html 点击(此处)折叠或打开 1.到 甲骨文(oracle)下载jdk不用多说 tar ...
- Android加载asset的db
extends:http://blog.csdn.net/lihenair/article/details/21232887 项目需要将预先处理的db文件加载到数据库中,然后读取其中的信息并显示 加载 ...
- python标准库和第三方库的区别
1.python的标准库是随着pyhon安装的时候默认自带的库. 2.python的第三方库,需要下载后安装到python的安装目录下,不同的第三方库安装及使用方法不同. 3.它们调用方式是一样的,都 ...
- 利用javascript判断文件是否存在
1 判断本地文件是否存在 var fso,s=filespec; // filespec="C:/path/myfile.txt" fso=new ActiveXObject(&q ...
- POJ-放苹果(DP)
放苹果 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 29074 Accepted: 18376 Description 把M个 ...