You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
Write a function that reverses a string. The input string is given as an array of characters`s`.
9
8
10
9
You must do this by modifying the input array[in-place](https://en.wikipedia.org/wiki/In-place_algorithm) with`O(1)` extra memory.
11
10
12
-
Difficulty:**Easy**
13
-
14
11
###[Example 1]
15
12
16
13
**Input**:`s = ["h","e","l","l","o"]`
17
14
18
15
**Output**:`["o","l","l","e","h"]`
19
16
20
17
###[Example 2]
18
+
21
19
**Input**:`s = ["H","a","n","n","a","h"]`
22
20
23
21
**Output**:`["h","a","n","n","a","H"]`
24
22
25
23
###[Constraints]
26
-
-`1 <= s.length <= 105`
24
+
25
+
-`1 <= s.length <= 10^5`
27
26
-`s[i]` is a[printable ascii character](https://en.wikipedia.org/wiki/ASCII#Printable_characters).
28
27
28
+
###[Hints]
29
+
29
30
<details>
30
31
<summary>Hint 1</summary>
31
32
The entire logic for reversing a string is based on using the opposite directional two-pointer approach!
32
33
</details>
33
34
34
35
##Intuition
35
-
[中文题解](#中文题解)
36
36
37
37
1. This problem can be solved in one line of code using the built-in`sort()` method of the programming language. If this question is asked in an interview, the questioner should be testing how to do it without the built-in method.
38
38
2. Use**two pointers** with**opposite directions**, initially one pointer points to the index`0` and the other pointer points to the index`s.length - 1`.
39
39
3. Traverse the elements of the array, and the loop condition is`while (left < right)`. In the loop body,`left += 1`,`right -= 1`.
40
40
4. In the loop body, swap the two values.
41
41
5. The above is the template for`two pointers` in`opposite directions`.
42
42
43
-
##Approach
43
+
##Steps
44
44
1. Use two pointers with**opposite directions**, initially one pointer points to the index`0` and the other pointer points to the index`s.length - 1`.
45
+
45
46
```ruby
46
47
left=0
47
48
right= s.length-1
48
49
```
49
50
50
51
2. Traverse the elements of the array, and the loop condition is`while (left < right)`. In the loop body,`left += 1`,`right -= 1`.
52
+
51
53
```ruby
52
54
left=0
53
55
right= s.length-1
@@ -59,6 +61,7 @@ end
59
61
```
60
62
61
63
3. In the loop body, swap the two values.
64
+
62
65
```ruby
63
66
left=0
64
67
right= s.length-1
@@ -72,10 +75,12 @@ end
72
75
```
73
76
74
77
##Complexity
78
+
75
79
* Time:`O(n)`.
76
80
* Space:`O(1)`.
77
81
78
82
##Java
83
+
79
84
```java
80
85
classSolution {
81
86
publicvoidreverseString(char[]s) {
@@ -95,6 +100,7 @@ class Solution {
95
100
```
96
101
97
102
##Python
103
+
98
104
```python
99
105
classSolution:
100
106
defreverseString(self,s: List[str]) ->None:
@@ -108,6 +114,7 @@ class Solution:
108
114
```
109
115
110
116
##C++
117
+
111
118
```cpp
112
119
classSolution {
113
120
public:
@@ -126,6 +133,7 @@ public:
126
133
```
127
134
128
135
##JavaScript
136
+
129
137
```javascript
130
138
varreverseString=function (s) {
131
139
let left=0
@@ -141,6 +149,7 @@ var reverseString = function (s) {