[Algo] 611. Compress String II
Given a string, replace adjacent, repeated characters with the character followed by the number of repeated occurrences.
Assumptions
The string is not null
The characters used in the original string are guaranteed to be ‘a’ - ‘z’
Examples
“abbcccdeee” → “a1b2c3d1e3”
public class Solution {
public String compress(String input) {
// Write your solution here
char[] charArr = input.toCharArray();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < charArr.length; i++) {
char cur = charArr[i];
int count = 1;
while (i + 1 < charArr.length && charArr[i + 1] == charArr[i]) {
count += 1;
i += 1;
}
sb.append(cur).append(count);
}
return sb.toString();
}
}
[Algo] 611. Compress String II的更多相关文章
- [Algo] 175. Decompress String II
Given a string in compressed form, decompress it to the original string. The adjacent repeated chara ...
- [CareerCup] 1.5 Compress String 压缩字符串
1.5 Implement a method to perform basic string compression using the counts of repeated characters. ...
- [LeetCode] 344 Reverse String && 541 Reverse String II
原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...
- leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String
344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...
- Reverse Words in a String I & Reverse Words in a String II
Reverse Words in a String I Given an input string, reverse the string word by word. For example,Give ...
- leadcode 541. Reverse String II
package leadcode; /** * 541. Reverse String II * Easy * 199 * 575 * * * Given a string and an intege ...
- NYOJ 1067 Compress String(区间dp)
Compress String 时间限制:2000 ms | 内存限制:65535 KB 难度:3 描写叙述 One day,a beautiful girl ask LYH to help he ...
- LeetCode 541. 反转字符串 II(Reverse String II)
541. 反转字符串 II 541. Reverse String II
- 【leetcode_easy】541. Reverse String II
problem 541. Reverse String II 题意: 给定一个字符串,每隔k个字符翻转这k个字符,剩余的小于k个则全部翻转,否则还是只翻转剩余的前k个字符. solution1: cl ...
随机推荐
- ssh: connect to host 120.79.26.164 port 22: Connection timed out报错问题
要是使用阿里云服务器,出现这种错误,一般是端口没有打开.需要在阿里云控制台中设置端口后,即可使用ssh连接.
- Swift 协议protocol
// associatedtype 关键字 用来声明一个类型的占位符作为协议定义的一部分 protocol LXFViewModelType { associatedtype Input associ ...
- linux之 文本编辑 的基础知识点
第一步 打开终端 创建文件命令 touch 文件名.后缀名 打开文件命令 vi 文件名.后缀名 (此时进去txt文件之后为一般模式,你无法对文件进行增删改) 之后按 i 或 a 或o 都 ...
- js对象等号赋值的bug
var a = {n: 1}; var b = a; a.x = a = {n: 2}; console.log(a.x); console.log(b.x); 有道题是这样的,觉得很奇葩,分析一下 ...
- 第一部分 JavaScript语言核心(一)
第二章 词法结构 P25 JavaScript是区分大小写的语言:但HTML不区分大小写(XHTML区分大小写,但浏览器具有强大的纠错能力.) 第三章 类型.变量和值 P32 var(variable ...
- sudo 提权漏洞(CVE-2019-14287)复现 (10.16 第二十二天)
sudo是Linux系统命令,让普通账号以root身份去执行某些命令,比,安装软件.查看某些配置文件.关机.重启等操作,如果普通账号需要使用sudo需要修改配置文件/etc/sudoers,将sudo ...
- P 1033 旧键盘打字
转跳点:
- h5-伸缩布局-小案例
1.伸缩布局案例1-基本页面布局 1.1.html <div class="layout"> <header></header> <mai ...
- MVC学生管理系统-阶段I(显示学生列表)
项目源码 :https://download.csdn.net/download/weixin_44718300/11091042 目录 MVC设计模式 前期准备: NO01:新建一个index.js ...
- bfs--P1301 魔鬼之城
*传送 求最小步数,bfs求解.因为题目要求可以走八个方向(上下左右和对角线),所以两个方位数组来找八个方向 int dirx[9]={0,0,1,1,1,0,-1,-1,-1}; int diry[ ...