213. String Compression【LintCode java】
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】的更多相关文章
- 213. String Compression【easy】
Implement a method to perform basic string compression using the counts of repeated characters. For ...
- 423. Valid Parentheses【LintCode java】
Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine ...
- 422. Length of Last Word【LintCode java】
Description Given a string s consists of upper/lower-case alphabets and empty space characters ' ', ...
- 420. Count and Say【LintCode java】
Description The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, ...
- 415. Valid Palindrome【LintCode java】
Description Given a string, determine if it is a palindrome, considering only alphanumeric character ...
- 408. Add Binary【LintCode java】
Description Given two binary strings, return their sum (also a binary string). Example a = 11 b = 1 ...
- 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 ...
- 451. Swap Nodes in Pairs【LintCode java】
Description Given a linked list, swap every two adjacent nodes and return its head. Example Given 1- ...
- 445. Cosine Similarity【LintCode java】
Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...
随机推荐
- STC12LE5620AD RAM问题
1.此款单片机内部有 sram:768B=512B(aux)+256B(Internal) 2.内部RAM解析 2. 3.内部扩展RAM 4.keil中可以选择内存类型 5. 网上摘抄的一段话: 在S ...
- MyEclipse 根据左括号或右括号查找另外一半
在MyEclipse 中如果代码嵌套太多,查找括号是一件非常头疼的事情,今天突然发现了一个快捷键(如下)可以直接定位到另外一半的位置 Ctrl+Shift+P,光标会自动跳到相应的花括号位置, 并且可 ...
- git详细使用教程入门到精通(史上最全的git教程)
Git是分布式版本控制系统,那么它就没有中央服务器的,每个人的电脑就是一个完整的版本库,这样,工作的时候就不 需要联网了,因为版本都是在自己的电脑上.既然每个人的电脑都有一个完整的版本库,那多个人如何 ...
- Oracle查找lobsegment、lobindex对应的表
在查看表空间的使用情况的时候,发现有几个LOBSEGMENT.LOBINDEX类型的对象占用了大量的空间.于是想找出那些表占用了大量的空间,以便于清理. Oracle对BLOB类型的定义 ...
- Struts2-01
一.Struts2的介绍 讲Struts2框架之前,我们需要知道框架是什么呢?估计大多数初学者都只知道其名却不知其意,框架就是一个半成品,别人将一些功能已经写好了,我们只需要拿来用即可,像我们之前使用 ...
- c#数据库访问服务(综合数据库操作)
前面给大家说封装了常用的数据库,并且整理了使用.最近我再次把项目整合了.做成比较完善的服务. 还是重复的说下数据库操作封装. berkeley db数据库,Redis数据库,sqlite数据库. 每个 ...
- LeetCode 中级 - 救生艇(105)
第 i 个人的体重为 people[i],每艘船可以承载的最大重量为 limit. 每艘船最多可同时载两人,但条件是这些人的重量之和最多为 limit. 返回载到每一个人所需的最小船数.(保证每个人都 ...
- hdu_4465_Candy
LazyChild is a lazy child who likes candy very much. Despite being very young, he has two large cand ...
- DBUtils 学习使用
DBUtils 学习使用 commons-dbutils简介 commons-dbutils是Apache组织提供的一个开源JDBC工具类库,它是对JDBC的简单封装,学习成本极低,并且使用dbuti ...
- html+css3 实现各种loading效果
效果见下图 代码: 建议直接去本人github上浏览代码 https://github.com/wuliqiangqiang/loading <!DOCTYPE html> <htm ...