LeetCode String Compression
原题链接在这里:https://leetcode.com/problems/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.
题解:
Accumlate the count of repeating chars, if count > 1, append to the char.
Use (""+count).toCharArray() to easily append int as char array.
Time Complexity: O(chars.length). Space: O(1).
AC Java:
class Solution {
public int compress(char[] chars) {
if(chars == null || chars.length == 0){
return 0;
}
int pos = 0;
int count = 0;
int i = 0;
while(i<chars.length){
char cur = chars[i];
while(i<chars.length && chars[i] == cur){
count++;
i++;
}
chars[pos++] = chars[i-1];
if(count > 1){
for(char c : (""+count).toCharArray()){
chars[pos++] = c;
}
}
count = 0;
}
return pos;
}
}
类似Encode and Decode Strings, Count and Say, Design Compressed String Iterator.
LeetCode String Compression的更多相关文章
- [LeetCode] String Compression 字符串压缩
Given an array of characters, compress it in-place. The length after compression must always be smal ...
- 【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_443. String Compression
443. String Compression Easy Given an array of characters, compress it in-place. The length after co ...
- UVA 1351 十三 String Compression
String Compression Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit ...
- 443. String Compression
原题: 443. String Compression 解题: 看到题目就想到用map计数,然后将计数的位数计算处理,这里的解法并不满足题目的额外O(1)的要求,并且只是返回了结果array的长度,并 ...
- CF825F String Compression 解题报告
CF825F String Compression 题意 给定一个串s,其中重复出现的子串可以压缩成 "数字+重复的子串" 的形式,数字算长度. 只重复一次的串也要压. 求压缩后的 ...
- 213. String Compression【LintCode java】
Description Implement a method to perform basic string compression using the counts of repeated char ...
- 213. String Compression【easy】
Implement a method to perform basic string compression using the counts of repeated characters. For ...
随机推荐
- laravel中上传图片之后图片的处理
$file=Input::file('file'); if ($file->isValid()){ $entension=$file->getClientOriginalExtension ...
- request.getPathInfo() 方法的作用
request.getPathInfo(); 这个方法返回请求的实际URL相对于请求的serlvet的url的路径.(个人理解.) 比如,有一个Servlet的映射是这样配置的: <servle ...
- 微信小程序单个倒计时效果
var end_time = grouponList.expire_time.replace(/-/g, '/') grouponcountdown(that, end_time) //适用于商品列表 ...
- MySQL20个经典面试题
MySQL20个经典面试题 Part2:经典题目 1.MySQL的复制原理以及流程 基本原理流程,3个线程以及之间的关联: 2.MySQL中myisam与innodb的区别,至少5点 (1).问5点不 ...
- String类的编码和解码问题
我们前面知道同一个字符在利用不同的编码表得到的结果一般是不一样的. 这里讨论个字符串的编码和解码问题 字符串的一些方法: String(byte[] b,Charset charset); Strin ...
- Linux设备驱动——简单的字符驱动
本文介绍Linux字符设备的静态注册方法, 其中涉及到的模块加载,不了解的可以先参考 构建和运行模块 1. 还是线上源代码: //memdev.h #ifndef _MEMDEV_H_ #define ...
- The capacitive screen technology - tadpole
- vscode 实用插件
1.Auto Rename Tag,非常实用!要修改标签名称的时候自动修改结束标签,节省一半时间,提升效率,非常棒! 2.vscode-icon,这款必须要推荐,明显提升效率的小插件,在项目文件多类型 ...
- etl工具-Bireme
前段时间做数据仓库项目,自己实现了一部分etl功能,后面一直没有时间去深入挖掘.这个工具貌似不错,写个帖子做下记录: https://hashdatainc.github.io/bireme/READ ...
- ThinkPHP 连接数据库
今天在配置连接Mysql 时出现了bool(false)的提示,仔细修改了mysql的密码,还是不对,发现问题应该数据库本身设置了前缀tb_本身的拼写错误和注释掉了 'DB_PREFIX'=>' ...