LeetCode-557. Reverse words in a string III

Title description:

Given a string, you need to reverse the character order of each word in the string, while still preserving the initial order of spaces and words.

Tip:
In the string, each word is separated by a single space, and there will not be any extra spaces in the string

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

code show as below:

class Solution {
    
    
    public String reverseWords(String s) {
    
    
        String[] strs = s.split(" ");
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < strs.length; i++) {
    
    
            buffer.append(new StringBuffer(strs[i]).reverse().toString());
            buffer.append(" ");
        }
        return buffer.toString().trim();
    }
}

Results of the:
Insert picture description here

Guess you like

Origin blog.csdn.net/FYPPPP/article/details/114240775