题目如下:

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.

解题思路:从头到尾遍历数组,记录连续字符的个数,然后插入数组前部,注意每次插入要记录当前的偏移量offset 。

代码如下:

class Solution(object):def compress(self, chars):""":type chars: List[str]:rtype: int"""lastChar = Nonecount = 0inx = 0offset = 0chars.append('END') # terminatorwhile inx < len(chars):i = chars[inx]if lastChar == None:lastChar = icount = 1elif lastChar == i:count += 1else:lastOff = offsetchars.insert(offset,lastChar)offset += 1if count != 1:count = str(count)for j in count:chars.insert(offset, j)offset += 1lastChar = icount = 1inx += (offset - lastOff)inx += 1#print charsdel chars[-1]return offset

转载于:https://www.cnblogs.com/seyjs/p/9275563.html

【leetcode】443. String Compression相关推荐

  1. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

  2. 【Leetcode】984. String Without AAA or BBB(配数学证明)

    题目地址: https://leetcode.com/problems/string-without-aaa-or-bbb/ 给定两个数xxx和yyy,都是非负整数,要求返回任意一个字符串,其含xxx ...

  3. 【Leetcode】79.单词搜索

    题目 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格 ...

  4. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  5. 【LeetCode】【HOT】39. 组合总和(回溯)

    [LeetCode][HOT]39. 组合总和 文章目录 [LeetCode][HOT]39. 组合总和 package hot;import java.util.ArrayList; import ...

  6. 【LeetCode】【HOT】31. 下一个排列

    [LeetCode][HOT]31. 下一个排列 文章目录 [LeetCode][HOT]31. 下一个排列 package hot;import java.util.Arrays;public cl ...

  7. 【LeetCode】【HOT】347. 前 K 个高频元素(哈希表+优先队列)

    [LeetCode][HOT]347. 前 K 个高频元素 文章目录 [LeetCode][HOT]347. 前 K 个高频元素 package hot;import java.util.Arrays ...

  8. 【LeetCode】【HOT】239. 滑动窗口最大值(双向队列)

    [LeetCode][HOT]239. 滑动窗口最大值 文章目录 [LeetCode][HOT]239. 滑动窗口最大值 package hot;import java.util.Arrays; im ...

  9. 【LeetCode】【HOT】215. 数组中的第K个最大元素(优先队列)

    [LeetCode][HOT]215. 数组中的第K个最大元素 文章目录 [LeetCode][HOT]215. 数组中的第K个最大元素 package hot;import java.util.Co ...

最新文章

  1. 利用SQL索引提高查询速度
  2. 收银系统服务器数据库,收银系统服务器数据库
  3. 天梯赛 L1-027 出租 (20 分)
  4. linux切换root权限
  5. Android 自定义的开关按钮——SwitchButton
  6. ​嵌入式开发为什么选择C语言?
  7. git 部分常用命令记录
  8. VB 窗体实现文件拖拽获取路径方法
  9. 列车运行图的编制原则是什么_我国农村将有序推进村庄规划编制
  10. 程序员谈如何掌握计算机专业英语
  11. 一款APP其实就是各种SDK的集合体
  12. android 屏幕分辨率 更改
  13. 股票分析软件 php,哪个股票分析软件最好用?
  14. 软件测试第三章课后习题
  15. Docker安装(有网环境下) 最新版docker-ce安装教程
  16. java对MP4视频编码转换为H264格式解决浏览器播放无画面问题
  17. 安卓调用百度地图服务
  18. “云适配”获1亿元B+轮融资,盯上了大企业的移动化需求
  19. excel拼接换行符:char(10)
  20. MT6735手动修改屏幕分辨率

热门文章

  1. 从零开始编写自己的C#框架(18)——Web层后端权限模块——菜单管理
  2. codebrag审核代码工具安装配置
  3. OAF_VO系列1 - Accelerator Keys
  4. redhat linux加密卷
  5. 2011年度十大杰出IT博客获奖感言
  6. 网站CSS样式不起作用,或只有一部分起作用?随手记
  7. c# cookie帮助类
  8. 02_python是一种什么语言?
  9. 51Nod 1013 3的幂的和 快速幂 | 乘法逆元 | 递归求和公式
  10. 转:WEB前端性能优化规则