LeetCode 186 - Reverse Words in a String II: A Microsoft Journey

preview_player
Показать описание
Python Standard Library:

My Favorite Algo Courses / books:

A Common Sense Guide to Data Structures and Algorithms

Data Structures the Fun Way

Elements of Programming Interviews in Python

Data Structures & Algorithms - Python

Grokking Algorithms

Big O Notation
Рекомендации по теме
Комментарии
Автор

Thanks for the solution. You can avoid the very last loop by using "while" instead of "if" in the inner loop as follows.

l, r = 0, len(s)-1

while l < r:
s[l], s[r] = s[r], s[l]
l += 1
r -= 1

l, r, ind = 0, 0, 0

while r < len(s):
while r < len(s) and s[r] != ' ':
r += 1
ind = r
r -= 1
while l < r:
s[l], s[r] = s[r], s[l]
l += 1
r -= 1

l, r = ind + 1, ind + 1

haniehsalehy
Автор

shoudn't the time complexity be O(n^2). Cause there is a while loop within another while loop. ?

pavandurganivas