← 返回 bytedance 的题目列表Reverse Words in a String (variant)
类型:online_judge
Given a string s containing words and spaces, return the string with the order of words reversed. Requirements:
Words are separated by one or more spaces.
In the output, words must be separated by a single space.
The result must not contain leading or trailing spaces.
Follow-up: If the input is provided as a mutable character array / mutable string, can you do it with O(1) extra space (in-place approach)?
Examples
Input: " the sky is blue "
Output: "blue is sky the"
Constraints
1 <= len(s) <= 1e5
s contains visible characters and spaces (at least one word)
Example
Input
the sky is blue
Output
blue is sky the