From e70ca923440e656d4c62761fcff2ff40e9c17b32 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 15:47:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200151.=E5=8F=8D=E8=BD=AC?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=B8=AD=E7=9A=84=E5=8D=95=E8=AF=8D?= =?UTF-8?q?=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0151.翻转字符串里的单词.md | 29 +++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md index 6dd3cd49..19ccb725 100644 --- a/problems/0151.翻转字符串里的单词.md +++ b/problems/0151.翻转字符串里的单词.md @@ -28,10 +28,11 @@ 输出: "example good a" 解释: 如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。 +## 算法公开课 -# 思路 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[字符串复杂操作拿捏了! | LeetCode:151.翻转字符串里的单词](https://www.bilibili.com/video/BV1uT41177fX),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -针对本题,我录制了视频讲解:[字符串复杂操作拿捏了! | LeetCode:151.翻转字符串里的单词](https://www.bilibili.com/video/BV1uT41177fX),结合本题解一起看,事半功倍! +## 思路 **这道题目可以说是综合考察了字符串的多种操作。** @@ -204,8 +205,7 @@ public: ## 其他语言版本 - -Java: +### Java: ```Java class Solution { @@ -433,9 +433,10 @@ class Solution { } ``` -python: +### python: (版本一)先删除空白,然后整个反转,最后单词反转。 **因为字符串是不可变类型,所以反转单词的时候,需要将其转换成列表,然后通过join函数再将其转换成列表,所以空间复杂度不是O(1)** + ```Python class Solution: def reverseWords(self, s: str) -> str: @@ -467,7 +468,7 @@ class Solution: return " ".join(words) ``` -Go: +### Go: 版本一: @@ -571,7 +572,8 @@ func reverse(b *[]byte, left, right int) { -javaScript: +### JavaScript: + ```js /** * @param {string} s @@ -630,7 +632,7 @@ function reverse(strArr, start, end) { } ``` -TypeScript: +### TypeScript: ```typescript function reverseWords(s: string): string { @@ -689,7 +691,7 @@ function reverseWords(s: string): string { }; ``` -Swift: +### Swift: ```swift func reverseWords(_ s: String) -> String { @@ -766,7 +768,7 @@ func reverseWord(_ s: inout [Character]) { } ``` -Scala: +### Scala: ```scala object Solution { @@ -824,8 +826,8 @@ object Solution { } ``` +### PHP: -PHP: ```php function reverseWords($s) { $this->removeExtraSpaces($s); @@ -872,7 +874,7 @@ function reverseString(&$s, $start, $end) { return ; } ``` -Rust: +### Rust: ```Rust // 根据C++版本二思路进行实现 @@ -924,7 +926,7 @@ pub fn remove_extra_spaces(s: &mut Vec) { } } ``` -C: +### C: ```C // 翻转字符串中指定范围的字符 @@ -972,3 +974,4 @@ char * reverseWords(char * s){ +