diff --git a/src/algorithms/string/is-palindrome/README.md b/src/algorithms/string/is-palindrome/README.md new file mode 100644 index 0000000000..c3565573d2 --- /dev/null +++ b/src/algorithms/string/is-palindrome/README.md @@ -0,0 +1,3 @@ +## 回文串 + +递归算法 diff --git a/src/algorithms/string/is-palindrome/__tests__/isPalindrome.test.js b/src/algorithms/string/is-palindrome/__tests__/isPalindrome.test.js new file mode 100644 index 0000000000..b4e14d39b6 --- /dev/null +++ b/src/algorithms/string/is-palindrome/__tests__/isPalindrome.test.js @@ -0,0 +1,11 @@ +import isPalindrome from '../isPalindrome'; + +describe('isPalindrome', () => { + it('should be a palindrome string', () => { + expect(isPalindrome('level')).toBe(true); + expect(isPalindrome('noon')).toBe(true); + }); + it('should not be a palindrome string', () => { + expect(isPalindrome('devel')).toBe(false); + }); +}); diff --git a/src/algorithms/string/is-palindrome/isPalindrome.js b/src/algorithms/string/is-palindrome/isPalindrome.js new file mode 100644 index 0000000000..1d3e5ca54f --- /dev/null +++ b/src/algorithms/string/is-palindrome/isPalindrome.js @@ -0,0 +1,14 @@ +export default function isPalindrome(str) { + const len = str.length; + const first = str[0]; + const last = str[str.length - 1]; + + if (len === 0 || len === 1) { + return true; + } + + if (first === last) { + return isPalindrome(str.substring(1, str.length - 1)); + } + return false; +} diff --git a/src/data-structures/priority-queue/PriorityQueue.js b/src/data-structures/priority-queue/PriorityQueue.js index 0b283c2c7a..06720c1952 100644 --- a/src/data-structures/priority-queue/PriorityQueue.js +++ b/src/data-structures/priority-queue/PriorityQueue.js @@ -5,7 +5,7 @@ import Comparator from '../../utils/comparator/Comparator'; // we take into account its priority instead of the element's value. export default class PriorityQueue extends MinHeap { constructor() { - // Call MinHip constructor first. + // Call MinHeap constructor first. super(); // Setup priorities map.