【leetcode】443. String Compression
题目如下:
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
.
解题思路:从头到尾遍历数组,记录连续字符的个数,然后插入数组前部,注意每次插入要记录当前的偏移量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相关推荐
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
- 【Leetcode】984. String Without AAA or BBB(配数学证明)
题目地址: https://leetcode.com/problems/string-without-aaa-or-bbb/ 给定两个数xxx和yyy,都是非负整数,要求返回任意一个字符串,其含xxx ...
- 【Leetcode】79.单词搜索
题目 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【LeetCode】【HOT】39. 组合总和(回溯)
[LeetCode][HOT]39. 组合总和 文章目录 [LeetCode][HOT]39. 组合总和 package hot;import java.util.ArrayList; import ...
- 【LeetCode】【HOT】31. 下一个排列
[LeetCode][HOT]31. 下一个排列 文章目录 [LeetCode][HOT]31. 下一个排列 package hot;import java.util.Arrays;public cl ...
- 【LeetCode】【HOT】347. 前 K 个高频元素(哈希表+优先队列)
[LeetCode][HOT]347. 前 K 个高频元素 文章目录 [LeetCode][HOT]347. 前 K 个高频元素 package hot;import java.util.Arrays ...
- 【LeetCode】【HOT】239. 滑动窗口最大值(双向队列)
[LeetCode][HOT]239. 滑动窗口最大值 文章目录 [LeetCode][HOT]239. 滑动窗口最大值 package hot;import java.util.Arrays; im ...
- 【LeetCode】【HOT】215. 数组中的第K个最大元素(优先队列)
[LeetCode][HOT]215. 数组中的第K个最大元素 文章目录 [LeetCode][HOT]215. 数组中的第K个最大元素 package hot;import java.util.Co ...
最新文章
- 利用SQL索引提高查询速度
- 收银系统服务器数据库,收银系统服务器数据库
- 天梯赛 L1-027 出租 (20 分)
- linux切换root权限
- Android 自定义的开关按钮——SwitchButton
- ​嵌入式开发为什么选择C语言?
- git 部分常用命令记录
- VB 窗体实现文件拖拽获取路径方法
- 列车运行图的编制原则是什么_我国农村将有序推进村庄规划编制
- 程序员谈如何掌握计算机专业英语
- 一款APP其实就是各种SDK的集合体
- android 屏幕分辨率 更改
- 股票分析软件 php,哪个股票分析软件最好用?
- 软件测试第三章课后习题
- Docker安装(有网环境下) 最新版docker-ce安装教程
- java对MP4视频编码转换为H264格式解决浏览器播放无画面问题
- 安卓调用百度地图服务
- “云适配”获1亿元B+轮融资,盯上了大企业的移动化需求
- excel拼接换行符:char(10)
- MT6735手动修改屏幕分辨率