作者: 负雪明烛
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:

  1. All characters have an ASCII value in [35, 126].
  2. 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)的更多相关文章

  1. CF825F String Compression 解题报告

    CF825F String Compression 题意 给定一个串s,其中重复出现的子串可以压缩成 "数字+重复的子串" 的形式,数字算长度. 只重复一次的串也要压. 求压缩后的 ...

  2. LeetCode 443. String Compression (压缩字符串)

    题目标签:String 这一题需要3个pointers: anchor:标记下一个需要存入的char read:找到下一个不同的char write:标记需要存入的位置 让 read指针 去找到下一个 ...

  3. leetcode 443. String Compression

    下面反向遍历,还是正向好. void left(vector<char>& v, bool p(int)) { ; ; ; while (del < max_index) { ...

  4. 443. String Compression

    原题: 443. String Compression 解题: 看到题目就想到用map计数,然后将计数的位数计算处理,这里的解法并不满足题目的额外O(1)的要求,并且只是返回了结果array的长度,并 ...

  5. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  6. 【leetcode】443. String Compression

    problem 443. String Compression Input ["a","a","b","b"," ...

  7. 443. String Compression - LeetCode

    Question 443. String Compression Solution 题目大意:把一个有序数组压缩, 思路:遍历数组 Java实现: public int compress(char[] ...

  8. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  9. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

随机推荐

  1. R语言中的read.table()

    参考资料:http://www.cnblogs.com/xianghang123/archive/2012/06/06/2538274.html read.table(file, header = F ...

  2. linux命令行快速统计文件(压缩文件)的行数

    统计(文件|压缩文件)的行数 zcat file.gz | sed -n '$='                                         #迅速.直接打印出多少行.-n 取消 ...

  3. 也谈string.Join和StringBuilder的性能比较

    前几天在园子里面看到一篇讲StringBuilder性能的文章.文章里面给出了一个测试用例,比较StringBuilder.AppendJoin和String.Join的性能.根据该测试结果,&quo ...

  4. linux 软链接与查看历史指令

    ln 说明 软连接也叫符号链接,类似于windows里的快捷方式,主要存放了路径. 基本语法 ln -s[原文件或目录][软连接名] 删除软链接 [root@hadoop102 ~]# rm -rf ...

  5. 日常Java(测试 (二柱)修改版)2021/9/22

    题目: 一家软件公司程序员二柱的小孩上了小学二年级,老师让家长每天出30道四则运算题目给小学生做. 二柱一下打印出好多份不同的题目,让孩子做了.老师看了作业之后,对二柱赞许有加.别的老师闻讯, 问二柱 ...

  6. Ubuntu 14.04 升级到 Ubuntu16.04

    Ubuntu 14.04 升级到 Ubuntu16.04 1). 更改source.list 源 (24条消息) Ubuntu16.04 source.list更改源_dylan的博客-CSDN博客_ ...

  7. Linux的小知识

    1. top 命令可以在Linux下查看任务管理器和当前进程使用资源情况. 2. Ctrl+c 即可退出,然后使用 kill+进程号 命令可杀死指定进程 3.在Linux的 /etc/rc.local ...

  8. MyBatis 如何实现流式查询

    基本概念 流式查询指的是查询成功后不是返回一个集合而是返回一个迭代器,应用每次从迭代器取一条查询结果.流式查询的好处是能够降低内存使用. 如果没有流式查询,我们想要从数据库取 1000 万条记录而又没 ...

  9. APK 反编译以及遇到的问题

    APK反编译: https://www.cnblogs.com/geeksongs/p/10864200.html 遇到的问题 https://www.jianshu.com/p/55bf5f688e ...

  10. 【编程思想】【设计模式】【行为模式Behavioral】模板模式Template

    Python转载版 https://github.com/faif/python-patterns/blob/master/behavioral/template.py #!/usr/bin/env ...