forked from shijbian/LeetCode
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse-string.py
More file actions
Latest commit
33 lines (29 loc) · 664 Bytes
/
Copy pathreverse-string.py
File metadata and controls
33 lines (29 loc) · 664 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
# Time: O(n)
# Space: O(n)
# Write a function that takes a string as input and
# returns the string reversed.
#
# Example:
# Given s = "hello", return "olleh".
classSolution(object):
defreverseString(self, s):
"""
:type s: str
:rtype: str
"""
string=list(s)
i, j=0, len(string) -1
whilei<j:
string[i], string[j] =string[j], string[i]
i+=1
j-=1
return"".join(string)
# Time: O(n)
# Space: O(n)
classSolution2(object):
defreverseString(self, s):
"""
:type s: str
:rtype: str
"""
returns[::-1]