11[#0151-reverse-words-in-a-string]
2- = 151. Reverse Words in a String
2+ = 151. 反转字符串中的单词
33
4- { leetcode} /problems/reverse-words-in-a-string/[LeetCode - Reverse Words in a String ^]
4+ https:// leetcode.cn /problems/reverse-words-in-a-string/[LeetCode - 151. 反转字符串中的单词 ^]
55
6- Given an input string, reverse the string word by word.
6+ 给你一个字符串 `s` ,请你反转字符串中 *单词* 的顺序。
77
8-
8+ *单词* 是由非空格字符组成的字符串。 `s` 中使用至少一个空格将字符串中的 *单词* 分隔开。
99
10- *Example 1:*
10+ 返回 *单词* 顺序颠倒且 *单词* 之间用单个空格连接的结果字符串。
1111
12- [subs="verbatim,quotes,macros"]
13- ----
14- *Input:* "`the sky is blue`"
15- *Output:* "`blue is sky the`"
16- ----
12+ **注意:** 输入字符串 `s` 中可能会存在前导空格、尾随空格或者单词间的多个空格。返回的结果字符串中,单词间应当仅用单个空格分隔,且不包含任何额外的空格。
1713
18- *Example 2:*
19-
20- [subs="verbatim,quotes,macros"]
21- ----
22- *Input:* " hello world! "
23- *Output:* "world! hello"
24- *Explanation:* Your reversed string should not contain leading or trailing spaces.
25- ----
14+ *示例 1:*
2615
27- *Example 3:*
16+ ....
17+ 输入:s = "the sky is blue"
18+ 输出:"blue is sky the"
19+ ....
2820
29- [subs="verbatim,quotes,macros"]
30- ----
31- *Input:* "a good example"
32- *Output:* "example good a"
33- *Explanation:* You need to reduce multiple spaces between two words to a single space in the reversed string.
34- ----
21+ *示例 2:*
3522
36-
23+ ....
24+ 输入:s = " hello world "
25+ 输出:"world hello"
26+ 解释:反转后的字符串中不能存在前导空格和尾随空格。
27+ ....
3728
38- *Note: *
29+ *示例 3: *
3930
31+ ....
32+ 输入:s = "a good example"
33+ 输出:"example good a"
34+ 解释:如果两个单词间有多余的空格,反转后的字符串需要将单词间的空格减少到仅有一个。
35+ ....
4036
41- * A word is defined as a sequence of non-space characters.
42- * Input string may contain leading or trailing spaces. However, your reversed string should not contain leading or trailing spaces.
43- * You need to reduce multiple spaces between two words to a single space in the reversed string.
37+ *提示:*
4438
39+ * `1 \<= s.length \<= 10^4^`
40+ * `s` 包含英文大小写字母、数字和空格 `+' '+`
41+ * `s` 中 *至少存在一个* 单词
4542
46-
4743
48- *Follow up:*
44+ **进阶:** 如果字符串在你使用的编程语言中是一种可变数据类型,请尝试使用 stem:[O(1)] 额外空间复杂度的 *原地* 解法。
4945
50- For C programmers, try to solve it _in-place_ in _O_(1) extra space.
5146
5247== 思路分析
5348
49+ 使用空格切分字符串,然后将字符串对调,再合并。看答案,有个一个更高效的办法,从后面分割单词,这样就不需要对调了。
50+
51+ image::images/0151-10.png[{image_attr}]
52+
5453[[src-0151]]
5554[tabs]
5655====
@@ -63,17 +62,18 @@ include::{sourcedir}/_0151_ReverseWordsInAString.java[tag=answer]
6362----
6463--
6564
66- // 二刷::
67- // +
68- // --
69- // [{java_src_attr}]
70- // ----
71- // include::{sourcedir}/_0151_ReverseWordsInAString_2.java[tag=answer]
72- // ----
73- // --
65+ 二刷::
66+ +
67+ --
68+ [{java_src_attr}]
69+ ----
70+ include::{sourcedir}/_0151_ReverseWordsInAString_2.java[tag=answer]
71+ ----
72+ --
7473====
7574
75+
7676== 参考资料
7777
78- . https://leetcode.cn/problems/reverse-words-in-a-string/solutions/2361551/151-fan-zhuan-zi-fu-chuan-zhong-de-dan-c-yb1r/?envType=study-plan-v2&envId=selected-coding-interview [151. 反转字符串中的单词 - 双指针,清晰图解^]
78+ . https://leetcode.cn/problems/reverse-words-in-a-string/solutions/2361551/151-fan-zhuan-zi-fu-chuan-zhong-de-dan-c-yb1r/[151. 反转字符串中的单词 - 双指针,清晰图解^]
7979
0 commit comments