forked from qianlongo/fe-handwriting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
33.reverse-string.js
54 lines (43 loc) · 1009 Bytes
/
33.reverse-string.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// 反转字符串 https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/xnhbqj/
/**
*
*/
// 双指针
const reverseString = (s) => {
let i = 0
let j = s.length - 1
while (i < j) {
let temp = s[ i ]
s[ i ] = s[ j ]
console.log(i, j, temp, s[ j ], s[ i ])
s[ j ] = temp
i++
j--
}
return s
}
// 单指针,做对称交换
const reverseString2 = (s) => {
const len = s.length
const halfLen = len / 2
let i = 0
while (i < halfLen) {
const temp = s[ i ]
const tail = len - 1 - i
s[ i ] = s[ tail ]
s[ tail ] = temp
i++
}
return s
}
// 结构交换两个值
const reverseString3 = (s) => {
const len = s.length
for (let left = 0, right = len - 1; left < right; left++, right--) {
[ s[ right ], s[ left ] ] = [ s[ left ], s[ right ] ]
}
return s
}
console.log(reverseString(["h","e","l","l","o"]))
console.log(reverseString2(["h","e","l","l","o"]))
console.log(reverseString3(["h","e","l","l","o"]))