【LeetCode】443. String Compression 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/string-compression/description/
题目描述
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.
题目大意
统计每个字符出现的次数,然后放到原地,需要按照顺序放。完成了字符串的压缩。
解题方法
使用额外空间
自己的方法比较简单粗暴,用了额外的空间来保存了所有的数字出现的次数,最后再放回到chars上。
class Solution(object):
def compress(self, chars):
"""
:type chars: List[str]
:rtype: int
"""
marks = ""
length = -1
cur = chars[0]
for i, value in enumerate(chars):
length += 1
if value != cur:
count = str(length) if length != 1 else ''
marks += cur + count
cur = value
length = 0
if i == len(chars) - 1:
length += 1
count = str(length) if length != 1 else ''
marks += cur + count
cur = value
length = 0
print marks
for i, mark in enumerate(marks):
chars[i] = mark
return len(marks)
不使用额外空间
保存一个pos位置,告诉我们当前需要放在哪个地方。然后我们统计连续的字符出现了多少次,如果大于1次才往后拼接上去。
class Solution(object):
def compress(self, chars):
"""
:type chars: List[str]
:rtype: int
"""
pre = chars[0]
count = 0
pos = 0
for ch in chars:
if pre == ch:
count += 1
else:
chars[pos] = pre
pos += 1
if count > 1:
count = str(count)
for i in range(len(count)):
chars[pos] = count[i]
pos += 1
count = 1
pre = ch
chars[pos] = pre
pos += 1
if count > 1:
count = str(count)
for i in range(len(count)):
chars[pos] = count[i]
pos += 1
return pos
日期
2018 年 1 月 27 日
2018 年 11 月 24 日 —— 周六快乐
【LeetCode】443. String Compression 解题报告(Python)的更多相关文章
- CF825F String Compression 解题报告
CF825F String Compression 题意 给定一个串s,其中重复出现的子串可以压缩成 "数字+重复的子串" 的形式,数字算长度. 只重复一次的串也要压. 求压缩后的 ...
- LeetCode 443. String Compression (压缩字符串)
题目标签:String 这一题需要3个pointers: anchor:标记下一个需要存入的char read:找到下一个不同的char write:标记需要存入的位置 让 read指针 去找到下一个 ...
- leetcode 443. String Compression
下面反向遍历,还是正向好. void left(vector<char>& v, bool p(int)) { ; ; ; while (del < max_index) { ...
- 443. String Compression
原题: 443. String Compression 解题: 看到题目就想到用map计数,然后将计数的位数计算处理,这里的解法并不满足题目的额外O(1)的要求,并且只是返回了结果array的长度,并 ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- 【leetcode】443. String Compression
problem 443. String Compression Input ["a","a","b","b"," ...
- 443. String Compression - LeetCode
Question 443. String Compression Solution 题目大意:把一个有序数组压缩, 思路:遍历数组 Java实现: public int compress(char[] ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
随机推荐
- Dango之form校验组件
目录 1.引入案例 2. form组件的功能 3. form组件的使用 3.1 自定义form校验类 3.2 校验数据 3.3 渲染页面 3.4 展示错误信息 3.5 自定义校验结果 3.6 form ...
- 51-Intersection of Two Linked Lists
Intersection of Two Linked Lists My Submissions QuestionEditorial Solution Total Accepted: 72580 Tot ...
- Linux文件系统属性和权限概念详解(包含inode、block、文件权限、文件软硬链接等)
Linux中的文件属性 ls -lih 包括:索引节点(inode),文件类型,权限属性,硬链接数,所归属的用户和用户组,文件大小,最近修改时间,文件名等等 索引节点:相当于身份证号,系统唯一,系统读 ...
- Prometheus_exporter安装与使用
Promethues概述:可以看一下更详细的介绍,以下为转载的博客,原文链接,支持原创,请多多支持!!:闫世成的博客园 Prometheus-node-exporter 1.简介: 内核公开的硬件和操 ...
- [云原生]Docker - 安装&卸载
目录 系统要求 卸载旧版本 安装Docker 方法一:通过repo安装 设置Repository 安装Docker Engine 升级Docker Engine 方法二:通过package安装 方法三 ...
- Shell脚本的条件控制和循环语句
条件判断:if语句 语法格式: if [ expression ] then Statement(s) to be executed if expression is true fi 注意:expre ...
- [学习总结]2、android中的VelocityTracker(获得速率用的类)
参考资料:http://blog.jrj.com.cn/4586793646,5298605a.html 感谢这位兄弟! android.view.VelocityTracker主要用跟踪触摸屏事件( ...
- CentOs 7 yum 安装Nginx
打开官网下载文档:http://nginx.org/en/download.html 2进入操作系统 centOs 7,建立文件夹 nginx ,进入nginx ,拷贝 上图1编辑命令:/etc/yu ...
- 什么是maven(二)
转自博主--一杯凉茶 maven项目构建ssh工程(父工程与子模块的拆分与聚合) 前一节我们明白了maven是个什么玩意,这一节就来讲讲他的一个重要的应用场景,也就是通过maven将一个ssh项目 ...
- 可扩展标记语言XML(淅淅沥沥的小雨)
XML简述 XML用于描述数据,是当前处理结构化文档信息的有力工具.与操作系统编程语言的开发平台无关,可以实现不同系统之间的数据交互. xml文件结构: 1 <?xml version=&quo ...