From 3ca83b3a34eb6483494ddc1b6154ef78fce1044e Mon Sep 17 00:00:00 2001 From: diguage Date: Thu, 19 Sep 2024 17:32:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=80=E5=88=B7151?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.adoc | 12 +++---- docs/0151-reverse-words-in-a-string.adoc | 21 +++++++++++ docs/index.adoc | 2 +- logbook/202406.adoc | 5 +++ .../_0150_EvaluateReversePolishNotation.java | 5 ++- .../leetcode/_0151_ReverseWordsInAString.java | 36 +++++++++++++++++++ 6 files changed, 73 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/diguage/algo/leetcode/_0151_ReverseWordsInAString.java diff --git a/README.adoc b/README.adoc index eb04b696b6..40f8a17d53 100644 --- a/README.adoc +++ b/README.adoc @@ -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^] diff --git a/docs/0151-reverse-words-in-a-string.adoc b/docs/0151-reverse-words-in-a-string.adoc index a89a3d2c5b..2a1548e210 100644 --- a/docs/0151-reverse-words-in-a-string.adoc +++ b/docs/0151-reverse-words-in-a-string.adoc @@ -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. 反转字符串中的单词 - 双指针,清晰图解^] diff --git a/docs/index.adoc b/docs/index.adoc index b5ef4ad7f9..9e6286f52d 100644 --- a/docs/index.adoc +++ b/docs/index.adoc @@ -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] diff --git a/logbook/202406.adoc b/logbook/202406.adoc index 86f174a618..adb157ea75 100644 --- a/logbook/202406.adoc +++ b/logbook/202406.adoc @@ -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} 道题。 diff --git a/src/main/java/com/diguage/algo/leetcode/_0150_EvaluateReversePolishNotation.java b/src/main/java/com/diguage/algo/leetcode/_0150_EvaluateReversePolishNotation.java index fdab765092..54975c285a 100644 --- a/src/main/java/com/diguage/algo/leetcode/_0150_EvaluateReversePolishNotation.java +++ b/src/main/java/com/diguage/algo/leetcode/_0150_EvaluateReversePolishNotation.java @@ -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) { diff --git a/src/main/java/com/diguage/algo/leetcode/_0151_ReverseWordsInAString.java b/src/main/java/com/diguage/algo/leetcode/_0151_ReverseWordsInAString.java new file mode 100644 index 0000000000..fba3fa35d1 --- /dev/null +++ b/src/main/java/com/diguage/algo/leetcode/_0151_ReverseWordsInAString.java @@ -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 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[] +}