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获取select标签的值、文本方式

    <script> function add(){            var prop_name=$("#prop").find("option:selec ...

  2. REST解惑

    本文是「架构风格:你真的懂REST吗?」的补充! REST全称是Representational State Transfer,目前普遍接受的中文翻译为「表述性状态转移」! 即使翻译过来了,你依然有一 ...

  3. HP-UNIX平台修改Oracle processes参数报错:ORA-27154、ORA-27300、ORA-27301、ORA-27302

    OS 版本     :HP-UX B.11.31Oracle版本:11.2.0.4 (RAC) (一)问题描述 最近发现无法连接上数据库,报错信息为“ORA-00020:maximum number ...

  4. 将jquery.qqFace.js表情转换成微信的字符码

    jquery.qqFace.js使用方法 引用 <script src="~/Content/qqFace/js/jquery.qqFace.js?v=3"></ ...

  5. 20181030NOIP模拟赛T2

    WYT的刷子 WYT有一把巨大的刷子,刷子的宽度为M米,现在WYT要使用这把大刷子去粉刷有N列的栅栏(每列宽度都为1米:每列的高度单位也为米,由输入数据给出). 使用刷子的规则是: 1.与地面垂直,从 ...

  6. 【读书笔记 - Effective Java】04. 通过私有构造器强化不可实例化的能力

    工具类(utility class)不希望被实例化,比如只包含静态方法和静态域的类.为了这个目的,需要让这个类包含一个私有构造器. // 私有构造器示例 public class UtilityCla ...

  7. 增强for循环和迭代器

    package example6; import java.util.ArrayList;import java.util.Iterator;import java.util.List; class ...

  8. ajaxSubmit 在ie9或360兼容中,form下是空的

    解决办法:在<head>....</head>中加入<meta http-equiv="X-UA-Compatible" content=" ...

  9. vscode vue 项目保存运行lint进行代码修正

    { "editor.tabSize": 2, "files.associations": { "*.vue": "vue" ...

  10. HTML表格-table

    表格 表格由 <table> 标签来定义. 每个表格均有若干行(由 <tr> 标签定义),每行被分割为若干单元格(由 <td> 标签定义). 字母 td 指表格数据 ...