Encode Adjacent Letters
Encode a string by counting the consecutive letter.
(i.e., "aaaabbxxxyyz" might become "a4b2x3y2z1").
分析:
这个问题是一个基本的数据压缩算法,将相邻的字符计数存储。解法没有什么特别需要注意的地方,按部就班的实现代码就可以了。
class Solution: # @param s, letters as string
# @return an encoded string
def encode(self, s):
if not s or len(s) == 0:
return "" val = ""
prev = s[0]
count = 1
for c in s[1:]:
if c == prev:
count += 1
else:
val += prev + str(count)
prev = c
count = 1
val += prev + str(count)
return val if __name__ == '__main__':
s = Solution()
assert s.encode("") == ""
assert s.encode("aaaabbxxxyyz") == "a4b2x3y2z1"
assert s.encode("aaaaaaaaaaab") == "a11b1"
print 'PASS'
小结:
extends下是目前不在LeetCode但是我遇到的有趣的程序问题。
Encode Adjacent Letters的更多相关文章
- Bioinformatics Glossary
原文:http://homepages.ulb.ac.be/~dgonze/TEACHING/bioinfo_glossary.html Affine gap costs: A scoring sys ...
- How to Write a Spelling Corrector
http://norvig.com/spell-correct.html Feb 2007to August 2016 How to Write a Spelling Corrector One we ...
- 图论:(Code Forces) Graph and String
Graph and String time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- IOI 2009:Mecho
IOI2009 Mecho Time Limit: 10000ms Memory Limit: 262144KB This problem will be judged on SPOJ. Origin ...
- uva 227 Puzzle
Puzzle A children's puzzle that was popular 30 years ago consisted of a 5x5 frame which contained ...
- Uva227.Puzzle
题目连接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 2272: [Usaco2011 Feb]Cowlphabet 奶牛文字
2272: [Usaco2011 Feb]Cowlphabet 奶牛文字 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 138 Solved: 97 ...
- [Swift]LeetCode809. 情感丰富的文字 | Expressive Words
Sometimes people repeat letters to represent extra feeling, such as "hello" -> "he ...
- [LeetCode] Expressive Words 富于表现力的单词
Sometimes people repeat letters to represent extra feeling, such as "hello" -> "he ...
随机推荐
- c# iText 生成PDF 有文字,图片,表格,文字样式,对齐方式,页眉页脚,等等等,
#region 下载说明书PDF protected void lbtnDownPDF_Click(object sender, EventArgs e) { int pid = ConvertHel ...
- swoole http_server 多进程并使用多进程处理消息
<?php $http = new swoole_http_server("0.0.0.0", 9511); $http->set([ 'worker_num' =&g ...
- mac 下 安装 mongodb
使用brew安装,不过brew不再更新, 通过 sudo chown -R $(whoami):admin /usr/local 这条语句终端有提醒的 xcode-select --install 需 ...
- 最简js深浅拷贝说明
1.浅拷贝 浅拷贝是拷贝引用,拷贝后的引用都是指向同一个对象的实例,彼此之间的操作会互相影响. 浅拷贝分两种情况: 1.直接拷贝源对象的引用 2. 源对象拷贝实例,但其属性对象(类型为Object, ...
- [javascript]jquery选择器笔记
技术文档 中文:http://jquery.cuishifeng.cn/ 英文:http://api.jquery.com/category/selectors/ 分类 基本选择器 层次选择器 过滤选 ...
- git 提交作业流程
git 提交作业流程,主要分为4个步骤 # 拉取远程git最新版本到本地,每次都可以先执行这条命令,因为会有其他同学更新仓库 git pull # add需要上传的文件,那个文件修改或者新增的,就ad ...
- 已知起始点,获取每段等距离途经点的经纬度(用百度js api作)
已知两个中文地址,自动规划路径,获取路径上每个3公里的点的经纬度 <html> <head> <meta http-equiv="Content-Type&qu ...
- DOM: EVENT FLOW
捕获阶段(capture phase) 捕获阶段的定义如下(w3c):The event object propagate through the target's ancestors from th ...
- HDU 3506 (环形石子合并)区间dp+四边形优化
Monkey Party Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Tot ...
- linux 使用dd命令清空文件