leetcode 557. Reverse Words in a String III

题目

Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

Example 1:

Input: "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"

解法:

1.c++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
string reverseWords(string s) {
for(int i = 0;i<s.length();i++)
{
if(s[i] != ' ')
{
int j = i;
for(;j<s.length() && s[j]!=' '; j++);
reverse(s.begin() + i,s.begin() + j);
i = j - 1;
}
}
return s;
}
};

2.python

1
2
3
4
5
6
7
class Solution(object):
def reverseWords(self, s):
"""
:type s: str
:rtype: str
"""
return " ".join(map(lambda x: x[::-1],s.split()))