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. 一个jquery ajax例子

    上次搞了个jquery的AutoComplete效果,感觉很久没写jquery了,趁热打铁,再找点东西练练手.这不,看了一下jquery手册,顺便写了一个小例子,源码我直接贴上来了.   1.新建一个 ...

  2. HDU 2897 邂逅明下 ( bash 博弈变形

    HDU 2897 邂逅明下 ( bash 博弈变形 题目大意 有三个数字n,p,q,表示一堆硬币一共有n枚,从这个硬币堆里取硬币,一次最少取p枚,最多q枚,如果剩下少于p枚就要一次取完.两人轮流取,直 ...

  3. ARM MDK 编译产生:RO、RW和ZI DATA说明

    1.比如编译一个工程文件,产生如下提示信息: Program Size: Code=18938 RO-data=622 RW-data=124 ZI-data=7724 RO段.RW段和ZI段 要了解 ...

  4. Python 学习笔记(九)Python元组和字典(一)

    Python 元组 元组的定义  元组(tuple)是一种Python对象类型,元组也是一种序列 Python中的元组与列表类似,不同之处元组的元素不能修改 元组使用小括号,列表使用方括号 元组的创建 ...

  5. 通过ES6写法去对Redux部分源码解读

    在Redux源码中主要有四个文件createStore,applyMiddleware,bindActionCreators,combineRedures createStore.js export ...

  6. chromium之ScopedNSAutoreleasePool浅析

    上代码,看看注释 ScopedNSAutoreleasePool只有Mac系统特有的,也可以理解为OC特有的函数, 其他系统为空实现   // On the Mac, ScopedNSAutorele ...

  7. 转:Java并发集合

    引自:http://ifeve.com/concurrent-collections-1/ 并发集合(一)引言 声明:本文是< Java 7 Concurrency Cookbook>的第 ...

  8. Redis(五):Redis的持久化

    Redis的持久化目录导航: 总体介绍 RDB(Redis DataBase) AOF(Append Only File) 总结(Which one) 总体介绍 官网介绍 RDB(Redis Data ...

  9. mysql的docker化安装

    mysql版本有很多,先看下各类版本号说明: 3.X至5.1.X:这是早期MySQL的版本.常见早期的版本有:4.1.7.5.0.56等. 5.4.X到5.7.X:这是为了整合MySQL AB公司社区 ...

  10. sea.js模块化工具

    sea.js 一. sea.js向全局中引入了两个变量seajs.define: 1.seajs用加载文件 seajs.use(deps,callback)异步引入入口模块 路径要以sea.js文件所 ...