mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 07:35:35 +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
|
||||
class Solution:
|
||||
def reverseWords(self, s: str) -> str:
|
||||
# 删除前后空白
|
||||
s = s.strip()
|
||||
# 反转整个字符串
|
||||
s = s[::-1]
|
||||
# 将字符串拆分为单词,并反转每个单词
|
||||
# split()函数能够自动忽略多余的空白字符
|
||||
s = ' '.join(word[::-1] for word in s.split())
|
||||
return s
|
||||
|
||||
@ -1029,3 +1028,4 @@ public string ReverseWords(string s) {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
Reference in New Issue
Block a user