I am attempting an array reversal problem.
Input array: s=["h","e","l","l","o"]
class Solution(object):
def reverseString(self, s):
"""
:type s: List[str]
:rtype: None Do not return anything, modify s in-place instead.
"""
left = 0
right = len(s) - 1
print (left)
print (right)
while left < right:
s[left], s[right] = s[right], s[left]
left = left + 1
right = right - 1
The code above runs successfully. However, when I adjust the line of code below: while left<right
it errors and I'm really confused as to why:
while left < right:
s[left] = s[right]
s[right] = s[left]
Below are the results:
- my output:
["o","l","l","l","o"]
- expected:
["o","l","l","e","h"]
I'm super confused as to what is going on here. Can someone explain please?