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. SpringBoot学习(1)

    内容概要: spring data springboot是spring团队基于spring4开发的一个框架. springboot来简化spring应用开发,约定大于配置,去繁从简,just run就 ...

  2. Django 2.0 的路由如何实现正则表达式

    在django2.0的路由系统中,摒弃了1.x中的url,而改用path.需要导入path. from django.urls import path,re_path 在1.x中,使用url()即可实 ...

  3. Js 操作 Cookies

    <script language=javascript> // cookie其实是一个key=value就是一个cookie而不是 //获得coolie 的值 function cooki ...

  4. Android我的便签-----SQLite的使用方法

    在Android开发中也有数据库的存在,最近有空,把以前写的一个便签来讲述一下Android中的数据库,跟大家分享分享的,希望对大家有所帮助. SQLite简介 SQLite,是一款轻量级的关系型数据 ...

  5. flask学习(十一):if判断语句

    1. 语法: {% if xxx %} {% else %} {% endif %} 2. if的使用,和python中if相差无几 用if判断,当用户名存在且年龄大于18时,才显示用户名和注销

  6. Android面试三之Service

    Service是什么 Service(服务)是一个没有用户界面的在后台运行执行耗时操作的应用组件.其他应用组件能够启动Service,并且当用户切换到另外的应用场景,Service将持续在后台运行.另 ...

  7. HDU 4696 Answers (脑补+数形结合)

    题意 给一个图,每个点的出度为1,每个点的权值为1或者2.给Q个询问,问是否能找到一条路径的权值和M. 思路 由于每个点的出度为1,所以必然存在环.又因为c[i]只能取1或者2,可以组成任意值,所以只 ...

  8. 清华大学 pip 源

    pypi 镜像使用帮助 pypi 镜像每 5 分钟同步一次. 临时使用 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-pac ...

  9. ReadWriteLock读写锁

    ReadWriteLock维护了一对锁,读锁可允许多个读线程并发使用,写锁是独占的. 下面通过一个简单的例子来了解ReadWriteLock. package com.ccfdod.juc; impo ...

  10. bzoj1997

    题解: 在圆上面的点能不能不交叉 和那一题差不多 http://www.cnblogs.com/xuanyiming/p/8110597.html 代码: #include<bits/stdc+ ...