反转字符串中的单词 III Reverse Words in a String III

反转字符串中的单词 III 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.

思路&题解

public String reverseWords(String s) {
    StringBuilder builder = new StringBuilder();
    Stack<Character> stack = new Stack<>();
    for (int i = 0; i < s.length(); i++) {
        if (s.charAt(i) == ' ') {
            while (!stack.isEmpty()) {
                builder.append(stack.pop());
            }
            builder.append(' ');
        } else {
            stack.push(s.charAt(i));
        }
    }
    while (!stack.isEmpty()) {
        builder.append(stack.pop());
    }
    return builder.toString();
}

知识点分析