# -*- 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. PHPExcel导出

    第一,先查出数据库里面要生成Excel的数据,如: $data= M('User')->findAll();   //查出数据 $name='Excelfile';    //生成的Excel文 ...

  2. javascript的stringFormat函数实现

    写一个简单的stringFormat来给自己用 function stringFormat(format, args) { var formatData; if (arguments.length = ...

  3. SQL Server 存储过程自启动

    前期准备: use master; create table LoginLog(LoginName nvarchar(32),LoginTime datetime); create procedure ...

  4. openwrt 新版IPV6问题

    我的版本是r45579,表现是路由认证后正确获取IPV6地址并且可以ping通外网,电脑可以获取地址但是连不上外网. 加一条转发规则就好了 先查看/etc/config/network里给电脑分配的地 ...

  5. Windows服务程序的原理及实现(服务分为WIN32服务和系统服务)

    今天给大家讲下怎样做一个服务程序...本来是想详细讲的,不过写着写着累得要命..很多 地方就没详细...不过代码我加了点注...如果还有一些不明白的自己查下MSDN......便宜 环境,,VC++6 ...

  6. 在QTableView中使用各种自定义委托

    QT的MVC(View/Delegate)模型十分强大,可以利用各种控件来对表格的输入进行限制,不过我以前一直没有过,这几天研究了一下,写个小例子,希望大家喜欢. 如果看不懂这个例子,请先看QT的自带 ...

  7. VC调用javascript的几种方法

    第一种:通过execScript调用.这种方法,虽然操作方便,但不能获取返回值.m_spHtmlDoc->get_parentWindow(&m_pHtmlWindow);VARIANT ...

  8. 关于mwArray和一般数组的区别

    可以用下面的代码详细理解mwArray和一般数组之间的区别 mwArray a(, , mxDOUBLE_CLASS); double *aData; aData = ]; int iii; ; ii ...

  9. OpenStack IdentityService Keystone V3 API Curl实战

    v3 API Examples Using Curl <Tokens> 1,Default scope 获取token Get an token with default scope (m ...

  10. hdu 1035 Robot Motion(模拟)

    Problem Description A robot has been programmed to follow the instructions in its path. Instructions ...