-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path151.py
32 lines (31 loc) · 879 Bytes
/
151.py
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
32
# https://leetcode.com/problems/reverse-words-in-a-string/
class Solution(object):
def reverseWords(self, s):
"""
:type s: str
:rtype: str
"""
inspace = False
result = ""
candidate = ""
s = s.strip()
for i in s[::-1]:
if i == ' ':
if inspace == True:
pass
else:
# popup word
result += candidate[::-1]
candidate = ""
inspace = True
else:
if inspace == True:
result += " "
inspace = False
candidate += i
if not inspace:
result += candidate[::-1]
return result
# TODO: use deque?
# TODO: use timeit?
#print Solution().reverseWords(" the sky is blue ")