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 ...
随机推荐
- 如何用javasript对Gridview的项目进行汇总统计?
当我们在gridview显示统计信息时,都会想在gridview最后一行显示[小计]结果,但gridview的话好像比较难搞(至少我也不会呀 囧~),那么我就结合jquery写了一个解决方案,下面举个 ...
- el表达式不显示值
1.场景是自己搭建一个ssm的项目,登录页面跳转到首页,首页显示登录用户的信息,用request传递的值,用el表达式在jsp页面中没有显示 2.解决办法 早jsp的代码中添加头<%@ page ...
- http://imgbase64.duoshitong.com/ 图片转换 base64
base64图片工具介绍: 1.支持 PNG.GIF.JPG.BMP.ICO 格式. 2.将图片转换为Base64编码,可以让你很方便地在没有上传文件的条件下将图片插入其它的网页.编辑器中. 这对于一 ...
- Linux - CentOS 7 yum方式快速安装MongoDB
一.安装环境及配置yum # more /etc/redhat-release CentOS Linux release 7.2.1511 (Core) # vi /etc/yum.repos.d/m ...
- 复习宝典之Maven项目管理
查看更多宝典,请点击<金三银四,你的专属面试宝典> 第二章:Maven项目管理 Maven是基于项目对象模型(POM project object model),可以通过一小段描述信息(配 ...
- Mysql存储引擎myisam与inndb的区别?
最近在研究Mysql存储引擎这块,说白了就是如何存储数据.如何为存储的数据建立索引和如何更新.查询数据等技术的实现方法,在此做一个大概总结: 其实在工作中用的最多也就是MYISAM和INNODB,IN ...
- Jquery中复选框选中取消实现文本框的显示隐藏
标签内容 <div class="box"> 请编写javascript代码,完成如下功能要求:<br /> 1.取消复选款后,要求促销价格.促销开始结束日 ...
- 为什么后台返回的日期我输出处理了在苹果手机里显示NAN?
现象: //结束时间var ent_time ="2018-04-28 09:36:00"alert((Date.parse(new Date(ent_time))));/ ...
- PHP DES加密解密
自定义密码加密解密函数,源自网友,记录保存一下. <?php /** * DES加密解密 */ class Mcrypt{ public function __construct(){} fun ...
- TortoiseGit —— 配置密钥
TortoiseGit 使用扩展名为ppk的密钥,而不是ssh-keygen生成的rsa密钥.使用命令ssh-keygen -C "邮箱地址" -t rsa产生的密钥在Tortoi ...