Description

Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa would become a2b1c5a3.

If the "compressed" string would not become smaller than the original string, your method should return the original string.

You can assume the string has only upper and lower case letters (a-z).

Example

str=aabcccccaaa return a2b1c5a3
str=aabbcc return aabbcc
str=aaaa return a4

解题:字符串压缩问题。用两个临时变量保存字符,一个变量count来计数,pre保存前一个字符,p保存当前字符,如果p不等于pre,将pre和count连到结果上去,并且跟新pre为p,一次循环即可。另外注意对最后一个(或几个相同的)字符进行处理。

public class Solution {
/**
* @param str: a string
* @return: a compressed string
*/
public String compress(String str) {
// write your code here
if(str == null)
return null;
String res = "";
int count = 0;
char pre = (byte)0;//前一个字符
char p = (byte)0;//当前字符
for(int i = 0; i < str.length(); i++){
if(pre == 0 && p == 0){
//初始化
pre = str.charAt(i);
p = str.charAt(i);
count++;
continue;//下一轮循环
}
//不是开头字符
p = str.charAt(i);
if(p == pre){
count++;
}else{
res = res + pre + String.valueOf(count);
pre = p;
count = 1;
}
}
//循环结束,处理最后一个(类)字符
res = res + pre + String.valueOf(count); if(res.length() < str.length()){
return res;
}else{
return str;
}
}
}

213. String Compression【LintCode java】的更多相关文章

  1. 213. String Compression【easy】

    Implement a method to perform basic string compression using the counts of repeated characters. For ...

  2. 423. Valid Parentheses【LintCode java】

    Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine ...

  3. 422. Length of Last Word【LintCode java】

    Description Given a string s consists of upper/lower-case alphabets and empty space characters ' ', ...

  4. 420. Count and Say【LintCode java】

    Description The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, ...

  5. 415. Valid Palindrome【LintCode java】

    Description Given a string, determine if it is a palindrome, considering only alphanumeric character ...

  6. 408. Add Binary【LintCode java】

    Description Given two binary strings, return their sum (also a binary string). Example a = 11 b = 1 ...

  7. 372. Delete Node in a Linked List【LintCode java】

    Description Implement an algorithm to delete a node in the middle of a singly linked list, given onl ...

  8. 451. Swap Nodes in Pairs【LintCode java】

    Description Given a linked list, swap every two adjacent nodes and return its head. Example Given 1- ...

  9. 445. Cosine Similarity【LintCode java】

    Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...

随机推荐

  1. 【UVA11806 Cheerleaders】 题解

    题目链接:https://www.luogu.org/problemnew/show/UVA11806 容斥原理+组合数 正着找合♂fa的不好找,那就用总方案数-不合♂fa的 #include < ...

  2. oracle本地安装注意事项

    这两天组员在本地windows上安装oracle数据库,安装完各种问题,pl/sql developer以及tns_admin配置以及tnsnames.ora和sqlnet.ora listener. ...

  3. Zabbix——使用邮件报警

    前提条件: 1. Zabbix版本4.0 zabbix-server 命令配置: yum install mailx -y #下载邮件功能 vi /etc/mail.rc set bsdcompat ...

  4. linux 安装 node 环境

    本篇学习的分享主要说在linux 安装 node 环境,个人也是在腾讯云的实验室课程学习的,这里只是个人的一个学习记录, 大家也可以去腾讯的实验室来体验一下,教程十分详细易学. 1 .安装 Node. ...

  5. RAC初体验(环境搭建)

      实施阶段: 1.主机配置 2.安装Clusterware 3.安装Oracle Database 4.配置Listener 5.创建ASM 6.创建Database 一.主机配置 1.网络设置 I ...

  6. Python豆瓣源

    pip install -i https://pypi.doubanio.com/simple/ xxxx

  7. js中定时器使用方法经验总结

    前言,最近在做一个音频播放项目的时候,碰到播放时间精度的问题,捣鼓了几天,最终巧妙的运用定时器去降低了错误发生频率 正题,下面是对定时器的使用总结,如有错误之处,请读者加以纠正. 延迟执行(1次) s ...

  8. JSON字符串与JS对象格式转换

    JSON通常用于服务器向客户端传送数据,传回来的JSON数据是字符串的形式,所以要转变为JS对象形式才方便我们使用. JSON字符串转变为JS对象:JSON.parse( ); JS对象转变为JSON ...

  9. 『Linux基础 - 4 』linux常用命令(1)

    这篇笔记包含以下知识点: 几个概念的理解:Linux命令,控制台,终端, 终端提示符 对文件目录的操作的相关命令: 切换目录,列出目录下的文件等 对文件的操作的相关命令: 创建,删除,复制,修改,移动 ...

  10. select epoll poll

    如何理解 Epoll select 和 poll 三种模型,能否用生活中的例子做比喻? 比如说你从某宝下单买了几个东西,这几个东西分别由N个快递员分别给你送过来.在某一时刻,你开始等快递.对于sele ...