-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path151. 翻转字符串里的单词
More file actions
31 lines (30 loc) · 777 Bytes
/
151. 翻转字符串里的单词
File metadata and controls
31 lines (30 loc) · 777 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution:
def reverseWords(self, s: str) -> str:
s=s.split()
s=" ".join(s[::-1])
return s
# 使用API
# 自己编写
class Solution:
def reverseWords(self, s: str) -> str:
result=[]
if not s:
return ""
flag=0 if s[0]==" " else 1
total=""
for i in s:
if i!=" ":
total+=i
flag=1
continue
if i==" " and flag==1:
result.append(total+" ")
total=""
flag=0
continue
if total and total[0]!=" ":
result.append(total+" ")
total=""
for i in result:
total=i+total
return total[:-1]