557. Reverse Words in a String III

寵の児 提交于 2020-02-08 00:12:04

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.

class Solution {
    public String reverseWords(String s) {
        String[] arr = s.split(" ");
        StringBuilder sbb = new StringBuilder();
        for(int i = 0; i < arr.length; i++){
            StringBuilder sb = new StringBuilder(arr[i]);
            
            sbb.append(sb.reverse().toString()).append(" ");
        }
        String res = sbb.toString();
        return res.trim();
    }
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!