-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathq0557.py
More file actions
27 lines (24 loc) · 749 Bytes
/
q0557.py
File metadata and controls
27 lines (24 loc) · 749 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
class Solution:
def reverseWords(self, s: str) -> str:
words = s.split(" ")
for i in range(len(words)):
words[i] = words[i][::-1]
return " ".join(words)
def reverseWords2(self, s: str) -> str:
result = ""
for i in range(len(s)):
if s[i] == " ":
index = i - 1
while index >= 0 and s[index] != " ":
result += s[index]
index -= 1
result += " "
index = len(s) - 1
while index >= 0 and s[index] != " ":
result += s[index]
index -= 1
return result
solu = Solution()
s = "Let's take LeetCode contest"
result = solu.reverseWords(s)
print(result)