mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
更新 0151.反转字符串中的单词 排版格式修复
This commit is contained in:
@ -28,10 +28,11 @@
|
|||||||
输出: "example good a"
|
输出: "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
|
```Java
|
||||||
class Solution {
|
class Solution {
|
||||||
@ -433,9 +433,10 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
python:
|
### python:
|
||||||
(版本一)先删除空白,然后整个反转,最后单词反转。
|
(版本一)先删除空白,然后整个反转,最后单词反转。
|
||||||
**因为字符串是不可变类型,所以反转单词的时候,需要将其转换成列表,然后通过join函数再将其转换成列表,所以空间复杂度不是O(1)**
|
**因为字符串是不可变类型,所以反转单词的时候,需要将其转换成列表,然后通过join函数再将其转换成列表,所以空间复杂度不是O(1)**
|
||||||
|
|
||||||
```Python
|
```Python
|
||||||
class Solution:
|
class Solution:
|
||||||
def reverseWords(self, s: str) -> str:
|
def reverseWords(self, s: str) -> str:
|
||||||
@ -467,7 +468,7 @@ class Solution:
|
|||||||
return " ".join(words)
|
return " ".join(words)
|
||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
### Go:
|
||||||
|
|
||||||
版本一:
|
版本一:
|
||||||
|
|
||||||
@ -571,7 +572,8 @@ func reverse(b *[]byte, left, right int) {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
javaScript:
|
### JavaScript:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
/**
|
/**
|
||||||
* @param {string} s
|
* @param {string} s
|
||||||
@ -630,7 +632,7 @@ function reverse(strArr, start, end) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
TypeScript:
|
### TypeScript:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
function reverseWords(s: string): string {
|
function reverseWords(s: string): string {
|
||||||
@ -689,7 +691,7 @@ function reverseWords(s: string): string {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
Swift:
|
### Swift:
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
func reverseWords(_ s: String) -> String {
|
func reverseWords(_ s: String) -> String {
|
||||||
@ -766,7 +768,7 @@ func reverseWord(_ s: inout [Character]) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Scala:
|
### Scala:
|
||||||
|
|
||||||
```scala
|
```scala
|
||||||
object Solution {
|
object Solution {
|
||||||
@ -824,8 +826,8 @@ object Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### PHP:
|
||||||
|
|
||||||
PHP:
|
|
||||||
```php
|
```php
|
||||||
function reverseWords($s) {
|
function reverseWords($s) {
|
||||||
$this->removeExtraSpaces($s);
|
$this->removeExtraSpaces($s);
|
||||||
@ -872,7 +874,7 @@ function reverseString(&$s, $start, $end) {
|
|||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
Rust:
|
### Rust:
|
||||||
|
|
||||||
```Rust
|
```Rust
|
||||||
// 根据C++版本二思路进行实现
|
// 根据C++版本二思路进行实现
|
||||||
@ -924,7 +926,7 @@ pub fn remove_extra_spaces(s: &mut Vec<char>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
C:
|
### C:
|
||||||
|
|
||||||
```C
|
```C
|
||||||
// 翻转字符串中指定范围的字符
|
// 翻转字符串中指定范围的字符
|
||||||
@ -972,3 +974,4 @@ char * reverseWords(char * s){
|
|||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user