mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge pull request #2764 from ZaneCodeJourney/0151.python
0151.翻转字符串里的单词 Python 版本1 删除冗余代码
This commit is contained in:
@ -440,11 +440,10 @@ class Solution {
|
|||||||
```Python
|
```Python
|
||||||
class Solution:
|
class Solution:
|
||||||
def reverseWords(self, s: str) -> str:
|
def reverseWords(self, s: str) -> str:
|
||||||
# 删除前后空白
|
|
||||||
s = s.strip()
|
|
||||||
# 反转整个字符串
|
# 反转整个字符串
|
||||||
s = s[::-1]
|
s = s[::-1]
|
||||||
# 将字符串拆分为单词,并反转每个单词
|
# 将字符串拆分为单词,并反转每个单词
|
||||||
|
# split()函数能够自动忽略多余的空白字符
|
||||||
s = ' '.join(word[::-1] for word in s.split())
|
s = ' '.join(word[::-1] for word in s.split())
|
||||||
return s
|
return s
|
||||||
|
|
||||||
@ -1029,3 +1028,4 @@ public string ReverseWords(string 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