Question is saying do not return anything that means you need to do inplace reversing. Also this is list of string not a string so whatever you change inside function it will be reflected in the list because lists are mutable.
Correct Solution will be
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
s.reverse()
Also If you want to do in your style then correct line will be
s[:] = s[::-1]
Answer from Deepak Tripathi on Stack OverflowSo the code goes:
s = [“h”, ”e”, ”l”, ”l”, ”o”]
s[:] = s[::-1] print(s)
This prints the reverse of s in both VSCode and on the LeetCode website ide
I tried to see if the follow would work:
s = [“h”, ”e”, ”l”, ”l”, ”o”]
s = s[::-1] print(s)
This did work but only on VSCode not on leet code. So my question is why did it work in VSCode and not LeetCode?
There’s other solutions I tried to the problem that work in VSCode but not in LeetCode shock I find it odd, am I doing something I shouldn't?
Today's daily challenge "Reverse words in a string" - Python Solution
reversing a string LeetCode question
Videos
Question is saying do not return anything that means you need to do inplace reversing. Also this is list of string not a string so whatever you change inside function it will be reflected in the list because lists are mutable.
Correct Solution will be
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
s.reverse()
Also If you want to do in your style then correct line will be
s[:] = s[::-1]
The first solution creates a new list and does not change the parameter (which also feels cleaner).
Edit: Never mind the following part. It won't do anything. Thanks.
However you could do something like
s = s[::-1]
instead of the return statement.