Skip to content

Commit 0ea46b4

Browse files
committed
add LeetCode 344. 反转字符串
1 parent 4c47062 commit 0ea46b4

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

Diff for: 双指针/LeetCode 344. 反转字符串.md

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
![](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9jZG4uanNkZWxpdnIubmV0L2doL2Nob2NvbGF0ZTE5OTkvY2RuL2ltZy8yMDIwMDgyODE0NTUyMS5qcGc?x-oss-process=image/format,png)
2+
>仰望星空的人,不应该被嘲笑
3+
4+
## 题目描述
5+
6+
编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 `char[]` 的形式给出。
7+
8+
不要给另外的数组分配额外的空间,你必须**原地修改输入数组**、使用 O(1) 的额外空间解决这一问题。
9+
10+
你可以假设数组中的所有字符都是 `ASCII` 码表中的可打印字符。
11+
12+
13+
14+
示例 1:
15+
16+
```javascript
17+
输入:["h","e","l","l","o"]
18+
输出:["o","l","l","e","h"]
19+
```
20+
21+
示例 2:
22+
23+
```javascript
24+
输入:["H","a","n","n","a","h"]
25+
输出:["h","a","n","n","a","H"]
26+
```
27+
28+
来源:力扣(LeetCode)
29+
链接:https://leetcode-cn.com/problems/reverse-string
30+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
31+
32+
## 解题思路
33+
34+
方法一:利用JS原生api
35+
36+
```javascript
37+
/**
38+
* @param {character[]} s
39+
* @return {void} Do not return anything, modify s in-place instead.
40+
*/
41+
var reverseString = function (s) {
42+
return s.reverse();
43+
};
44+
```
45+
46+
方法二:双指针,头尾交换
47+
48+
```javascript
49+
/**
50+
* @param {character[]} s
51+
* @return {void} Do not return anything, modify s in-place instead.
52+
*/
53+
var reverseString = function (s) {
54+
let i = 0, j = s.length - 1;
55+
while (i < j) {
56+
[s[i], s[j]] = [s[j], s[i]]; // 双指针,交换
57+
i++ , j--;
58+
}
59+
return s;
60+
};
61+
```
62+
63+
64+
## 最后
65+
文章产出不易,还望各位小伙伴们支持一波!
66+
67+
往期精选:
68+
69+
<a href="https://github.com/Chocolate1999/Front-end-learning-to-organize-notes">小狮子前端の笔记仓库</a>
70+
71+
<a href="https://github.com/Chocolate1999/leetcode-javascript">leetcode-javascript:LeetCode 力扣的 JavaScript 解题仓库,前端刷题路线(思维导图)</a>
72+
73+
小伙伴们可以在Issues中提交自己的解题代码,🤝 欢迎Contributing,可打卡刷题,Give a ⭐️ if this project helped you!
74+
75+
76+
<a href="https://yangchaoyi.vip/">访问超逸の博客</a>,方便小伙伴阅读玩耍~
77+
78+
![](https://img-blog.csdnimg.cn/2020090211491121.png#pic_center)
79+
80+
```javascript
81+
学如逆水行舟,不进则退
82+
```
83+
84+

0 commit comments

Comments
 (0)