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# 人民币转成大写
/// <summary> /// 转换人民币大小金额 /// </summary> /// <param name="num">金额</ ...
- 一致性hash演示
看到点关于一致性hash的说明,觉得挺有意思,就想体验一下. 上代码看看,体会一下在一致性hash环境下的cache和获取. 因为是小代码演示,没有很细致,包括hash函数就是用取余操作,但活生生的显 ...
- FTRL 使用tensorflow的实现
import tensorflow as tfimport numpy as npfrom sklearn import metricsfrom sklearn.datasets import loa ...
- pfSense 2.4.3 发布,包含重要的安全修复补丁
pfSense 2.4.3 已发布,本次更新包含重要的安全修复和 bug 修复,还引入了一些新特性,具体如下. 值得关注的更新 包含一些重要的安全修复补丁: Kernel PTI mitigation ...
- 转载:Object的create方法文档
源地址:https://developer.mozilla.org/zh-CN/docs/JavaScript/Reference/Global_Objects/Object/create#.E4.B ...
- Kotlin------数据类型和语法
今天简单的来介绍Kotlin的基本语法.编程语言大多相通的,会基础学起来都很快,理论都一样,实现的代码语言不一样而已. 数值类型 Kotlin 处理数值的方法和 java 很相似,但不是完全一样.比如 ...
- PHP 中使用explode()函数切割字符串为数组
explode()函数的作用:使用一个字符串分割另一个字符串,打散为数组. 例如: 字符串 $pizza = "第1 第2 第3 第4 第5 第6"; 根据空格分割后:$piece ...
- python decorator 装饰器
python装饰器是个对嵌套函数的语法糖 作用是在函数调用方法不变的情况下,将函数包装成另一个函数来使用 ---- import time def sum1(): sum = 1 + 2 print ...
- 20165202 2017-2018-2 《Java程序设计》第6周学习总结
教材学习内容总结 Ch8 String类: 程序可以直接使用,String类不能有子类 - 构造String对象 使用String类声明并创建对象 String s = new String(&quo ...
- # 2018-2019-2 20165210《网络攻防技术》Exp1 PC平台逆向破解(BOF实验)
2018-2019-2 20165210<网络攻防技术>Exp1 PC平台逆向破解(BOF实验) 实验分为三个部分: 手工修改可执行文件,改变程序执行流程,直接跳转到getShell函数. ...