Reverse Words in a String
Given an input string, reverse the string word by word.
For example,
Given s = "
the sky is blue
",
return "blue is sky the
".Update (2015-02-12):
For C programmers: Try to solve itin-placeinO(1) space.
string的常见操作,先将字符串以空格分割,然后逆序合并
class Solution:
# @param s, a string
# @return a string
def reverseWords(self, s):
return ' '.join(s.split()[::-1])
public class Solution {
public String reverseWords(String s) {
return Stream.of(s.trim().split("\\s+")).sorted((x, y) -> -1).collect(Collectors.joining(" "));
}
}
package main
import (
"fmt"
"strings"
)
func reverse_words(s string) string {
words := strings.Fields(s)
for i, j := 0, len(words)-1; i < j; i, j = i+1, j-1 {
words[i], words[j] = words[j], words[i]
}
return strings.Join(words, " ")
}
func main() {
fmt.Println(reverse_words("one two three"))
}
Last modified 4yr ago