【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"
Note: In the string, each word is separated by single space and there will not be any extra space in the string.

解析

传入一个字符串,将每个单词反序,然后依然保留空格输出

我的解法

第一次写一行解法,对比了下leetcode上面的最多票数解法要简便许多,可能是因为最多票的解法那时候还没出java8吧

public class ReverseWordsInAStringIII {
    public static String reverseWords(String s) {
        return Arrays.stream(s.split(" ")).map(w -> new StringBuffer(w).reverse().toString()).collect(Collectors.joining(" ")).toString();
    }
}
posted @ 2017-07-19 19:18  l.shane  阅读(125)  评论(0编辑  收藏  举报