Lintcode Flip String problem-python implementation

Source: Internet
Author: User

title Description: try to implement a function reversewords, the function passed in the parameter is a string, the return value is the word between the words to adjust the string (only the word order adjustment can be).

For example: The incoming parameter is "The sky is blue" and the adjusted string is "Blue is sky the".

Problem Solving Ideas: the string is converted into a character array and then adjusted in reverse order to "the sky is Blue", getting [' E ', ' u ', ' l ', ' B ', ', ' s ', ' I ', ', ' Y ', ' k ', ' s ', ', ' e ', ' H ' ', ' t ']; then look for the boundaries of each word, adjust each word in reverse order, and eventually concatenate into a string.

defReversewords (s):"""reverse the word between words"""s= S.strip (' ')#Remove First space    ifs = = NoneorLen (s) = =0:returnNone S= List (s) [::-1]    #Search for word boundaries and adjust the order of words in reverse orderStart =-1End=-1 forIinchRange (len (s)):ifS[i]! =' ': Start= Iif(i==0ors[i-1]==' ')ElseStart End= Iif(I==len (s)-1ors[i+1]==' ')ElseEndifStart! =-1 andEnd! =-1: Reverse (S, start, end) Start=-1End=-1return "'. Join (s)defreverse (s, start, end):"""Reverse Word"""     whileStart <End:s[start], S[end]=S[end], S[start] Start+ = 1End-= 1

Lintcode Flip String problem-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.