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.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
在数组中输入一个1 2时,需要start i同时变
[一句话思路]:
往前走多长,然后截断、更新,属于 前向窗口类:end边统计往后走到哪,start边统计可以更新到哪
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
String.valueOf 函数可以将数字转化成字符串。不是.toString,这是针对已有的字符串
- 只有count != 1时才需要添加,读题时就要备注特殊条件
[二刷]:
- 用等号表示的最末尾一位是n - 1,也需要备注
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
字符型数组应写成char[], 不是chars[]
[总结]:
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[关键模板化代码]:
for 循环+ j 满足条件时移动
for (int end = 0, count = 0; end < chars.length; end++)
{
count++;
//change if neccessary
if (end == chars.length - 1 || chars[end] != chars[end + 1]) {
chars[start] = chars[end];
start++;
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
Encode and Decode Strings 用各种类的设计,有基础就还行
[代码风格] :
写框架时只能把for的重要事项写好,具体换行还是需要自己理解
class Solution {
public int compress(char[] chars) {
//cc
if (chars.length == 0) {
return 0;
}
//ini
int start = 0; for (int end = 0, count = 0; end < chars.length; end++)
{
count++;
//change if neccessary
if (end == chars.length - 1 || chars[end] != chars[end + 1]) {
chars[start] = chars[end];
start++; //add to only if (count != 1)
if (count != 1) {
char[] arrs = String.valueOf(count).toCharArray();
for (int i = 0; i < arrs.length; i++, start++) {
chars[start] = arrs[i];
}
}
//reset count
count = 0;
}
} //return start;
return start;
}
}
443. String Compression字符串压缩的更多相关文章
- [LeetCode] String Compression 字符串压缩
Given an array of characters, compress it in-place. The length after compression must always be smal ...
- 443. String Compression - LeetCode
Question 443. String Compression Solution 题目大意:把一个有序数组压缩, 思路:遍历数组 Java实现: public int compress(char[] ...
- 【leetcode】443. String Compression
problem 443. String Compression Input ["a","a","b","b"," ...
- 443. String Compression
原题: 443. String Compression 解题: 看到题目就想到用map计数,然后将计数的位数计算处理,这里的解法并不满足题目的额外O(1)的要求,并且只是返回了结果array的长度,并 ...
- 443 String Compression 压缩字符串
给定一组字符,使用原地算法将其压缩.压缩后的长度必须始终小于或等于原数组长度.数组的每个元素应该是长度为1 的字符(不是 int 整数类型).在完成原地修改输入数组后,返回数组的新长度.进阶:你能否仅 ...
- LeetCode 443. String Compression (压缩字符串)
题目标签:String 这一题需要3个pointers: anchor:标记下一个需要存入的char read:找到下一个不同的char write:标记需要存入的位置 让 read指针 去找到下一个 ...
- 【LeetCode】443. String Compression 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用额外空间 不使用额外空间 日期 题目地址:htt ...
- leetcode 443. String Compression
下面反向遍历,还是正向好. void left(vector<char>& v, bool p(int)) { ; ; ; while (del < max_index) { ...
- [LC] 443. String Compression
Given an array of characters, compress it in-place. The length after compression must always be smal ...
随机推荐
- 【解题报告】牡丹江现场赛之ABDIK ZOJ 3819 3820 3822 3827 3829
那天在机房做的同步赛,比现场赛要慢了一小时开始,直播那边已经可以看到榜了,所以上来就知道A和I是水题,当时机房电脑出了点问题,就慢了好几分钟,12分钟才A掉第一题... A.Average Score ...
- apt-get指令的autoclean,clean,autoremove的区别
apt-get使用各用于处理apt包的公用程序集,我们可以用它来在线安装.卸载和升级软件包等,下面列出一些apt-get包含的常用的一些工具: 工具 说明 install 其后加上软件包名,用于安装一 ...
- 两种方式创建Maven项目【方式一】
经常使用maven进行项目的管理,今天整理两种方式创建maven项目及创建过程中碰到的问题怎么解决: 方式一: 1.新建maven项目,点击下一步. 2.勾选Create a simple proje ...
- Python 函数 -next()
next() next() 返回迭代器的下一个项目. iterator - 可迭代对象. default - 可选,用于设置在没有下一个元素时返回该默认值,如果不设置,又没有下一个元素则会触发 St ...
- PCB 锣板和半孔工艺的差别
PCB 锣板和半孔工艺的差别 PCB 在做模块时会用到半孔工艺,但是由于半孔是特殊工艺. 需要加费用,打板时费还不低. 下面这个图是锣板和半孔工艺的差别. https://www.amobbs.com ...
- php执行多个存储过程(二)
使用公共连接文件形式 include_once 'inc/conni.php'; $result = mysqli_query($conni, "call proc_test()" ...
- PS相关技术
PS相关长时间不用就忘记了,做个笔记,记录下来 (1)复制图层,可以将图层复制到另外的图层里去,这样,多个图层就可以编辑了 (2)通过建立选区,可以选择右键,通过剪切的图层,通过复制的图层将图片抠出来 ...
- 分布式缓存系统 Memcached CAS协议
Memcached在1.2.4版本后新增了CAS(Check and Set)协议,主要用于并发控制:memcached中同一个item同时被多个线程(多个客户端)更改的并发问题.CAS协议最本质的东 ...
- 比赛安排(穷举法或dfs)
比赛安排 时间限制: 1 Sec 内存限制: 125 MB提交: 11 解决: 10[提交][状态][讨论版][命题人:外部导入] 题目描述 设有2n(n<=6)个球队进行单循环比赛,计划在 ...
- oracle 11g r2 rac +openfiler 2.99 +centos 6.5+vbox
继上篇openfiler 2.99安装之后,这一篇讲介绍openfiler的存储配置和oracle 端的服务配置 参考文档:https://www.oracle.com/technetwork/cn/ ...