Skip to content

Commit 8955e5c

Browse files
hushicaihushicai
authored andcommitted
feat(string): 添加回文串递归算法
1 parent 94abfec commit 8955e5c

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 回文串
2+
3+
递归算法
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import isPalindrome from '../isPalindrome';
2+
3+
describe('isPalindrome', () => {
4+
it('should be a palindrome string', () => {
5+
expect(isPalindrome('level')).toBe(true);
6+
expect(isPalindrome('noon')).toBe(true);
7+
});
8+
it('should not be a palindrome string', () => {
9+
expect(isPalindrome('devel')).toBe(false);
10+
});
11+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export default function isPalindrome(str) {
2+
const len = str.length;
3+
const first = str[0];
4+
const last = str[str.length - 1];
5+
6+
if (len === 0 || len === 1) {
7+
return true;
8+
}
9+
10+
if (first === last) {
11+
return isPalindrome(str.substring(1, str.length - 1));
12+
}
13+
return false;
14+
}

0 commit comments

Comments
 (0)