Skip to content

Commit ebbf936

Browse files
committed
344-reverse-string.md Separated Chinese solutions from English solutions.
1 parent 7ccc8d8 commit ebbf936

File tree

3 files changed

+71
-163
lines changed

3 files changed

+71
-163
lines changed

en/1-1000/344-reverse-string.md

Lines changed: 25 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,55 @@
11
# 344. Reverse String - Best Practices of LeetCode Solutions
2-
LeetCode link: [344. Reverse String](https://leetcode.com/problems/reverse-string),
3-
[344. 反转字符串](https://leetcode.cn/problems/reverse-string)
42

5-
[中文题解](#中文题解)
3+
LeetCode link: [344. Reverse String](https://leetcode.com/problems/reverse-string), difficulty: **Easy**.
4+
5+
## LeetCode description of "344. Reverse String"
66

7-
## LeetCode problem description
87
Write a function that reverses a string. The input string is given as an array of characters `s`.
98

109
You must do this by modifying the input array [in-place](https://en.wikipedia.org/wiki/In-place_algorithm) with `O(1)` extra memory.
1110

12-
Difficulty: **Easy**
13-
1411
### [Example 1]
1512

1613
**Input**: `s = ["h","e","l","l","o"]`
1714

1815
**Output**: `["o","l","l","e","h"]`
1916

2017
### [Example 2]
18+
2119
**Input**: `s = ["H","a","n","n","a","h"]`
2220

2321
**Output**: `["h","a","n","n","a","H"]`
2422

2523
### [Constraints]
26-
- `1 <= s.length <= 105`
24+
25+
- `1 <= s.length <= 10^5`
2726
- `s[i]` is a [printable ascii character](https://en.wikipedia.org/wiki/ASCII#Printable_characters).
2827

28+
### [Hints]
29+
2930
<details>
3031
<summary>Hint 1</summary>
3132
The entire logic for reversing a string is based on using the opposite directional two-pointer approach!
3233
</details>
3334

3435
## Intuition
35-
[中文题解](#中文题解)
3636

3737
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.
3838
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`.
3939
3. Traverse the elements of the array, and the loop condition is `while (left < right)`. In the loop body, `left += 1`, `right -= 1`.
4040
4. In the loop body, swap the two values.
4141
5. The above is the template for `two pointers` in `opposite directions`.
4242

43-
## Approach
43+
## Steps
4444
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+
4546
```ruby
4647
left = 0
4748
right = s.length - 1
4849
```
4950

5051
2. Traverse the elements of the array, and the loop condition is `while (left < right)`. In the loop body, `left += 1`, `right -= 1`.
52+
5153
```ruby
5254
left = 0
5355
right = s.length - 1
@@ -59,6 +61,7 @@ end
5961
```
6062

6163
3. In the loop body, swap the two values.
64+
6265
```ruby
6366
left = 0
6467
right = s.length - 1
@@ -72,10 +75,12 @@ end
7275
```
7376

7477
## Complexity
78+
7579
* Time: `O(n)`.
7680
* Space: `O(1)`.
7781

7882
## Java
83+
7984
```java
8085
class Solution {
8186
public void reverseString(char[] s) {
@@ -95,6 +100,7 @@ class Solution {
95100
```
96101

97102
## Python
103+
98104
```python
99105
class Solution:
100106
def reverseString(self, s: List[str]) -> None:
@@ -108,6 +114,7 @@ class Solution:
108114
```
109115

110116
## C++
117+
111118
```cpp
112119
class Solution {
113120
public:
@@ -126,6 +133,7 @@ public:
126133
```
127134

128135
## JavaScript
136+
129137
```javascript
130138
var reverseString = function (s) {
131139
let left = 0
@@ -141,6 +149,7 @@ var reverseString = function (s) {
141149
```
142150

143151
## C#
152+
144153
```c#
145154
public class Solution
146155
{
@@ -161,6 +170,7 @@ public class Solution
161170
```
162171

163172
## Go
173+
164174
```go
165175
func reverseString(s []byte) {
166176
left := 0
@@ -176,6 +186,7 @@ func reverseString(s []byte) {
176186
```
177187

178188
## Ruby
189+
179190
```ruby
180191
def reverse_string(s)
181192
left = 0
@@ -191,77 +202,31 @@ end
191202
```
192203

193204
## C
205+
194206
```c
195207
// Welcome to create a PR to complete the code of this language, thanks!
196208
```
197209

198210
## Kotlin
211+
199212
```kotlin
200213
// Welcome to create a PR to complete the code of this language, thanks!
201214
```
202215

203216
## Swift
217+
204218
```swift
205219
// Welcome to create a PR to complete the code of this language, thanks!
206220
```
207221

208222
## Rust
223+
209224
```rust
210225
// Welcome to create a PR to complete the code of this language, thanks!
211226
```
212227

213228
## Other languages
214-
```
215-
// Welcome to create a PR to complete the code of this language, thanks!
216-
```
217-
218-
## 力扣“344. 反转字符串”问题描述
219-
力扣链接:[344. 反转字符串](https://leetcode.cn/problems/reverse-string) ,难度:**简单**
220-
221-
编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 `s` 的形式给出。
222-
223-
不要给另外的数组分配额外的空间,你必须 **原地** **修改输入数组**、使用 `O(1)` 的额外空间解决这一问题。
224-
225-
### [示例 1]
226-
**输入**: `s = ["h","e","l","l","o"]`
227229

228-
**输出**: `["o","l","l","e","h"]`
229-
230-
# 中文题解
231-
## 思路
232-
1. 这种问题用编程语言内置的`sort()`方法可以一行代码解决。如果面试中出这种题目,出题人考察的应该是不用内置方法如何做。
233-
2.**方向相对**的两个指针,最初时一个指针指向下标`0`,另一个指针指向下标`s.length - 1`
234-
3. 遍历数组元素,循环条件为`while (left < right)`。在循环体内,`left += 1`, `right -= 1`
235-
4. 在循环体内,交换数组中两个下标对应的值。
236-
5. 以上为`方向相对`**双指针**的模板。
237-
238-
## 步骤
239-
1.**方向相对的两个指针**,最初时一个指针指向下标`0`,另一个指针指向下标`s.length - 1`
240-
```ruby
241-
left = 0
242-
right = s.length - 1
243230
```
244-
245-
2. 遍历数组元素,循环条件为`while (left < right)`。在循环体内,`left += 1`, `right -= 1`
246-
```ruby
247-
left = 0
248-
right = s.length - 1
249-
250-
while left < right # 1
251-
left += 1 # 2
252-
right -= 1 # 3
253-
end
254-
```
255-
256-
3. 在循环体内,交换数组中两个下标对应的值。
257-
```ruby
258-
left = 0
259-
right = s.length - 1
260-
261-
while left < right
262-
s[left], s[right] = s[right], s[left] # 1
263-
264-
left += 1
265-
right -= 1
266-
end
231+
// Welcome to create a PR to complete the code of this language, thanks!
267232
```

en/3001-4000/unorganized.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ Add a table to show the differences between A-Start and breadth-first search
200200
- 1514-path-with-maximum-probability.md
201201
- 1334 https://leetcode.com/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance
202202
- 752 a star
203+
- 3464 https://leetcode.cn/problems/maximize-the-distance-between-points-on-a-square
203204

204205
## other finished problems
205206
- https://leetcode.com/problems/k-closest-points-to-origin/

0 commit comments

Comments
 (0)