Skip to content

Commit f8d4c49

Browse files
committed
二刷151
1 parent 1eeb76e commit f8d4c49

File tree

4 files changed

+90
-42
lines changed

4 files changed

+90
-42
lines changed
Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,55 @@
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

docs/images/0151-10.png

28 KB
Loading

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,11 @@ endif::[]
943943
|{doc_base_url}/0150-evaluate-reverse-polish-notation.adoc[题解]
944944
|✅ 遇到数字就入栈,遇到运算符号就将栈顶元素出栈,计算完成后再入栈。
945945

946+
|{counter:codes2503}
947+
|{leetcode_base_url}/reverse-words-in-a-string/[151. 反转字符串中的单词^]
948+
|{doc_base_url}/0151-reverse-words-in-a-string.adoc[题解]
949+
|✅ 双指针,切割对调。
950+
946951
|===
947952

948953
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class _0151_ReverseWordsInAString_2 {
7+
// tag::answer[]
8+
/**
9+
* @author D瓜哥 · https://www.diguage.com
10+
* @since 2025-06-09 21:36:52
11+
*/
12+
public String reverseWords(String s) {
13+
List<String> result = new ArrayList<>();
14+
int idx = 0;
15+
while (idx < s.length()) {
16+
int end = s.indexOf(" ", idx);
17+
if (end == -1) {
18+
result.add(s.substring(idx));
19+
break;
20+
} else if (idx == end) {
21+
idx++;
22+
} else {
23+
result.add(s.substring(idx, end));
24+
idx = end + 1;
25+
}
26+
}
27+
int left = 0, right = result.size() - 1;
28+
while (left < right) {
29+
String word = result.get(left);
30+
result.set(left, result.get(right));
31+
result.set(right, word);
32+
left++;
33+
right--;
34+
}
35+
return String.join(" ", result);
36+
}
37+
38+
// end::answer[]
39+
public static void main(String[] args) {
40+
new _0151_ReverseWordsInAString_2()
41+
.reverseWords("the sky is blue");
42+
}
43+
}

0 commit comments

Comments
 (0)