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 ...
随机推荐
- POJ 3984 迷宫问题(简单bfs+路径打印)
传送门: http://poj.org/problem?id=3984 迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- C# 5.0中使用CallerMemberName、CallerFilePath和CallerLineNumber获取代码的调用方信息(转载)
很多时候,我们需要在运行过程中记录一些调测的日志信息,如下所示: public void DoProcessing() { TraceMessage("DoProcessing()被XXX调 ...
- Django全面讲解(2/2)
前戏 Django是Python语言编写的一个全栈式Web框架(其他的还有Tornado,Flask),可帮助我们快速编写一个具有数据库功能,增删改查.后台管理等功能的网站,若不考虑很高的执行速度,去 ...
- RockBrain USB Server- 云计算虚拟化USB设备集中管理、远程共享解决方案(涉及银企直联)
RockBrain USB Server- 云计算虚拟化USB设备集中管理.远程共享解决方案(涉及银企直联) 技术需求: 1.企业员工的大量USB Key,需要将key接入USB Server虚拟池, ...
- 使用 Solr 构建企业级搜索服务器
最近因项目需要一个全文搜索引擎服务, 在考察了Lucene及Solr后,我们选择了Solr. 本文简要记录了基于Solr搭建一个企业搜索服务器的过程.网上的资料太多千篇一律,也可能版本不同,总之在参照 ...
- MySql服务未知原因消失了的解决办法
一: 5.1及后续版本: 打开计算机命令窗口,切换至mysql安装目录,然后运行bin目录下的mysqld.exe,命令如下: D:\MySQL\bin>mysqld.exe -installS ...
- Vue--- 一点车项目
一点车项目 cli脚手架 + 组件化 +数据交互+路由指向+存入数据库 前端页面 cli脚手架的安装与搭建 创建对应包 页面组件化编辑 (共享组件:摘取出来一模一样的组件重用)(私有组件:在自 ...
- 简易坦克大战python版
#! /usr/bin/env python # -*- coding:utf8 -*- ''' *author:wasua *purpose:学习python语言,其中的类以及pygame应用 ...
- RHEL6(RedHat6)和SUSE11系统配置IPV6地址
临时生效 RHEL6和SUSE11系统临时配置IPv6地址操作是一样的,比如添加如下ipv6地址. ip - addr add ::A/ dev eth2 ip - route add default ...
- Eclipse报The default workspace'xxxxx' is in use or cannot be created Pl
原因:出现这种情况一般是workspace的配置文件中出现了.lock文件(workspace/.metadata/.lock),锁定了workspace.把.lock文件删除即可.如果该文件不能删除 ...