[Leetcode][Python]19: Remove Nth Node From End of List
# -*- 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的更多相关文章
- 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...
- 【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 ...
- 【LeetCode】19. Remove Nth Node From End of List 删除链表的倒数第 N 个结点
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:链表, 删除节点,双指针,题解,leetcode, 力扣 ...
- 【一天一道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 ...
- 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 ...
- 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 ...
- LeetCode:19. Remove Nth Node From End of List(Medium)
1. 原题链接 https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/ 2. 题目要求 给出一个链表,请 ...
- 【LeetCode】19. Remove Nth Node From End of List
题目: 思路:如果链表为空或者n小于1,直接返回即可,否则,让链表从头走到尾,每移动一步,让n减1. 1.链表1->2->3,n=4,不存在倒数第四个节点,返回整个链表 扫过的节点依次:1 ...
- 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. ...
随机推荐
- haproxy hdr和path
path : string This extracts the request's URL path, which starts at the first slash and ends before ...
- SQL Server 2008数据库的一些基本概念 区、页、行
原文地址:http://www.cnblogs.com/liuzhendong/archive/2011/10/11/2207361.html 以前总是没弄明白这些基本概念,现在整理如下: 1.区: ...
- hdu1258Sum It Up (DFS)
Description Given a specified total t and a list of n integers, find all distinct sums using numbers ...
- svm中的数学和算法
支持向量机(Support Vector Machine)是Cortes和Vapnik于1995年首先提出的,它在解决小样本.非线性及高维模式识别中表现出很多特有的优势,并可以推广应用到函数拟合等其它 ...
- UML视图(四)状态图
以下是一个图书馆管理系统的状态图,非常典型,涵盖状态图的全部元素的使用,由于状态图相对照较简单,直接从看图就能非常好地掌握.假设想对状态图的元素严谨的概念进行了解,在图下方,有仔细的叙述. 看了上面的 ...
- linux中grep使用方法具体解释
查找特定字符串并颜色显示 [root@fwq test]# grep -n 'the' regular_express.txt --color=auto 8:I can't finish the te ...
- oracle优化思考-双刃剑
oracle优化是一个双刃剑,特别注意这把剑用的场合:系统规划OLTP or OLAP 优化1:索引 在DML操作时.必须维护索引.假设大量的DML操作,想想看,IO是不是老高了? 索引长处:在非常多 ...
- ThreadLocal 在web环境下使用的边界问题
ThreadLocal 相关分析,请查看http://wangxinchun.iteye.com/blog/1884228 另外一个必须要提的点是: ThreadLocal在线程池环境下的使用. 比如 ...
- .Net资源文件全球化
I:本博文代码示例效果图 好久没来写随笔了.不多说先上大饼! 跟着直接上 [代码下载地址] II:ASP.NET 资源文件介绍 在ASP.NET特殊文件夹内有那么两个不太引人注意,他们分别是App_ ...
- HasMap
您还未登录 ! 登录 注册 论坛首页 → Java企业应用论坛 → 深入理解HashMap 全部 Hibernate Spring Struts iBATIS 企业应用 Lucene SOA Java ...