mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
Update 0151.翻转字符串里的单词.md
This commit is contained in:
@ -434,134 +434,40 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
python:
|
python:
|
||||||
|
(版本一)先删除空白,然后整个反转,最后单词反转。
|
||||||
|
### 因为字符串是不可变类型,所以反转单词的时候,需要将其转换成列表,然后通过join函数再将其转换成列表,所以空间复杂度不是O(1)
|
||||||
```Python
|
```Python
|
||||||
class Solution:
|
class Solution:
|
||||||
#1.去除多余的空格
|
def reverseWords(self, s: str) -> str:
|
||||||
def trim_spaces(self, s):
|
# 删除前后空白
|
||||||
n = len(s)
|
s = s.strip()
|
||||||
left = 0
|
# 反转整个字符串
|
||||||
right = n-1
|
s = s[::-1]
|
||||||
|
# 将字符串拆分为单词,并反转每个单词
|
||||||
while left <= right and s[left] == ' ': #去除开头的空格
|
s = ' '.join(word[::-1] for word in s.split())
|
||||||
left += 1
|
return s
|
||||||
while left <= right and s[right] == ' ': #去除结尾的空格
|
|
||||||
right -= 1
|
|
||||||
tmp = []
|
|
||||||
while left <= right: #去除单词中间多余的空格
|
|
||||||
if s[left] != ' ':
|
|
||||||
tmp.append(s[left])
|
|
||||||
elif tmp[-1] != ' ': #当前位置是空格,但是相邻的上一个位置不是空格,则该空格是合理的
|
|
||||||
tmp.append(s[left])
|
|
||||||
left += 1
|
|
||||||
return tmp
|
|
||||||
|
|
||||||
#2.翻转字符数组
|
|
||||||
def reverse_string(self, nums, left, right):
|
|
||||||
while left < right:
|
|
||||||
nums[left], nums[right] = nums[right], nums[left]
|
|
||||||
left += 1
|
|
||||||
right -= 1
|
|
||||||
return None
|
|
||||||
|
|
||||||
#3.翻转每个单词
|
|
||||||
def reverse_each_word(self, nums):
|
|
||||||
start = 0
|
|
||||||
end = 0
|
|
||||||
n = len(nums)
|
|
||||||
while start < n:
|
|
||||||
while end < n and nums[end] != ' ':
|
|
||||||
end += 1
|
|
||||||
self.reverse_string(nums, start, end-1)
|
|
||||||
start = end + 1
|
|
||||||
end += 1
|
|
||||||
return None
|
|
||||||
|
|
||||||
#4.翻转字符串里的单词
|
|
||||||
def reverseWords(self, s): #测试用例:"the sky is blue"
|
|
||||||
l = self.trim_spaces(s) #输出:['t', 'h', 'e', ' ', 's', 'k', 'y', ' ', 'i', 's', ' ', 'b', 'l', 'u', 'e'
|
|
||||||
self.reverse_string(l, 0, len(l)-1) #输出:['e', 'u', 'l', 'b', ' ', 's', 'i', ' ', 'y', 'k', 's', ' ', 'e', 'h', 't']
|
|
||||||
self.reverse_each_word(l) #输出:['b', 'l', 'u', 'e', ' ', 'i', 's', ' ', 's', 'k', 'y', ' ', 't', 'h', 'e']
|
|
||||||
return ''.join(l) #输出:blue is sky the
|
|
||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
(版本二)使用双指针
|
||||||
|
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
def reverseWords(self, s: str) -> str:
|
def reverseWords(self, s: str) -> str:
|
||||||
# method 1 - Rude but work & efficient method.
|
# 将字符串拆分为单词,即转换成列表类型
|
||||||
s_list = [i for i in s.split(" ") if len(i) > 0]
|
words = s.split()
|
||||||
return " ".join(s_list[::-1])
|
|
||||||
|
|
||||||
# method 2 - Carlo's idea
|
# 反转单词
|
||||||
def trim_head_tail_space(ss: str):
|
left, right = 0, len(words) - 1
|
||||||
p = 0
|
|
||||||
while p < len(ss) and ss[p] == " ":
|
|
||||||
p += 1
|
|
||||||
return ss[p:]
|
|
||||||
|
|
||||||
# Trim the head and tail space
|
|
||||||
s = trim_head_tail_space(s)
|
|
||||||
s = trim_head_tail_space(s[::-1])[::-1]
|
|
||||||
|
|
||||||
pf, ps, s = 0, 0, s[::-1] # Reverse the string.
|
|
||||||
while pf < len(s):
|
|
||||||
if s[pf] == " ":
|
|
||||||
# Will not excede. Because we have clean the tail space.
|
|
||||||
if s[pf] == s[pf + 1]:
|
|
||||||
s = s[:pf] + s[pf + 1:]
|
|
||||||
continue
|
|
||||||
else:
|
|
||||||
s = s[:ps] + s[ps: pf][::-1] + s[pf:]
|
|
||||||
ps, pf = pf + 1, pf + 2
|
|
||||||
else:
|
|
||||||
pf += 1
|
|
||||||
return s[:ps] + s[ps:][::-1] # Must do the last step, because the last word is omit though the pointers are on the correct positions,
|
|
||||||
```
|
|
||||||
|
|
||||||
```python
|
|
||||||
class Solution: # 使用双指针法移除空格
|
|
||||||
def reverseWords(self, s: str) -> str:
|
|
||||||
|
|
||||||
def removeextraspace(s):
|
|
||||||
start = 0; end = len(s)-1
|
|
||||||
while s[start]==' ':
|
|
||||||
start+=1
|
|
||||||
while s[end]==' ':
|
|
||||||
end-=1
|
|
||||||
news = list(s[start:end+1])
|
|
||||||
slow = fast = 0
|
|
||||||
while fast<len(news):
|
|
||||||
while fast>0 and news[fast]==news[fast-1]==' ':
|
|
||||||
fast+=1
|
|
||||||
news[slow]=news[fast]
|
|
||||||
slow+=1; fast+=1
|
|
||||||
#return "".join(news[:slow])
|
|
||||||
return news[:slow]
|
|
||||||
|
|
||||||
def reversestr(s):
|
|
||||||
left,right = 0,len(s)-1
|
|
||||||
news = list(s)
|
|
||||||
while left < right:
|
while left < right:
|
||||||
news[left],news[right] = news[right],news[left]
|
words[left], words[right] = words[right], words[left]
|
||||||
left+=1; right-=1
|
left += 1
|
||||||
#return "".join(news)
|
right -= 1
|
||||||
return news
|
|
||||||
|
|
||||||
news = removeextraspace(s)
|
# 将列表转换成字符串
|
||||||
news.append(' ')
|
return " ".join(words)
|
||||||
fast=slow=0
|
|
||||||
#print(news)
|
|
||||||
while fast<len(news):
|
|
||||||
while news[fast]!=' ':
|
|
||||||
fast+=1
|
|
||||||
news[slow:fast] = reversestr(news[slow:fast])
|
|
||||||
# print(news[slow:fast])
|
|
||||||
fast=slow=fast+1
|
|
||||||
news2 = reversestr(news[:-1])
|
|
||||||
return ''.join(news2)
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
Reference in New Issue
Block a user