#-*-Coding:utf8-*-
‘‘‘
__author__ = ' [email protected] '
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.
One is read off as "1s" or 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 would be represented as a string.
===comments by dabay===
Test instructions half a day did not understand. It turns out that the given number n is a few and returns the first string. For example, if n is 5, it returns the string "111221".
The first one is definitely 1, and then the next string is generated in a say way.
When you are say:
Compare whether the next number is the same,
If the same, the counter adds a
If not the same, 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