-
Notifications
You must be signed in to change notification settings - Fork 0
/
Reverse String
36 lines (34 loc) · 924 Bytes
/
Reverse String
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
using iterative method (two pointers)
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
l,r=0,len(s)-1
while l<r:
s[l],s[r]=s[r],s[l]
l,r=l+1,r-1
or using stack
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
stack = []
for i in s:
stack.append(i)
i=0
while stack:
s[i]=stack.pop()
i+=1
or using recusrion
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
def reversing (l,r):
if l<r:
s[l],s[r]=s[r],s[l]
reversing(l+1,r-1)
reversing(0,len(s)-1)