Skip to content

Commit f5305fd

Browse files
authored
Update 344._reverse_string.md
1 parent 3760700 commit f5305fd

File tree

1 file changed

+40
-19
lines changed

1 file changed

+40
-19
lines changed
Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,36 @@
1-
### 344. Reverse String
1+
# 344. Reverse String
22

3+
**<font color=red>难度: Easy</font>**
34

5+
## 刷题内容
46

5-
题目:
6-
<https://leetcode.com/problems/reverse-string/>
7+
> 原题连接
78
9+
* https://leetcode.com/problems/reverse-string/
810

9-
难度:
10-
Easy
11+
> 内容描述
1112
12-
思路:
13+
```
14+
Write a function that takes a string as input and returns the string reversed.
1315
14-
不要脸的python AC code:
16+
Example 1:
1517
18+
Input: "hello"
19+
Output: "olleh"
20+
Example 2:
21+
22+
Input: "A man, a plan, a canal: Panama"
23+
Output: "amanaP :lanac a ,nalp a ,nam A"
24+
```
25+
26+
## 解题方案
27+
28+
> 思路 1
29+
******- 时间复杂度: O(N)******- 空间复杂度: O(N)******
30+
31+
因为python不支持item assignment
32+
33+
所以如果非要用two pointer来做的话,那么会是这样
1634

1735
```python
1836
class Solution(object):
@@ -21,12 +39,23 @@ class Solution(object):
2139
:type s: str
2240
:rtype: str
2341
"""
24-
return s[::-1]
42+
lst = list(s)
43+
start, end = 0, len(lst) - 1
44+
45+
while start < end:
46+
lst[end], lst[start] = lst[start], lst[end]
47+
start += 1
48+
end -= 1
49+
return ''.join(lst)
2550
```
2651

27-
因为python不支持item assignment
2852

29-
所以如果非要用two pointer来做的话,那么会是这样
53+
54+
> 思路 2
55+
******- 时间复杂度: O(N)******- 空间复杂度: O(1)******
56+
57+
不要脸的python AC code:
58+
3059

3160
```python
3261
class Solution(object):
@@ -35,13 +64,5 @@ class Solution(object):
3564
:type s: str
3665
:rtype: str
3766
"""
38-
lst = list(s)
39-
n = len(lst)
40-
start, end = 0, n - 1
41-
42-
while start < end:
43-
lst[end], lst[start] = lst[start],lst[end]
44-
start += 1
45-
end -= 1
46-
return ''.join(lst)
67+
return s[::-1]
4768
```

0 commit comments

Comments
 (0)