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 ...
随机推荐
- angular2 自定义双向绑定属性
import { Component, OnInit, Output, Input, EventEmitter } from '@angular/core'; @Component({ selecto ...
- BOM(Browser Object Model) 浏览器对象模型
JavaScript 实现是由 3 个部分组成:核心(ECMAScript),文档对象模型(DOM),浏览器对象模型(BOM) BOM(Browser Object Model) 浏览器对象模型BOM ...
- Back Track5学习笔记
1.BT5默认用户名:root.密码:toor(公司是yeslabccies) 2.进入图形化界面命令:startx 3.更改密码:sudo passwd root 扫描工具 第一部分网络配置: 4. ...
- 三重Des对称加密在Android、Ios 和Java 平台的实现
引言 如今手机app五彩缤纷,确保手机用户的数据安全是开发人员必须掌握的技巧,下面通过实例介绍DES在android.ios.java平台的使用方法: DES加密是目前最常用的对称加密方式, ...
- Ansible 小手册系列 十二(Facts)
Facts 是用来采集目标系统信息的,具体是用setup模块来采集得. 使用setup模块来获取目标系统信息 ansible hostname -m setup 仅显示与ansible相关的内存信息 ...
- 【问题解决记录】无法识别的标志“-sdlMode”,在“p2”中
本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/ 这是本人第一次使用MarkDown编辑器,试试看效果-w-,下面是正文: 一.报错原因 昨 ...
- guava学习--cache
转载:http://outofmemory.cn/java/guava/cache/how-to-use-guava-cache http://www.cnblogs.com/parryyang/ ...
- laravel中单独获取一个错误信息的方法
获取单独的错误信息的方法
- spring --解析自定义注解SpringAOP(配合@Aspect)
1:首先,声明自定义注解 @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) public @interface DtT ...
- LeetCode OJ:ZigZag Conversion(字符串的Z字型转换)
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...