151. 문자열에서 단어 뒤집기 | LeetCode | Top Interview 150 | Coding Questions

발행: (2025년 12월 27일 오전 07:03 GMT+9)
1 min read
원문: Dev.to

Source: Dev.to

문제

솔루션

class Solution {
    public String reverseWords(String s) {
        String[] words = s.trim().split("\\s+");
        StringBuilder result = new StringBuilder();

        for (int i = words.length - 1; i >= 0; i--) {
            result.append(words[i]);
            if (i != 0) result.append(" ");
        }

        return result.toString();
    }
}
Back to Blog

관련 글

더 보기 »