zoukankan      html  css  js  c++  java
  • [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)




  • 相关阅读:
    Week03-面向对象入门
    Week02-Java基本语法与类库
    201621123056 《Java程序设计》第1周学习总结
    2.2确定一个字符是否在指定范围内
    2.1确定一个char包含何种字符
    1.自己写一个计算器demo
    1.23 确定一个Decimal或Double的整数部分
    1.5 测试奇偶性
    1.2度转化为弧度 1.3弧度转换为度
    1.1确定分数与浮点数值之间的近似相等性。
  • 原文地址:https://www.cnblogs.com/Dabay/p/4276253.html
Copyright © 2011-2022 走看看