-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path151.py
39 lines (34 loc) · 1.06 KB
/
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
33
34
35
36
37
38
39
__________________________________________________________________________________________________
sample 20 ms submission
import sys
class Solution:
def reverseWords(self, s: str) -> str:
words = s.split()
left = 0
right = len(words)-1
while left < right:
words[left], words[right] = words[right], words[left]
left += 1
right -= 1
return " ".join(words)
__________________________________________________________________________________________________
sample 13284 kb submission
#
# @lc app=leetcode id=151 lang=python3
#
# [151] Reverse Words in a String
#
class Solution:
def reverseWords(self, s: str) -> str:
res = []
s += ' '
temp = ''
for c in s:
if c == ' ':
if temp:
res.insert(0, temp)
temp = ''
else:
temp+=c
return ' '.join(res)
__________________________________________________________________________________________________