-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse_string.py
More file actions
40 lines (33 loc) · 817 Bytes
/
reverse_string.py
File metadata and controls
40 lines (33 loc) · 817 Bytes
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
36
37
38
39
40
# 344. Reverse String
# https://leetcode.com/problems/reverse-string
import unittest
class Solution:
def reverseString(self, s):
"""
:type s: str
:rtype: str
"""
size = len(s)
if size <= 1:
return s
arr = list(s)
left = 0
right = size - 1
while left < right:
arr[left], arr[right] = arr[right], arr[left]
left += 1
right -= 1
return ''.join(arr)
class TestReverseString(unittest.TestCase):
def test(self):
sol = Solution()
self.assertEqual(
sol.reverseString('abc'),
'cba'
)
self.assertEqual(
sol.reverseString('cad'),
'dac'
)
if __name__ == '__main__':
unittest.TestCase()