-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path541. Reverse String II.js
51 lines (44 loc) · 1.37 KB
/
541. Reverse String II.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
/**
* @param {string} s string to reverse
* @param {number} k reverse k characters
* @return {string} reversed string
*/
var reverseStr = function (s, k) {
let result = '';
let arr = [];
for (let i = 0; i < s.length; i += 2 * k) arr.push(s.substring(i, i + 2 * k));
arr.forEach(item => {
result += item.substring(0, k).split('').reverse().join('');
result += item.substring(0 + k, 2 * k);
});
return result;
};
// var reverseStr = function (s, k) {
// let result = '';
// for (let i = 0; i < s.length; i += 2 * k) {
// result += s.substring(i, i + k).split('').reverse().join('');
// result += s.substring(i + k, i + 2 * k);
// }
// return result;
// };
var reverseStr = function (s, k) {
const strArr = s.split('');
for (let i = 0; i < s.length; i += 2 * k) {
let left = i;
let right = Math.min(i + k - 1, s.length - 1);
while (left < right) {
const temp = strArr[left];
strArr[left] = strArr[right];
strArr[right] = temp;
left++;
right--;
}
}
return strArr.join('');
}
console.log(reverseStr('abcdefg', 2));
console.log(reverseStr('abcdegfhijklmnopqrstuvwxyz', 3));
console.log(reverseStr('abcdefg', 8));
console.log(reverseStr('abcd', 2));
console.log(reverseStr('ab', 3));
console.log(reverseStr('a', 2));