本文详细解析了LeetCode上的经典面试题“Reverse Words in a String III”。通过Java实现,介绍了如何在保持原有单词顺序的同时,反转每个单词内的字符顺序。文章提供了完整的代码示例,并附有力扣官方题解链接,深入探讨了StringBuilder类的reverse(), setLength(), insert()等方法的使用。
LeetCode Top Interview Questions 557. Reverse Words in a String III (Java版; Easy)
题目描述
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.