Skip to content

Commit dbf66f5

Browse files
author
zhangxing
committed
feat: 添加反转字符串2
1 parent 6176fd2 commit dbf66f5

File tree

8 files changed

+38
-46
lines changed

8 files changed

+38
-46
lines changed

反转字符串2.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// https://leetcode-cn.com/problems/reverse-string-ii/
2+
3+
// 给定一个字符串 s 和一个整数 k,从字符串开头算起,每计数至 2k 个字符,就反转这 2k 字符中的前 k 个字符。
4+
5+
// 如果剩余字符少于 k 个,则将剩余字符全部反转。
6+
// 如果剩余字符小于 2k 但大于或等于 k 个,则反转前 k 个字符,其余字符保持原样。
7+
8+
// 示例 1:
9+
10+
// 输入:s = "abcdefg", k = 2
11+
// 输出:"bacdfeg"
12+
// 示例 2:
13+
14+
// 输入:s = "abcd", k = 2
15+
// 输出:"bacd"
16+
17+
var reverseStr = function (s, k) {
18+
// 相同的处理逻辑考虑递归
19+
if (s.length < k) {
20+
return s.split('').reverse().join('');
21+
} else if (s.length <= 2 * k) {
22+
return s.slice(0, k).split('').reverse().join('') + s.slice(k);
23+
} else {
24+
return reverseStr(s.slice(0, 2 * k), k) + reverseStr(s.slice(2 * k), k);
25+
}
26+
};
27+
console.log(reverseStr('abcdefg', 2));

回文数.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* @param {number} x
3+
* @return {boolean}
4+
*/
5+
var isPalindrome = function (x) {
6+
if (typeof x !== 'number') return false;
7+
8+
const s = x + '';
9+
if (s === s.split('').reverse().join()) return true;
10+
return false;
11+
};
File renamed without changes.

栈和队列/641.设计双端循环队列.js

Whitespace-only changes.

栈和队列/859.亲密字符串.js

Whitespace-only changes.

栈和队列/86.分隔链表.js

Whitespace-only changes.

栈和队列/933.最近请求次数.js

Whitespace-only changes.

链表/2.js

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)