问题描述:
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue
",
return "blue is sky the
".
解题思路:
先利用split()方法将句子按空格分为几个单词的列表,
然后reverse()函数使列表逆序,
最后join函数以空格为间隔拼成字符串返回结果。
Python代码:
class Solution:
# @param s, a string
# @return a string
def reverseWords(self, s):
a = s.split()
a.reverse()
return ‘ ‘.join(a)
LeetCode OJ1:Reverse Words in a String
时间: 2024-11-04 08:08:51