[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 ...
随机推荐
- 三十五、在SAP中定义选择屏幕,设置选择范围
一.代码如下,有2个地方需要注意,一个是SELECT-OPTIONS,还有一个是IN的使用 二.我们定义一下选择文本 三.我们运行程序 四.输出 五.当然,选择的时候,我们也可以用其他的方式,如下图
- 039-PHP使用闭包函数来进行父实例的变量自增,错误示例
<?php // 如何使用闭包函数来进行父实例的变量自增,错误示例 function demo(){ $num = 1; $func = function() use($num){ echo $ ...
- ubuntu12.04安装JDK8
系统里已经有jdk1.6,下载并解压jdk后,按照网上的教程(bash.bashrc)没成功. sudo update-alternatives --install /usr/bin/java jav ...
- 远程控制使用kill软件映射内网进行远程控制(9.28 第十四天)
1.能ping通IP情况下远程控制 设置kill软件中的端口.密码.上线列表 2.在软件的Bin\Plugins目录下找到Consys21.dll复制到/phpstudy/www目录下留作生成软件 3 ...
- linux下的 sudo ln -s 源文件 目标文件
这是linux中一个非常重要命令,请大家一定要熟悉.它的功能是为某一个文件或目录在另外一个位置建立一个同步的链接,类似Windows下的超级链接. 这个命令最常用的参数是-s,具体用法是:sudo l ...
- POJ 1426:Find The Multiple
Find The Multiple Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u Su ...
- C++ 内存映射
HANDLE hFile = NULL;HANDLE hFileMap = NULL;const viewmapsize = 8 * 1024 * 1024;//8mDWORD highsize,lo ...
- ORACLE 将一个库的部分值带条件插入到另外一个库
将一个表插入另外一个表,两种方法: 1.insert into table1 select * from table2 ; 或者2.create table1 as select * from tab ...
- Activity LauchMode设置
lauchMode: standard: 标准模式,每次调用startActivity()方法就会产生一个新的实例. singleTop: 如果已经有一个实例位于Activit ...
- [CISCN2019 总决赛 Day2 Web1]Easyweb
0x00 知识点 1:备份文件泄露 2:SQL注入 3:php短标签 短标签<? ?>需要php.ini开启short_open_tag = On,但<?= ?>不受该条控制. ...