151. Reverse Words in a String
solution
class Solution:
def reverseWords(self, s: str) -> str:
return " ".join(s.split()[::-1])class Solution:
def reverseWords(self, s: str) -> str:
words = s.strip().split()
left, right = 0, len(words) - 1
while left < right:
words[left], words[right] = words[right], words[left]
left += 1
right -= 1
return " ".join(words)follow up
Last updated