-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathno344.py
More file actions
38 lines (32 loc) · 866 Bytes
/
no344.py
File metadata and controls
38 lines (32 loc) · 866 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
'''
Reverse String
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
'''
class Solution(object):
def reverseString(self, s):
'''
:type s: str
:rtype: str
'''
if len(s) == 1 or len(s) == 0:
return s
if len(s) == 2:
return (s[1]+s[0])
s = list(s)
start = 0
end = len(s) - 1
while start <= end:
def swap(s, start, end):
temp = s[start]
s[start] = s[end]
s[end] = temp
return s
s = swap(s, start, end)
start += 1
end -= 1
return ''.join(s)
if __name__ == "__main__":
test = Solution()
print test.reverseString("123456789")