Skip to content

Commit bfbc842

Browse files
committedDec 12, 2020
feat: reverse-words-in-a-string
1 parent 2b30979 commit bfbc842

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
 

‎str.reverse-words-in-a-string.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
"""
3+
151. 翻转字符串里的单词
4+
给定一个字符串,逐个翻转字符串中的每个单词。
5+
说明:
6+
无空格字符构成一个 单词 。
7+
输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。
8+
如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。
9+
"""
10+
def reverseWords(self, s: str) -> str:
11+
arr = s.strip().split(' ')
12+
b = reversed(arr)
13+
res = ''
14+
for i in b:
15+
print(i)
16+
if res and res[-1] == ' ' and i == '':
17+
continue
18+
res += i + ' '
19+
return res[:-1]
20+
21+
so = Solution()
22+
print(so.reverseWords(' hello world '))

0 commit comments

Comments
 (0)
Please sign in to comment.