mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-11 02:58:13 +08:00
44 lines
1.8 KiB
HTML
44 lines
1.8 KiB
HTML
<p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
|
|
|
|
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
|
|
|
|
<p>Return <em>a string of the words in reverse order concatenated by a single space.</em></p>
|
|
|
|
<p><b>Note</b> that <code>s</code> may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.</p>
|
|
|
|
<p> </p>
|
|
<p><strong>Example 1:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> s = "the sky is blue"
|
|
<strong>Output:</strong> "blue is sky the"
|
|
</pre>
|
|
|
|
<p><strong>Example 2:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> s = " hello world "
|
|
<strong>Output:</strong> "world hello"
|
|
<strong>Explanation:</strong> Your reversed string should not contain leading or trailing spaces.
|
|
</pre>
|
|
|
|
<p><strong>Example 3:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> s = "a good example"
|
|
<strong>Output:</strong> "example good a"
|
|
<strong>Explanation:</strong> You need to reduce multiple spaces between two words to a single space in the reversed string.
|
|
</pre>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
|
|
<li><code>s</code> contains English letters (upper-case and lower-case), digits, and spaces <code>' '</code>.</li>
|
|
<li>There is <strong>at least one</strong> word in <code>s</code>.</li>
|
|
</ul>
|
|
|
|
<p> </p>
|
|
<p><b data-stringify-type="bold">Follow-up: </b>If the string data type is mutable in your language, can you solve it <b data-stringify-type="bold">in-place</b> with <code data-stringify-type="code">O(1)</code> extra space?</p>
|