Skip to content

Commit

Permalink
一刷151
Browse files Browse the repository at this point in the history
  • Loading branch information
diguage committed Sep 19, 2024
1 parent 0569c9f commit 3ca83b3
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 8 deletions.
12 changes: 6 additions & 6 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1083,12 +1083,12 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
|Medium
|

//|{counter:codes}
//|{leetcode_base_url}/reverse-words-in-a-string/[151. Reverse Words in a String^]
//|{source_base_url}/_0151_ReverseWordsInAString.java[Java]
//|{doc_base_url}/0151-reverse-words-in-a-string.adoc[题解]
//|Medium
//|
|{counter:codes}
|{leetcode_base_url}/reverse-words-in-a-string/[151. Reverse Words in a String^]
|{source_base_url}/_0151_ReverseWordsInAString.java[Java]
|{doc_base_url}/0151-reverse-words-in-a-string.adoc[题解]
|Medium
|

|{counter:codes}
|{leetcode_base_url}/maximum-product-subarray/[152. Maximum Product Subarray^]
Expand Down
21 changes: 21 additions & 0 deletions docs/0151-reverse-words-in-a-string.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,31 @@ Given an input string, reverse the string word by word.

For C programmers, try to solve it _in-place_ in _O_(1) extra space.

== 思路分析

[[src-0151]]
[tabs]
====
一刷::
+
--
[{java_src_attr}]
----
include::{sourcedir}/_0151_ReverseWordsInAString.java[tag=answer]
----
--
// 二刷::
// +
// --
// [{java_src_attr}]
// ----
// include::{sourcedir}/_0151_ReverseWordsInAString_2.java[tag=answer]
// ----
// --
====

== 参考资料

. 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. 反转字符串中的单词 - 双指针,清晰图解^]

2 changes: 1 addition & 1 deletion docs/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ include::0148-sort-list.adoc[leveloffset=+1]

include::0150-evaluate-reverse-polish-notation.adoc[leveloffset=+1]

// include::0151-reverse-words-in-a-string.adoc[leveloffset=+1]
include::0151-reverse-words-in-a-string.adoc[leveloffset=+1]

include::0152-maximum-product-subarray.adoc[leveloffset=+1]

Expand Down
5 changes: 5 additions & 0 deletions logbook/202406.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,11 @@
|{doc_base_url}/0295-find-median-from-data-stream.adoc[题解]
|⭕️ 两个优先队列的解法真妙!

|{counter:codes}
|{leetcode_base_url}/reverse-words-in-a-string/[151. Reverse Words in a String^]
|{doc_base_url}/0151-reverse-words-in-a-string.adoc[题解]
|✅ 更优解是倒序遍历数组,用双指针记录单词下标。

|===

截止目前,本轮练习一共完成 {codes} 道题。
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,14 @@
* @since 2020-01-26 16:01
*/
public class _0150_EvaluateReversePolishNotation {
// tag::answer[]
// tag::answer[]
/**
* Runtime: 4 ms, faster than 97.32% of Java online submissions for Evaluate Reverse Polish Notation.
*
* Memory Usage: 41.1 MB, less than 6.00% of Java online submissions for Evaluate Reverse Polish Notation.
*
* @author D瓜哥 · https://www.diguage.com
* @since 2020-01-26 16:01
*/
public int evalRPN(String[] tokens) {
if (Objects.isNull(tokens) || tokens.length == 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.diguage.algo.leetcode;

import java.util.Deque;
import java.util.LinkedList;

public class _0151_ReverseWordsInAString {
// tag::answer[]

/**
* @author D瓜哥 · https://www.diguage.com
* @since 2024-09-19 17:11:20
*/
public String reverseWords(String s) {
Deque<String> stack = new LinkedList<>();
boolean inWord = false;
for (char c : s.toCharArray()) {
if (c == ' ') {
inWord = false;
} else {
if (!inWord) {
stack.push(String.valueOf(c));
inWord = true;
} else {
stack.push(stack.pop() + c);
}
}
}
StringBuilder sb = new StringBuilder(s.length());
while (!stack.isEmpty()) {
sb.append(stack.pop()).append(" ");
}
sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}
// end::answer[]
}

0 comments on commit 3ca83b3

Please sign in to comment.