Old Wei wins the offer to take you to learn --- Brush title series (44. Flip word order of the columns)

44. Flip word order of the columns

problem:

Cattle recently came off a new employee Fish, every morning and always will be holding a magazine in English, write some sentences in the book. Cat Fish wrote to colleagues interested in the content, look to the day he borrowed Fish, but not read its meaning. For example, "student. A am I". Later I realized that this guy had the sentence word order reversed, the correct sentence should be "I am a student.". Cat on the order of every one of these words do not flip the line, can you help him?

thought:

Create two stacks to achieve flip characters.

python code:

# -*- coding:utf-8 -*-
class Solution:
    def ReverseSentence(self, s):
        # write code here
        s_list=s.split(' ')
        n=len(s_list)
        tmp=[]
        result=[]
        for i in s_list:
            tmp.append(i)
        for x in range(n):
            result.append(tmp.pop())
        return ' '.join([i for i in result])
            
        
Published 160 original articles · won praise 30 · views 70000 +

Guess you like

Origin blog.csdn.net/yixieling4397/article/details/105057070