[Leetcode] [Python]19:remove Nth Node from End of List

Source: Internet
Author: User
Tags first string

#-*-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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.