[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' = ...
随机推荐
- shell 脚本调试
1.第一行加 -xv #!/bin/bash –xv 2. bash -x shellName 3.如果只想调试其中几行脚本的话可以用 set -x 和 set +x 把要调试的部分包含进来: 比如: ...
- Simple Mail Transfer Protocol --- SMTP协议
https://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol Simple Mail Transfer Protocol
- Struts2之Action三种接收参数形式与简单的表单验证
有了前几篇的基础,相信大家对于Struts2已经有了一个很不错的认识,本篇我将为大家介绍一些关于Action接收参数的三种形式,以及简单的表单验证实现,下面进入正题,首先我们一起先来了解一下最基本的A ...
- for,for-each,for-in,for-of,map的比较
参考: 全面解析JavaScript里的循环方法之forEach,for-in,for-of Iterator 和 for...of 循环 JavaScript Array 对象 常规for for循 ...
- js深浅copy
...点copy是浅拷贝var obj1 = [1,{a: 1}];//var obj2 = Object.assign( {}, obj1);//浅copy//var obj2 = JSON.par ...
- hdu4027Can you answer these queries?【线段树】
A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use ...
- poj1269 intersecting lines【计算几何】
We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a p ...
- Java 多线程(六) synchronized关键字详解
多线程的同步机制对资源进行加锁,使得在同一个时间,只有一个线程可以进行操作,同步用以解决多个线程同时访问时可能出现的问题. 同步机制可以使用synchronized关键字实现. 当synchroniz ...
- veterbi
https://www.zhihu.com/question/20136144 作者:知乎用户链接:https://www.zhihu.com/question/20136144/answer/372 ...
- nginx分区域名转发 tp5域名分目录配置
需求 本来我们一般情况下都是域名abc.com解析到网站的根目录/root/public这种.但是客户突然提出了一个奇葩的需求,客户要求以后可能网站会增多,需要增加分区的功能,比如abc.com/wh ...