Leetcode "Reverse Words in a String" Python implementation

Source: Internet
Author: User

title :

Given an input string, reverse the string word by word.

For example,
Given s = " the sky is blue ",
Return " blue is sky the ".

code : OJ online test via runtime:172 ms

1 classSolution:2     #@param s, a string3     #@return A string4     defreversewords (self, s):5Words = S.split (' ')6         7         ifLen (words) < 2 :8             returns9         TenTMP ="" One          forWordinchwords: AWord = Word.replace (' ',"') -             ifWord! ="" : -TMP = Word +" "+tmp the          -TMP =Tmp.strip () -          -         returnTmp

Ideas :

Think of a few spaces in the middle, but it's OK.

Finally, we use the strip () function to get the first blank.

Leetcode "Reverse Words in a String" Python implementation

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.