From 8955e5c6c6883cf9098267f3d3c3431db4276195 Mon Sep 17 00:00:00 2001 From: hushicai Date: Tue, 4 Jun 2019 10:08:53 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat(string):=20=E6=B7=BB=E5=8A=A0=E5=9B=9E?= =?UTF-8?q?=E6=96=87=E4=B8=B2=E9=80=92=E5=BD=92=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/algorithms/string/is-palindrome/README.md | 3 +++ .../is-palindrome/__tests__/isPalindrome.test.js | 11 +++++++++++ .../string/is-palindrome/isPalindrome.js | 14 ++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 src/algorithms/string/is-palindrome/README.md create mode 100644 src/algorithms/string/is-palindrome/__tests__/isPalindrome.test.js create mode 100644 src/algorithms/string/is-palindrome/isPalindrome.js 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; +} From dd8e1101e68196165fe09ba35ad11e389eab83f1 Mon Sep 17 00:00:00 2001 From: hushicai Date: Thu, 6 Jun 2019 21:48:54 +0800 Subject: [PATCH 2/2] fix wrong spell --- src/data-structures/priority-queue/PriorityQueue.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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.