[LeetCode] Challenge log 344

.
  1. pythonic s[::-1]
  2. recursive (memory limit exceed)
  3. swithch fisrt and last

Write a function that takes a string as input and returns the string reversed.

Example:
Given s = “hello”, return “olleh”.


Soulution:
1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution:
def reverseString(self, s):
"""
:type s: str
:rtype: str
"""
l = len(s)
if l == 0:
return ""
elif l == 1:
return s
else:
return s[-1] + self.reverseString(s[:-1])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution:
def reverseString(self, s):
"""
:type s: str
:rtype: str
"""
s = list(s)
i = 0
j = len(s) - 1
while i < j:
s[i], s[j] = s[j], s[i]
i += 1
j -= 1
return ''.join(s)
0%