0151python版本1删除冗余代码

This commit is contained in:
Ziyang Wen 2024-09-27 21:17:44 +08:00
parent e7c8d01fc4
commit b32888260f
1 changed files with 2 additions and 2 deletions

View File

@ -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>