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的更多相关文章

  1. Bioinformatics Glossary

    原文:http://homepages.ulb.ac.be/~dgonze/TEACHING/bioinfo_glossary.html Affine gap costs: A scoring sys ...

  2. How to Write a Spelling Corrector

    http://norvig.com/spell-correct.html Feb 2007to August 2016 How to Write a Spelling Corrector One we ...

  3. 图论:(Code Forces) Graph and String

    Graph and String time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  4. IOI 2009:Mecho

    IOI2009 Mecho Time Limit: 10000ms Memory Limit: 262144KB This problem will be judged on SPOJ. Origin ...

  5. uva 227 Puzzle

     Puzzle  A children's puzzle that was popular 30 years ago consisted of a 5x5 frame which contained ...

  6. Uva227.Puzzle

    题目连接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  7. 2272: [Usaco2011 Feb]Cowlphabet 奶牛文字

    2272: [Usaco2011 Feb]Cowlphabet 奶牛文字 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 138  Solved: 97 ...

  8. [Swift]LeetCode809. 情感丰富的文字 | Expressive Words

    Sometimes people repeat letters to represent extra feeling, such as "hello" -> "he ...

  9. [LeetCode] Expressive Words 富于表现力的单词

    Sometimes people repeat letters to represent extra feeling, such as "hello" -> "he ...

随机推荐

  1. 爬虫框架Scrapy之Downloader Middlewares

    反反爬虫相关机制 Some websites implement certain measures to prevent bots from crawling them, with varying d ...

  2. 初识numpy的多维数组对象ndarray

    PS:内容来源于<利用Python进行数据分析> 一.创建ndarray 1.array :将一个序列(嵌套序列)转换为一个数组(多维数组) In[2]: import numpy as ...

  3. 【分类】AlexNet论文总结

    目录 0. 论文链接 1. 概述 2. 对数据集的处理 3. 网络模型 3.1 ReLU Nonlinearity 3.2 Training on multiple GPUs 3.3 Local Re ...

  4. valgrind的memchk和callgrind

    一.安装valgrind 安装valgrind,正常的三部曲configure/make/make install就行. 二.memchk使用 1.执行命令 [root@10g-host4 tools ...

  5. [小问题笔记(四)] Enum枚举类型转换为DataTable( C# )

    枚举: public enum ProductType { 小产品=, 大产品, 超大产品 } 转换方法: /// <summary> /// 枚举类型转化为DataTable /// & ...

  6. C#代码实现 Excel表格与Object互相转换,Excel表格导入数据库(.NET2.0 .NET4.0)

    前些天在工作上遇到这个需求,在GitHub找到一个开源代码可以用,Fork了一个版本,整理一下发出来. ①.Net项目中使用Nuget安装一个 NPOI 包    https://github.com ...

  7. 【平台中间件】Nginx安装配置,实现版本更新不影响服务访问

    为什么要做负载均衡? 当你网站是一个企业站.个人博客的时候,或者访问量比较小的时候,一台服务器完全应付的了,那就完全没必要做负载均衡.但是,如果你的网站是平台级别,用户达到十万百万级别了,一台服务器明 ...

  8. CTR的贝叶斯平滑

    参考论文: Click-Through Rate Estimation for Rare Events in Online Advertising 参考的博客: 1.https://jiayi797. ...

  9. npm install遇到的问题

    phantomjs-prebuilt@2.1.16 install: 'node install.js' 在虚拟机上初始化vue-cli项目,npm install时遇到的问题 npm install ...

  10. LA-4255 Guess (拓扑排序+构造)

    题目大意:一个未知的整数序列,给出其任意一个区间和的正负,还原这个序列.任意一个满足条件的序列即可. 题目分析:将连续区间和转化为前缀和之差,sumx-1与sumy的大小关系已知,以此建立一条有向边, ...