更新 0151.反转字符串中的单词 排版格式修复

This commit is contained in:
jinbudaily
2023-07-19 15:47:12 +08:00
parent 963eb8fc8e
commit e70ca92344

View File

@ -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<char>) {
}
}
```
C:
### C:
```C
// 翻转字符串中指定范围的字符
@ -972,3 +974,4 @@ char * reverseWords(char * s){
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>