Skip to content

Commit 0a43709

Browse files
committed
344. 反转字符串 (二刷)
1 parent ecb03de commit 0a43709

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

algorithms/reverse-string.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
* @return {void} Do not return anything, modify s in-place instead.
77
*/
88
var reverseString = function(s) {
9-
const len = s.length;
10-
let start = 0, end = len - 1;
9+
let left = 0;
10+
let right = s.length - 1;
1111

12-
while (start < end) {
13-
[s[start], s[end]] = [s[end], s[start]];
14-
start++;
15-
end--;
12+
while (left < right) {
13+
[s[left], s[right]] = [s[right], s[left]];
14+
left++;
15+
right--;
1616
}
1717
};

vs-lt/344.反转字符串.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* @lc app=leetcode.cn id=344 lang=javascript
3+
*
4+
* [344] 反转字符串
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* @param {character[]} s
10+
* @return {void} Do not return anything, modify s in-place instead.
11+
*/
12+
var reverseString = function(s) {
13+
let left = 0;
14+
let right = s.length - 1;
15+
16+
while (left < right) {
17+
[s[left], s[right]] = [s[right], s[left]];
18+
left++;
19+
right--;
20+
}
21+
};
22+
// @lc code=end
23+

0 commit comments

Comments
 (0)