Skip to content

Commit 23598cd

Browse files
344. Reverse String
Difficulty: Easy 478 / 478 test cases passed. Runtime: 204 ms Memory Usage: 18 MB
1 parent 6c835f3 commit 23598cd

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Easy/344.ReverseString.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
Write a function that reverses a string. The input string is given as an
3+
array of characters char[].
4+
Do not allocate extra space for another array, you must do this by
5+
modifying the input array in-place with O(1) extra memory.
6+
You may assume all the characters consist of printable ascii characters.
7+
8+
Example:
9+
Input: ["h","e","l","l","o"]
10+
Output: ["o","l","l","e","h"]
11+
"""
12+
#Difficulty: Easy
13+
#478 / 478 test cases passed.
14+
#Runtime: 204 ms
15+
#Memory Usage: 18 MB
16+
17+
#Runtime: 204 ms, faster than 91.57% of Python3 online submissions for Reverse String.
18+
#Memory Usage: 18 MB, less than 5.81% of Python3 online submissions for Reverse String.
19+
20+
class Solution:
21+
def reverseString(self, s: List[str]) -> None:
22+
i = 0
23+
l = len(s) - 1
24+
while i <= l:
25+
s[i], s[l] = s[l], s[i]
26+
i += 1
27+
l -= 1
28+
return s

0 commit comments

Comments
 (0)