# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com' 38: Count and Say
https://oj.leetcode.com/problems/count-and-say/ The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.
Given an integer n, generate the nth sequence.
Note: The sequence of integers will be represented as a string. ===Comments by Dabay===
题意半天没搞懂。原来是给的数字n是几,就返回第几个字符串。例如,如果n是5,就返回“111221”这个字符串。
第一个铁定是1,然后用say的方式来往后生成下一个字符串。 say的时候:
比较下一个数字是否一样,
如果一样,计数器加一
如果不一样,say
''' class Solution:
# @return a string
def countAndSay(self, n):
current_result = "1"
start = 1
while start < n:
previous_result = current_result
current_result = ""
counting_number = None
counter = 0
for num in previous_result:
if counting_number is None:
counting_number = num
counter = 1
elif counting_number == num:
counter += 1
else:
current_result += "%s%s" % (counter, counting_number)
counting_number = num
counter = 1
else:
current_result += "%s%s" % (counter, counting_number)
start += 1
return current_result def main():
sol = Solution()
print sol.countAndSay(5) if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)

[Leetcode][Python]19: Remove Nth Node From End of List的更多相关文章

  1. 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...

  2. 【LeetCode】19. Remove Nth Node From End of List (2 solutions)

    Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list and r ...

  3. 【LeetCode】19. Remove Nth Node From End of List 删除链表的倒数第 N 个结点

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:链表, 删除节点,双指针,题解,leetcode, 力扣 ...

  4. 【一天一道LeetCode】#19. Remove Nth Node From End of List

    一天一道LeetCode系列 (一)题目 Given a linked list, remove the nth node from the end of list and return its he ...

  5. LeetCode题解(19)--Remove Nth Node From End of List

    https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 原题: Given a linked list, remove the  ...

  6. LeetCode OJ 19. Remove Nth Node From End of List

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  7. LeetCode:19. Remove Nth Node From End of List(Medium)

    1. 原题链接 https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/ 2. 题目要求 给出一个链表,请 ...

  8. 【LeetCode】19. Remove Nth Node From End of List

    题目: 思路:如果链表为空或者n小于1,直接返回即可,否则,让链表从头走到尾,每移动一步,让n减1. 1.链表1->2->3,n=4,不存在倒数第四个节点,返回整个链表 扫过的节点依次:1 ...

  9. 61. Rotate List(M);19. Remove Nth Node From End of List(M)

    61. Rotate List(M) Given a list, rotate the list to the right by k places, where k is non-negative. ...

随机推荐

  1. Lotto--poj2245

    Lotto Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6605   Accepted: 4185 Description ...

  2. QT5删除隐藏目录+隐藏文件

    1.功能需求 删除一个目录(包括目录本身),同时删除该目录中所有文件及目录(含隐藏的) 2.遇到的问题 qt5中已经有了递归删除目录的函数--->bool QDir::removeRecursi ...

  3. Oracle EBS-SQL (BOM-14):检查工艺路线明细.sql

    select msi.segment1                                         装配件编码,       msi.description             ...

  4. ie 6 position fixed

    IE6position:fixed问题解决方案 2013-11-06 18:25:04|  分类: JS/CSS |  标签:ie6  fixed  |举报 |字号大中小订阅     普通写法 #to ...

  5. 一张图片入门Python

    一张图片入门Python 之前已有别人整理了,一张图入门Python,快速了解各种基本的语法. 英文版: 图 5.1. Quick Python Script Explanation 中文版: 图 5 ...

  6. Java 基础类型

    在Java中,基本的数据类型主要有8种: 1)int  4 字节(byte)  -2的31次方到2的31次方-1 2)short 2 字节(byte) -2的15次方到2的15次方-1 3)long ...

  7. Vis.js图表插件

    Vis.js是一款基于JavaScript的可视化图表库,Vis.js不像其他的图表库那样仅仅支持几种常用的数据图表,比如线形图.柱状图.饼图等,Vis.js支持上百种不同类型的可视化图表类型,比如时 ...

  8. 蜂窝移动网络是什么,它和 Wi-Fi 有什么区别? 蓝牙和无线有什么区别?

    蜂窝移动网络是什么,它和 Wi-Fi 有什么区别? 转自知乎用户的一个回答: 原题问的是"数据流量是什么",不知道怎么又被改成"蜂窝移动网络是什么"了.说下个人 ...

  9. 菜鸟必须知道的linux的文件目录结构

    Linux文件目录结 / 根目录,所有的目录.文件.设备都在/之下,/就是Linux文件系统的组织者,也是最上级的领导者. /bin bin就是二进制(binary)英文缩写.在一般的系统当中,你都可 ...

  10. Linq 中的Select事例

    ---恢复内容开始--- 1.对查询结果进行转换. string[] names={"ADE","Jerry","Steves"}; var ...