From 8ad58b2b86db7c999b479bd5321a23378c296ecb Mon Sep 17 00:00:00 2001 From: Du Teng Date: Sat, 11 Jul 2015 18:54:16 +0800 Subject: [PATCH 001/126] update --- .../README.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Algorithms/LowestCommonAncestorofaBinarySearchTree/README.md diff --git a/Algorithms/LowestCommonAncestorofaBinarySearchTree/README.md b/Algorithms/LowestCommonAncestorofaBinarySearchTree/README.md new file mode 100644 index 0000000..90c99ee --- /dev/null +++ b/Algorithms/LowestCommonAncestorofaBinarySearchTree/README.md @@ -0,0 +1,32 @@ +https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/ +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @param {TreeNode} p + * @param {TreeNode} q + * @return {TreeNode} + */ +var lowestCommonAncestor = function(root, p, q) { + var tmp; + if(p > q) { + tmp = p; + p = q; + q = tmp; + } + if(root.val >= p.val && root.val <= q.val) { + return root; + }else if(p.val < root.val & q.val < root.val) { + return lowestCommonAncestor(root.left, p, q); + } else if(p.val > root.val && q.val > root.val){ + return lowestCommonAncestor(root.right, p, q); + } + +}; +``` From 2db614b38343915c25ffbbf2e1a4d90b37bbca96 Mon Sep 17 00:00:00 2001 From: Du Teng Date: Sat, 11 Jul 2015 18:58:20 +0800 Subject: [PATCH 002/126] Update README.md --- Algorithms/LowestCommonAncestorofaBinarySearchTree/README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Algorithms/LowestCommonAncestorofaBinarySearchTree/README.md b/Algorithms/LowestCommonAncestorofaBinarySearchTree/README.md index 90c99ee..8a9ce0d 100644 --- a/Algorithms/LowestCommonAncestorofaBinarySearchTree/README.md +++ b/Algorithms/LowestCommonAncestorofaBinarySearchTree/README.md @@ -1,4 +1,7 @@ -https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/ +#[LowestCommonAncestorofaBinarySearchTree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree) + +##JavaScript + ```javascript /** * Definition for a binary tree node. From 31e89ba22bda400ee5acc0bd629747e6d289f9e6 Mon Sep 17 00:00:00 2001 From: Du Teng Date: Sat, 11 Jul 2015 19:01:18 +0800 Subject: [PATCH 003/126] update --- Algorithms/SameTree/README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Algorithms/SameTree/README.md diff --git a/Algorithms/SameTree/README.md b/Algorithms/SameTree/README.md new file mode 100644 index 0000000..bb0635c --- /dev/null +++ b/Algorithms/SameTree/README.md @@ -0,0 +1,28 @@ +#[SameTree](https://leetcode.com/problems/same-tree/) + +##JavaScript + +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} p + * @param {TreeNode} q + * @return {boolean} + */ +var isSameTree = function(p, q) { + if(p == null || q == null) { + return p == q; + } + if(p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right)) { + return true; + } else { + return false; + } +}; +``` From 982425c2b93746c8174a3ff34518f424755adaea Mon Sep 17 00:00:00 2001 From: Du Teng Date: Sat, 11 Jul 2015 19:29:17 +0800 Subject: [PATCH 004/126] update --- Algorithms/PathSum/README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Algorithms/PathSum/README.md diff --git a/Algorithms/PathSum/README.md b/Algorithms/PathSum/README.md new file mode 100644 index 0000000..1a6c2d5 --- /dev/null +++ b/Algorithms/PathSum/README.md @@ -0,0 +1,31 @@ +#[PathSum](https://leetcode.com/problems/path-sum/) + +##JavaScript + +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @param {number} sum + * @return {boolean} + */ +var hasPathSum = function(root, sum) { + if(root == null) { + return false; + } + if(root.left == null && root.right == null && sum == root.val) { + return true; + } + if(hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val)) { + return true; + } else { + return false; + } +}; +``` From f8704b092e34f36a091de18e86cc4b5b7772660b Mon Sep 17 00:00:00 2001 From: Du Teng Date: Sat, 11 Jul 2015 19:30:44 +0800 Subject: [PATCH 005/126] update --- Algorithms/{PathSum => Path Sum}/README.md | 0 Algorithms/{SameTree => Same Tree}/README.md | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename Algorithms/{PathSum => Path Sum}/README.md (100%) rename Algorithms/{SameTree => Same Tree}/README.md (100%) diff --git a/Algorithms/PathSum/README.md b/Algorithms/Path Sum/README.md similarity index 100% rename from Algorithms/PathSum/README.md rename to Algorithms/Path Sum/README.md diff --git a/Algorithms/SameTree/README.md b/Algorithms/Same Tree/README.md similarity index 100% rename from Algorithms/SameTree/README.md rename to Algorithms/Same Tree/README.md From 1378dcb9856a2d082e50c0903a8e18e11c57ad43 Mon Sep 17 00:00:00 2001 From: Du Teng Date: Sat, 11 Jul 2015 19:33:39 +0800 Subject: [PATCH 006/126] update --- .../README.md | 0 .../Maximum Depth of Binary Tree/README.md | 24 +++++++++++++++++++ 2 files changed, 24 insertions(+) rename Algorithms/{LowestCommonAncestorofaBinarySearchTree => Lowest Common Ancestor of a Binary Search Tree}/README.md (100%) create mode 100644 Algorithms/Maximum Depth of Binary Tree/README.md diff --git a/Algorithms/LowestCommonAncestorofaBinarySearchTree/README.md b/Algorithms/Lowest Common Ancestor of a Binary Search Tree/README.md similarity index 100% rename from Algorithms/LowestCommonAncestorofaBinarySearchTree/README.md rename to Algorithms/Lowest Common Ancestor of a Binary Search Tree/README.md diff --git a/Algorithms/Maximum Depth of Binary Tree/README.md b/Algorithms/Maximum Depth of Binary Tree/README.md new file mode 100644 index 0000000..dd9c153 --- /dev/null +++ b/Algorithms/Maximum Depth of Binary Tree/README.md @@ -0,0 +1,24 @@ +#[Maximum Depth of Binary Tree ](https://leetcode.com/problems/maximum-depth-of-binary-tree/) + +##JavaScript + +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number} + */ +var maxDepth = function(root) { + if(root == null) { + return 0; + } else { + return Math.max(maxDepth(root.left), maxDepth(root.right)) +1; + } +}; +``` From 87049b0b1a86a8ba96df5d1cc4db6c6d1c85d7ca Mon Sep 17 00:00:00 2001 From: Du Teng Date: Sun, 12 Jul 2015 00:17:14 +0800 Subject: [PATCH 007/126] update --- Algorithms/Merge Sorted Array/README.md | 24 +++++++++++++++ Algorithms/Plus One/README.md | 28 +++++++++++++++++ .../READMD.md | 24 +++++++++++++++ Algorithms/Remove Element/README.md | 30 +++++++++++++++++++ Algorithms/Valid Parentheses/README.md | 29 ++++++++++++++++++ 5 files changed, 135 insertions(+) create mode 100644 Algorithms/Merge Sorted Array/README.md create mode 100644 Algorithms/Plus One/README.md create mode 100644 Algorithms/Remove Duplicates from Sorted Array/READMD.md create mode 100644 Algorithms/Remove Element/README.md create mode 100644 Algorithms/Valid Parentheses/README.md diff --git a/Algorithms/Merge Sorted Array/README.md b/Algorithms/Merge Sorted Array/README.md new file mode 100644 index 0000000..07a6c50 --- /dev/null +++ b/Algorithms/Merge Sorted Array/README.md @@ -0,0 +1,24 @@ +#[Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/) + +##JavaScript + +```javascript +/** + * @param {number[]} nums1 + * @param {number} m + * @param {number[]} nums2 + * @param {number} n + * @return {void} Do not return anything, modify nums1 in-place instead. + */ +var merge = function(nums1, m, nums2, n) { + while(n > 0) { + if(m <= 0 || nums1[m - 1] <= nums2[n -1]) { + nums1[m + n - 1] = nums2[n - 1]; + n--; + } else { + nums1[m + n - 1] = nums1[m - 1]; + m--; + } + } +}; +``` diff --git a/Algorithms/Plus One/README.md b/Algorithms/Plus One/README.md new file mode 100644 index 0000000..2db8b09 --- /dev/null +++ b/Algorithms/Plus One/README.md @@ -0,0 +1,28 @@ +#[Plus One](https://leetcode.com/problems/plus-one/) + +##JavaScript + +```javascript +/** + * @param {number[]} digits + * @return {number[]} + */ +var plusOne = function(digits) { + digits.unshift(0); + var tail = digits.length - 1; + digits[tail]++; + while(tail > 0) { + if(digits[tail] == 10) { + digits[tail] = 0; + tail--; + digits[tail]++; + } else { + tail--; + } + } + if(digits[0] == 0) { + digits.shift(); + } + return digits; +}; +``` diff --git a/Algorithms/Remove Duplicates from Sorted Array/READMD.md b/Algorithms/Remove Duplicates from Sorted Array/READMD.md new file mode 100644 index 0000000..709ef82 --- /dev/null +++ b/Algorithms/Remove Duplicates from Sorted Array/READMD.md @@ -0,0 +1,24 @@ +#[Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) + +##JavaScript + +```javascript +/** + * @param {number[]} nums + * @return {number} + */ +var removeDuplicates = function(nums) { + var i = 0; + var j = 0; + while(j < nums.length) { + if(nums[i] == nums[j]) { + j++; + } else { + i++; + nums[i] = nums[j]; + j++; + } + } + return i + 1; +}; +``` diff --git a/Algorithms/Remove Element/README.md b/Algorithms/Remove Element/README.md new file mode 100644 index 0000000..f46678a --- /dev/null +++ b/Algorithms/Remove Element/README.md @@ -0,0 +1,30 @@ +#[Remove Element](https://leetcode.com/problems/remove-element/) + +##JavaScript + +```javascript +/** + * @param {number[]} nums + * @param {number} val + * @return {number} + */ +var removeElement = function(nums, val) { + var head = 0; + var tail = nums.length - 1; + while(head <= tail) { + if(nums[head] != val) { + head++; + continue; + } + + if(nums[tail] == val) { + tail--; + continue; + } + + nums[head] = nums[tail]; + tail--; + } + return head; +}; +``` diff --git a/Algorithms/Valid Parentheses/README.md b/Algorithms/Valid Parentheses/README.md new file mode 100644 index 0000000..52521ba --- /dev/null +++ b/Algorithms/Valid Parentheses/README.md @@ -0,0 +1,29 @@ +#[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) + +##JavaScript + +```javascript +/** + * @param {string} s + * @return {boolean} + */ +var isValid = function(s) { + var stack = []; + var match = { + '_)': '(', + '_]': '[', + '_}': '{' + }; + var len; + for(var i = 0; i < s.length; i++) { + len = stack.length; + if(len > 0 && stack[len-1] == match['_' + s[i]]) { + stack.pop(); + } else { + stack.push(s[i]); + } + } + return stack.length == 0 ? true : false; +}; + +``` From b1f6d9b628ac55a9e2a227f136a1c8080ac961de Mon Sep 17 00:00:00 2001 From: Du Teng Date: Sun, 12 Jul 2015 00:19:05 +0800 Subject: [PATCH 008/126] update --- .../Remove Duplicates from Sorted Array/{READMD.md => README.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Algorithms/Remove Duplicates from Sorted Array/{READMD.md => README.md} (100%) diff --git a/Algorithms/Remove Duplicates from Sorted Array/READMD.md b/Algorithms/Remove Duplicates from Sorted Array/README.md similarity index 100% rename from Algorithms/Remove Duplicates from Sorted Array/READMD.md rename to Algorithms/Remove Duplicates from Sorted Array/README.md From a5213784056634afcae983f68e35a39f3605f524 Mon Sep 17 00:00:00 2001 From: Du Teng Date: Sun, 12 Jul 2015 00:20:13 +0800 Subject: [PATCH 009/126] update --- .../Lowest Common Ancestor of a Binary Search Tree/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Algorithms/Lowest Common Ancestor of a Binary Search Tree/README.md b/Algorithms/Lowest Common Ancestor of a Binary Search Tree/README.md index 8a9ce0d..87d809e 100644 --- a/Algorithms/Lowest Common Ancestor of a Binary Search Tree/README.md +++ b/Algorithms/Lowest Common Ancestor of a Binary Search Tree/README.md @@ -1,4 +1,4 @@ -#[LowestCommonAncestorofaBinarySearchTree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree) +#[Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree) ##JavaScript From 91ba29088f61da43ac9a9d4933e943ba00a9a5c4 Mon Sep 17 00:00:00 2001 From: Du Teng Date: Sun, 12 Jul 2015 00:37:46 +0800 Subject: [PATCH 010/126] Update README.md --- README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 83934dd..e4d386e 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,10 @@ leedcode ======== -Solution of [leetcode](http://oj.leetcode.com/problems/) +My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) + + +| No | Title | Source Code | Difficulty | +|----| ----- | -------- | ---------- | +|235|Lowest Common Ancestor of a Binary SearchTree|[JavaScrpt](https://github.com/duteng/leedcode/tree/master/Algorithms/Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree)|Easy| +|104|Maximum Depth of Binary Tree|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Maximum%20Depth%20of%20Binary%20Tree)|Easy| +|88|Merge Sorted Array|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Merge%20Sorted%20Array)|Easy| From 29085e8e2df8eea8a87192ffc212dde0eebcb5f7 Mon Sep 17 00:00:00 2001 From: Du Teng Date: Sun, 12 Jul 2015 00:55:16 +0800 Subject: [PATCH 011/126] update --- Algorithms/README.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 Algorithms/README.md diff --git a/Algorithms/README.md b/Algorithms/README.md new file mode 100644 index 0000000..f683342 --- /dev/null +++ b/Algorithms/README.md @@ -0,0 +1,6 @@ +#[]() + +##JavaScript + +```javascript +``` From dd12b73f3e2fbf1e737d54743c23d692ae4eb056 Mon Sep 17 00:00:00 2001 From: Du Teng Date: Sun, 12 Jul 2015 03:07:59 +0800 Subject: [PATCH 012/126] update --- Algorithms/Compare Version Numbers/README.md | 30 ++++++++++ Algorithms/Largest Number/README.md | 55 +++++++++++++++++++ Algorithms/Permutations/README.md | 31 +++++++++++ .../Reverse Words in a String/README.md | 16 ++++++ 4 files changed, 132 insertions(+) create mode 100644 Algorithms/Compare Version Numbers/README.md create mode 100644 Algorithms/Largest Number/README.md create mode 100644 Algorithms/Permutations/README.md create mode 100644 Algorithms/Reverse Words in a String/README.md diff --git a/Algorithms/Compare Version Numbers/README.md b/Algorithms/Compare Version Numbers/README.md new file mode 100644 index 0000000..8e3dfa8 --- /dev/null +++ b/Algorithms/Compare Version Numbers/README.md @@ -0,0 +1,30 @@ +#[Compare Version Numbers ](https://leetcode.com/problems/compare-version-numbers/) + +##JavaScript + +```javascript +/** + * @param {string} version1 + * @param {string} version2 + * @return {number} + */ +var compareVersion = function(version1, version2) { + var arr1 = version1.split('.').map(function(item) { + return parseInt(item); + }); + var arr2 = version2.split('.').map(function(item) { + return parseInt(item); + }); + var a, b; + for(var i = 0; i < arr1.length || i < arr2.length; i++) { + a = i >= arr1.length ? 0 : arr1[i]; + b = i >= arr2.length ? 0 : arr2[i]; + if(a > b) { + return 1; + } else if(a < b) { + return -1; + } + } + return 0; +}; +``` diff --git a/Algorithms/Largest Number/README.md b/Algorithms/Largest Number/README.md new file mode 100644 index 0000000..3732a03 --- /dev/null +++ b/Algorithms/Largest Number/README.md @@ -0,0 +1,55 @@ +#[Largest Number ](https://leetcode.com/problems/largest-number/) + +##JavaScript + +```javascript +/** + * @param {number[]} nums + * @return {string} + */ +var largestNumber = function(nums) { + var retStr = nums.sort(function(num1, num2) { + var arr1 = num1 + ''.split(); + var arr2 = num2 + ''.split(); + var len1 = arr1.length; + var len2 = arr2.length; + var a, b; + + for (var i = 0; i < len1 || i < len2; i++) { + a = i >= arr1.length ? arr1[i % len1] : arr1[i]; + b = i >= arr2.length ? arr2[i % len2] : arr2[i]; + if (a != b) { + return b - a; + } + } + + // [121,12] 12 > 121; [212,21] 212 > 21 + var isRise; + var checkArr = len1 > len2 ? arr1 : arr2; + for (var j = 0; j < checkArr.length - 1; j++) { + if (checkArr[j] != checkArr[j + 1]) { + if (checkArr[j] > checkArr[j + 1]) { + isRise = false; + break; + } else { + isRise = true; + break; + } + } + } + if (isRise) { + return len1 - len2; + } else { + return len2 - len1; + } + + + }).join(''); + + if (retStr[0] == '0') { + return '0'; + } else { + return retStr; + } +}; +``` diff --git a/Algorithms/Permutations/README.md b/Algorithms/Permutations/README.md new file mode 100644 index 0000000..42845ca --- /dev/null +++ b/Algorithms/Permutations/README.md @@ -0,0 +1,31 @@ +#[Permutations ](https://leetcode.com/problems/permutations/) + +##JavaScript + +```javascript +/** + * @param {number[]} nums + * @return {number[][]} + */ +var permute = function(nums) { + var ret = []; + if(nums.length == 1) { + ret.push(nums); + return ret; + } + + var item, item_copy; + var head = nums.shift(); + var pre_ret = permute(nums); + + for(var i = 0; i < pre_ret.length; i++ ) { + item = pre_ret[i]; + for(var j = 0; j <= item.length; j++) { + item_copy = [].slice.call(item); + item_copy.splice(j, 0, head); + ret.push(item_copy); + } + } + return ret; +}; +``` diff --git a/Algorithms/Reverse Words in a String/README.md b/Algorithms/Reverse Words in a String/README.md new file mode 100644 index 0000000..6eaada9 --- /dev/null +++ b/Algorithms/Reverse Words in a String/README.md @@ -0,0 +1,16 @@ +#[Reverse Words in a String ](https://leetcode.com/problems/reverse-words-in-a-string/) + +##JavaScript + +```javascript +/** + * @param {string} str + * @returns {string} + */ +var reverseWords = function(str) { + return str.split(' ').reverse().filter(function(item) { + return '' != item; + }).join(' ').trim(); +}; + +``` From 7e9caeb61fd0957b980acd63f8ef2e048a1d898b Mon Sep 17 00:00:00 2001 From: Du Teng Date: Sun, 12 Jul 2015 03:12:41 +0800 Subject: [PATCH 013/126] update --- 1-reverse_words_in_a_string.cpp | 38 -------- 16-single_number.cpp | 29 ------ 2-evaluate_reverse_polish_notation.cpp | 101 --------------------- 3-max_points_on_a_line.cpp | 120 ------------------------- permutations.cpp | 80 ----------------- set-matrix-zeroes.cpp | 99 -------------------- 6 files changed, 467 deletions(-) delete mode 100644 1-reverse_words_in_a_string.cpp delete mode 100644 16-single_number.cpp delete mode 100644 2-evaluate_reverse_polish_notation.cpp delete mode 100644 3-max_points_on_a_line.cpp delete mode 100644 permutations.cpp delete mode 100644 set-matrix-zeroes.cpp diff --git a/1-reverse_words_in_a_string.cpp b/1-reverse_words_in_a_string.cpp deleted file mode 100644 index d692f61..0000000 --- a/1-reverse_words_in_a_string.cpp +++ /dev/null @@ -1,38 +0,0 @@ -class Solution { -public: - void reverseWords(string &s) { - istringstream is(s); - string tmp=""; - string out=""; - while(is>>tmp){ - tmp+=" "; - tmp+=out; - out=tmp; - } - s=out.substr(0,out.length()-1); - } -}; - -/* -#include -#include -#include -using namespace std; - -int main () { - - int n,val; - string stringvalues; - - stringvalues = "125 320 512 750 333"; - istringstream iss(stringvalues); - - for (n=0; n<5; n++) - { - iss >> val; - cout << val*2 << endl; - } - - return 0; -} -*/ diff --git a/16-single_number.cpp b/16-single_number.cpp deleted file mode 100644 index 06cb336..0000000 --- a/16-single_number.cpp +++ /dev/null @@ -1,29 +0,0 @@ -class Solution { -public: - int singleNumber(int A[], int n) { - int result = 0; - for(int i = 0; i < n; i++) { - result = result ^ A[i]; - } - return result; - } -}; -/* -#include -using namespace std; - -int singleNumber(int A[], int n) { - int result = 0; - for(int i = 0; i < n; i++) { - result = result ^ A[i]; - } - return result; -} -int main() { - int arr[] = {2,3,4,5,6,4,6,2,5}; - int single = singleNumber(arr, 9); - cout<<"single:"< &tokens) { - vector::iterator string_ite; - vector stack; - int result; - for(string_ite=tokens.begin ();string_ite!=tokens.end ();string_ite++) { - string s = *string_ite; - if(s.compare("+") == 0 || - s.compare("-") == 0 || - s.compare("*") == 0 || - s.compare("/") == 0) { - int a = stack[stack.size()-2]; - int b = stack[stack.size()-1]; - - stack.pop_back(); - stack.pop_back(); - if(s.compare("+") == 0) { - result = a+b; - } - if(s.compare("-") == 0) { - result = a-b; - } - if(s.compare("*") == 0) { - result = a*b; - } - if(s.compare("/") == 0) { - result = a/b; - } - stack.push_back(result); - } else { - int number = atoi(s.c_str()); - stack.push_back(number); - } - - } - return stack[0]; - } -}; - - -/* -#include -#include -#include -#include - -using namespace std; - -void print_vector(vector v) { - for (int index=0;index &token) { - vector::iterator string_ite; - vector stack; - int result; - for(string_ite=token.begin ();string_ite!=token.end ();string_ite++) { - string s = *string_ite; - if(s.compare("+") == 0 || - s.compare("-") == 0 || - s.compare("*") == 0 || - s.compare("/") == 0) { - int a = stack[stack.size()-2]; - int b = stack[stack.size()-1]; - - stack.pop_back(); - stack.pop_back(); - if(s.compare("+") == 0) { - result = a+b; - } - if(s.compare("-") == 0) { - result = a-b; - } - if(s.compare("*") == 0) { - result = a*b; - } - if(s.compare("/") == 0) { - result = a/b; - } - stack.push_back(result); - } else { - int number = atoi(s.c_str()); - stack.push_back(number); - } - - } - return stack[0]; -} - -int main() { - string str[] = {"8", "2", "/"}; - vector rpn(str,str+sizeof(str)/sizeof(str[0])); - int ret = evalRPN (rpn); - cout< &points) { - if(points.size() < 3) { - return points.size(); - } - int maxPoints = 0; //the max point in line - int size = points.size(); - map count; - map::iterator iter; - for(int i = 0; i < size; i++ ) { - int x1 = points[i].x; - int y1 = points[i].y; - int coincideCount = 0; //number of duplicate points - count.clear(); - count[(double)INT_MIN] = 0; - for(int j = i + 1; j < size; j++) { - int x2 = points[j].x; - int y2 = points[j].y; - if(x1 == x2 && y1 == y2) { - coincideCount++; - } else if(x1 == x2){ - count[(double)INT_MIN]++; - } else { - double slope = 1.0*(y1-y2)/(x1-x2); - count[slope]++; - } - } - for(iter = count.begin(); iter != count.end(); iter++) { - if(iter->second + coincideCount > maxPoints) { - maxPoints = iter->second + coincideCount; - } - } - } - maxPoints = maxPoints + 1; - return maxPoints; - } -}; - - - -/* -#include -#include -#include -#include -#include -#include - -using namespace std; - -struct Point { - int x; - int y; - Point() : x(0), y(0) {} - Point(int a, int b) : x(a), y(b) {} -}; - -int maxPoints(vector &points) { - if(points.size() < 3) { - return points.size(); - } - int maxPoints = 0; //the max point in line - int size = points.size(); - map count; - map::iterator iter; - for(int i = 0; i < size; i++ ) { - int x1 = points[i].x; - int y1 = points[i].y; - int coincideCount = 0; //number of duplicate points - count.clear(); - count[(double)INT_MIN] = 0; - for(int j = i + 1; j < size; j++) { - int x2 = points[j].x; - int y2 = points[j].y; - if(x1 == x2 && y1 == y2) { - coincideCount++; - } else if(x1 == x2){ - count[(double)INT_MIN]++; - } else { - double slope = 1.0*(y1-y2)/(x1-x2); - count[slope]++; - } - } - for(iter = count.begin(); iter != count.end(); iter++) { - if(iter->second + coincideCount > maxPoints) { - maxPoints = iter->second + coincideCount; - } - } - } - maxPoints = maxPoints + 1; - return maxPoints; -} -int main() { - Point p1 = Point(1,1); - Point p2 = Point(2,2); - Point p3 = Point(4,3); - Point p4 = Point(4,4); - vector points; - points.push_back(p1); - points.push_back(p2); - points.push_back(p3); - points.push_back(p4); - - int maxNumber = maxPoints(points); - cout< > permute(vector &num) { - vector > result, tmp_result; - vector tmp_v; - int size = num.size(),last, m, n; - - if(size == 1) { - result.push_back(num); - } else { - last = num[size - 1]; - num.pop_back(); - tmp_result = permute(num); - m = tmp_result.size(); - n = tmp_result[0].size(); - for(int i = 0; i < m; i++) { - for(int j = 0; j <= n; j++) { - tmp_v = tmp_result[i]; - tmp_v.insert(tmp_v.begin() + j, last); - result.push_back(tmp_v); - } - } - } - return result; - } -}; - -/* -#include -#include -using namespace std; - -template -void print2(vector > v) { - if(v.empty()) { - return; - } - int m = v.size(), - n = v[0].size(); - for(int i = 0; i < m; i++) { - for(int j = 0; j < n; j++) { - cout< > permute(vector &num) { - vector > result, tmp_result; - vector tmp_v; - vector::iterator iter; - int size = num.size(),last, m, n; - - if(size == 1) { - result.push_back(num); - } else { - last = num[size - 1]; - num.pop_back(); - tmp_result = permute(num); - m = tmp_result.size(); - n = tmp_result[0].size(); - for(int i = 0; i < m; i++) { - for(int j = 0; j <= n; j++) { - tmp_v = tmp_result[i]; - tmp_v.insert(tmp_v.begin() + j, last); - result.push_back(tmp_v); - } - } - } - return result; -} - -int main() { - vector > result; - int arr[] = {5,6,7,8}; - vector v (arr, arr + sizeof(arr) / sizeof(arr[0]) ); - result = permute(v); - print2(result); -} -*/ diff --git a/set-matrix-zeroes.cpp b/set-matrix-zeroes.cpp deleted file mode 100644 index afb0166..0000000 --- a/set-matrix-zeroes.cpp +++ /dev/null @@ -1,99 +0,0 @@ -class Solution { -public: - void setZeroes(vector > &matrix) { - const int m = matrix.size(); - const int n = matrix[0].size(); - vector row(m, false); - vector col(n, false); - - for (int i = 0; i < m; ++i) { - for (int j = 0; j < n; ++j) { - if (matrix[i][j] == 0) { - row[i] = col[j] = true; - } - } - } - for (int i = 0; i < m; ++i) { - if (row[i]) { - fill(&matrix[i][0], &matrix[i][0] + n, 0); - } - } - for (int j = 0; j < n; ++j) { - if (col[j]) { - for (int i = 0; i < m; ++i) { - matrix[i][j] = 0; - } - } - } - } -}; -/* -#include -#include - -using namespace std; - - -template -void print(vector v) { - int m = v.size(), - n = v[0].size(), - i, j; - for(i = 0; i < m; i++) { - for(j = 0; j < n; j++) { - cout< > &matrix) { - const int m = matrix.size(); - const int n = matrix[0].size(); - vector row(m, false); - vector col(n, false); - - for (int i = 0; i < m; ++i) { - for (int j = 0; j < n; ++j) { - if (matrix[i][j] == 0) { - row[i] = col[j] = true; - } - } - } - for (int i = 0; i < m; ++i) { - if (row[i]) { - fill(&matrix[i][0], &matrix[i][0] + n, 0); - } - } - for (int j = 0; j < n; ++j) { - if (col[j]) { - for (int i = 0; i < m; ++i) { - matrix[i][j] = 0; - } - } - } -} - - -int main() { - vector > matrix; - int arr1[] = {16,2,77,29}; - int arr2[] = {16,0,77,29}; - int arr3[] = {16,2,77,29}; - int arr4[] = {16,2,77,29}; - vector vec1 (arr1, arr1 + sizeof(arr1) / sizeof(arr1[0]) ); - vector vec2 (arr2, arr2 + sizeof(arr2) / sizeof(arr2[0]) ); - vector vec3 (arr3, arr3 + sizeof(arr3) / sizeof(arr3[0]) ); - vector vec4 (arr4, arr4 + sizeof(arr4) / sizeof(arr4[0]) ); - - matrix.push_back(vec1); - matrix.push_back(vec2); - matrix.push_back(vec3); - matrix.push_back(vec4); - print(matrix); - setZeroes(matrix); - print(matrix); - return 0; -} - -*/ From 6959d9cb6f899dece955691a6d95e09a930bfbfd Mon Sep 17 00:00:00 2001 From: Du Teng Date: Thu, 20 Aug 2015 12:26:06 +0800 Subject: [PATCH 014/126] update --- Algorithms/Compare Version Numbers/README.md | 3 +- Algorithms/Largest Number/README.md | 3 +- .../README.md | 3 +- .../Maximum Depth of Binary Tree/README.md | 3 +- Algorithms/Merge Sorted Array/README.md | 3 +- Algorithms/Path Sum/README.md | 3 +- Algorithms/Permutations/README.md | 3 +- Algorithms/Plus One/README.md | 3 +- Algorithms/README.md | 6 --- .../README.md | 3 +- Algorithms/Remove Element/README.md | 3 +- .../Reverse Words in a String/README.md | 3 +- Algorithms/Same Tree/README.md | 3 +- Algorithms/Valid Parentheses/README.md | 3 +- README.md | 29 +++++++---- generate.js | 49 +++++++++++++++++++ 16 files changed, 94 insertions(+), 29 deletions(-) delete mode 100644 Algorithms/README.md create mode 100644 generate.js diff --git a/Algorithms/Compare Version Numbers/README.md b/Algorithms/Compare Version Numbers/README.md index 8e3dfa8..3f59680 100644 --- a/Algorithms/Compare Version Numbers/README.md +++ b/Algorithms/Compare Version Numbers/README.md @@ -1,5 +1,6 @@ #[Compare Version Numbers ](https://leetcode.com/problems/compare-version-numbers/) - +######No:`165` +######Difficulty:`Easy` ##JavaScript ```javascript diff --git a/Algorithms/Largest Number/README.md b/Algorithms/Largest Number/README.md index 3732a03..4610f4f 100644 --- a/Algorithms/Largest Number/README.md +++ b/Algorithms/Largest Number/README.md @@ -1,5 +1,6 @@ #[Largest Number ](https://leetcode.com/problems/largest-number/) - +######No:`179` +######Difficulty:`Medium` ##JavaScript ```javascript diff --git a/Algorithms/Lowest Common Ancestor of a Binary Search Tree/README.md b/Algorithms/Lowest Common Ancestor of a Binary Search Tree/README.md index 87d809e..17373ef 100644 --- a/Algorithms/Lowest Common Ancestor of a Binary Search Tree/README.md +++ b/Algorithms/Lowest Common Ancestor of a Binary Search Tree/README.md @@ -1,5 +1,6 @@ #[Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree) - +######No:`235` +######Difficulty:`Easy` ##JavaScript ```javascript diff --git a/Algorithms/Maximum Depth of Binary Tree/README.md b/Algorithms/Maximum Depth of Binary Tree/README.md index dd9c153..7bd6ce6 100644 --- a/Algorithms/Maximum Depth of Binary Tree/README.md +++ b/Algorithms/Maximum Depth of Binary Tree/README.md @@ -1,5 +1,6 @@ #[Maximum Depth of Binary Tree ](https://leetcode.com/problems/maximum-depth-of-binary-tree/) - +######No:`104` +######Difficulty:`Easy` ##JavaScript ```javascript diff --git a/Algorithms/Merge Sorted Array/README.md b/Algorithms/Merge Sorted Array/README.md index 07a6c50..bc6e6e4 100644 --- a/Algorithms/Merge Sorted Array/README.md +++ b/Algorithms/Merge Sorted Array/README.md @@ -1,5 +1,6 @@ #[Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/) - +######No:`88` +######Difficulty:`Easy` ##JavaScript ```javascript diff --git a/Algorithms/Path Sum/README.md b/Algorithms/Path Sum/README.md index 1a6c2d5..a22fc7f 100644 --- a/Algorithms/Path Sum/README.md +++ b/Algorithms/Path Sum/README.md @@ -1,5 +1,6 @@ #[PathSum](https://leetcode.com/problems/path-sum/) - +######No:`112` +######Difficulty:`Easy` ##JavaScript ```javascript diff --git a/Algorithms/Permutations/README.md b/Algorithms/Permutations/README.md index 42845ca..d8e0684 100644 --- a/Algorithms/Permutations/README.md +++ b/Algorithms/Permutations/README.md @@ -1,5 +1,6 @@ #[Permutations ](https://leetcode.com/problems/permutations/) - +######No:`46` +######Difficulty:`Medium` ##JavaScript ```javascript diff --git a/Algorithms/Plus One/README.md b/Algorithms/Plus One/README.md index 2db8b09..a140e70 100644 --- a/Algorithms/Plus One/README.md +++ b/Algorithms/Plus One/README.md @@ -1,5 +1,6 @@ #[Plus One](https://leetcode.com/problems/plus-one/) - +######No:`66` +######Difficulty:`Easy` ##JavaScript ```javascript diff --git a/Algorithms/README.md b/Algorithms/README.md deleted file mode 100644 index f683342..0000000 --- a/Algorithms/README.md +++ /dev/null @@ -1,6 +0,0 @@ -#[]() - -##JavaScript - -```javascript -``` diff --git a/Algorithms/Remove Duplicates from Sorted Array/README.md b/Algorithms/Remove Duplicates from Sorted Array/README.md index 709ef82..1904b8b 100644 --- a/Algorithms/Remove Duplicates from Sorted Array/README.md +++ b/Algorithms/Remove Duplicates from Sorted Array/README.md @@ -1,5 +1,6 @@ #[Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) - +######No:`26` +######Difficulty:`Easy` ##JavaScript ```javascript diff --git a/Algorithms/Remove Element/README.md b/Algorithms/Remove Element/README.md index f46678a..85faa71 100644 --- a/Algorithms/Remove Element/README.md +++ b/Algorithms/Remove Element/README.md @@ -1,5 +1,6 @@ #[Remove Element](https://leetcode.com/problems/remove-element/) - +######No:`27` +######Difficulty:`Easy` ##JavaScript ```javascript diff --git a/Algorithms/Reverse Words in a String/README.md b/Algorithms/Reverse Words in a String/README.md index 6eaada9..f97f34d 100644 --- a/Algorithms/Reverse Words in a String/README.md +++ b/Algorithms/Reverse Words in a String/README.md @@ -1,5 +1,6 @@ #[Reverse Words in a String ](https://leetcode.com/problems/reverse-words-in-a-string/) - +######No:`151` +######Difficulty:`Easy` ##JavaScript ```javascript diff --git a/Algorithms/Same Tree/README.md b/Algorithms/Same Tree/README.md index bb0635c..5ea71ce 100644 --- a/Algorithms/Same Tree/README.md +++ b/Algorithms/Same Tree/README.md @@ -1,5 +1,6 @@ #[SameTree](https://leetcode.com/problems/same-tree/) - +######No:`100` +######Difficulty:`Easy` ##JavaScript ```javascript diff --git a/Algorithms/Valid Parentheses/README.md b/Algorithms/Valid Parentheses/README.md index 52521ba..91e6816 100644 --- a/Algorithms/Valid Parentheses/README.md +++ b/Algorithms/Valid Parentheses/README.md @@ -1,5 +1,6 @@ #[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) - +######No:`20` +######Difficulty:`Easy` ##JavaScript ```javascript diff --git a/README.md b/README.md index e4d386e..518a481 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,19 @@ -leedcode -======== -My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) - - -| No | Title | Source Code | Difficulty | -|----| ----- | -------- | ---------- | -|235|Lowest Common Ancestor of a Binary SearchTree|[JavaScrpt](https://github.com/duteng/leedcode/tree/master/Algorithms/Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree)|Easy| -|104|Maximum Depth of Binary Tree|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Maximum%20Depth%20of%20Binary%20Tree)|Easy| -|88|Merge Sorted Array|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Merge%20Sorted%20Array)|Easy| +leedcode +======== +My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) + + +| No | Title | Source Code | Difficulty | +|----| ----- | -------- | ---------- ||20|Valid Parentheses|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Valid%20Parentheses)|Easy| +|26|Remove Duplicates from Sorted Array|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Remove%20Duplicates%20from%20Sorted%20Array)|Easy| +|27|Remove Element|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Remove%20Element)|Easy| +|46|Permutations |[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Permutations%20)|Medium| +|66|Plus One|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Plus%20One)|Easy| +|88|Merge Sorted Array|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Merge%20Sorted%20Array)|Easy| +|100|SameTree|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/SameTree)|Easy| +|104|Maximum Depth of Binary Tree |[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Maximum%20Depth%20of%20Binary%20Tree%20)|Easy| +|112|PathSum|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/PathSum)|Easy| +|151|Reverse Words in a String |[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Reverse%20Words%20in%20a%20String%20)|Easy| +|165|Compare Version Numbers |[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Compare%20Version%20Numbers%20)|Easy| +|179|Largest Number |[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Largest%20Number%20)|Medium| +|235|Lowest Common Ancestor of a Binary Search Tree|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree)|Easy| diff --git a/generate.js b/generate.js new file mode 100644 index 0000000..92784cc --- /dev/null +++ b/generate.js @@ -0,0 +1,49 @@ +var fs = require('fs'); +var events = require('events'); +var proxy = new events.EventEmitter(); + +var dir = './Algorithms'; +var repo_url = 'https://github.com/duteng/leedcode/tree/master/Algorithms/'; +var ret = []; + +targetDirs = fs.readdir(dir, function(err, files) { + var count = files.length; + files.forEach(function(value, index, arr) { + fs.readFile(dir + '/' + value + '/README.md', {encoding: 'utf8'}, function(err, data) { + var no = parseInt(data.match(/######No:`(\d+)`/)[1], 10); + var difficulty = data.match(/######Difficulty:`(\w+)`/)[1]; + var title = data.match(/#\[(.*)\]/)[1]; + var link = '[JavaScript](' + repo_url + title.replace(/\s/g, '%20') + ')'; + ret.push({ + no: no, + title:title, + difficulty: difficulty, + link: link + }); + if(ret.length == count) { + proxy.emit('readDone'); + } + + }); + }); +}); + +proxy.once('readDone', function() { + ret.sort(function(a, b) { + return a.no - b.no; + }); + + var content = [ + 'leedcode', + '========', + 'My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/)', + '', + '', + '| No | Title | Source Code | Difficulty |', + '|----| ----- | -------- | ---------- |'].join('\r\n'); + + for(var i = 0; i < ret.length; i++) { + content += '|' + ret[i].no + '|' + ret[i].title + '|' + ret[i].link + '|' + ret[i].difficulty + '|' + '\r\n'; + } + fs.writeFile('README.md', content); +}); From cc0fe8763c31afcea7164b742df9e4935a77667b Mon Sep 17 00:00:00 2001 From: Du Teng Date: Thu, 20 Aug 2015 12:34:41 +0800 Subject: [PATCH 015/126] update title --- Algorithms/Compare Version Numbers/README.md | 2 +- Algorithms/Largest Number/README.md | 2 +- Algorithms/Maximum Depth of Binary Tree/README.md | 2 +- Algorithms/Path Sum/README.md | 2 +- Algorithms/Permutations/README.md | 2 +- Algorithms/Reverse Words in a String/README.md | 2 +- Algorithms/Same Tree/README.md | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Algorithms/Compare Version Numbers/README.md b/Algorithms/Compare Version Numbers/README.md index 3f59680..924493a 100644 --- a/Algorithms/Compare Version Numbers/README.md +++ b/Algorithms/Compare Version Numbers/README.md @@ -1,4 +1,4 @@ -#[Compare Version Numbers ](https://leetcode.com/problems/compare-version-numbers/) +#[Compare Version Numbers](https://leetcode.com/problems/compare-version-numbers/) ######No:`165` ######Difficulty:`Easy` ##JavaScript diff --git a/Algorithms/Largest Number/README.md b/Algorithms/Largest Number/README.md index 4610f4f..66c1d36 100644 --- a/Algorithms/Largest Number/README.md +++ b/Algorithms/Largest Number/README.md @@ -1,4 +1,4 @@ -#[Largest Number ](https://leetcode.com/problems/largest-number/) +#[Largest Number](https://leetcode.com/problems/largest-number/) ######No:`179` ######Difficulty:`Medium` ##JavaScript diff --git a/Algorithms/Maximum Depth of Binary Tree/README.md b/Algorithms/Maximum Depth of Binary Tree/README.md index 7bd6ce6..492e02a 100644 --- a/Algorithms/Maximum Depth of Binary Tree/README.md +++ b/Algorithms/Maximum Depth of Binary Tree/README.md @@ -1,4 +1,4 @@ -#[Maximum Depth of Binary Tree ](https://leetcode.com/problems/maximum-depth-of-binary-tree/) +#[Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/) ######No:`104` ######Difficulty:`Easy` ##JavaScript diff --git a/Algorithms/Path Sum/README.md b/Algorithms/Path Sum/README.md index a22fc7f..1517572 100644 --- a/Algorithms/Path Sum/README.md +++ b/Algorithms/Path Sum/README.md @@ -1,4 +1,4 @@ -#[PathSum](https://leetcode.com/problems/path-sum/) +#[Path Sum](https://leetcode.com/problems/path-sum/) ######No:`112` ######Difficulty:`Easy` ##JavaScript diff --git a/Algorithms/Permutations/README.md b/Algorithms/Permutations/README.md index d8e0684..8a1cbec 100644 --- a/Algorithms/Permutations/README.md +++ b/Algorithms/Permutations/README.md @@ -1,4 +1,4 @@ -#[Permutations ](https://leetcode.com/problems/permutations/) +#[Permutations](https://leetcode.com/problems/permutations/) ######No:`46` ######Difficulty:`Medium` ##JavaScript diff --git a/Algorithms/Reverse Words in a String/README.md b/Algorithms/Reverse Words in a String/README.md index f97f34d..4ec142f 100644 --- a/Algorithms/Reverse Words in a String/README.md +++ b/Algorithms/Reverse Words in a String/README.md @@ -1,4 +1,4 @@ -#[Reverse Words in a String ](https://leetcode.com/problems/reverse-words-in-a-string/) +#[Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) ######No:`151` ######Difficulty:`Easy` ##JavaScript diff --git a/Algorithms/Same Tree/README.md b/Algorithms/Same Tree/README.md index 5ea71ce..c4be81e 100644 --- a/Algorithms/Same Tree/README.md +++ b/Algorithms/Same Tree/README.md @@ -1,4 +1,4 @@ -#[SameTree](https://leetcode.com/problems/same-tree/) +#[Same Tree](https://leetcode.com/problems/same-tree/) ######No:`100` ######Difficulty:`Easy` ##JavaScript From 5590fe1afa90e5e153221835fb5aba92805bd2da Mon Sep 17 00:00:00 2001 From: Du Teng Date: Thu, 20 Aug 2015 12:35:44 +0800 Subject: [PATCH 016/126] update title --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 518a481..225fcfc 100644 --- a/README.md +++ b/README.md @@ -7,13 +7,13 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |----| ----- | -------- | ---------- ||20|Valid Parentheses|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Valid%20Parentheses)|Easy| |26|Remove Duplicates from Sorted Array|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Remove%20Duplicates%20from%20Sorted%20Array)|Easy| |27|Remove Element|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Remove%20Element)|Easy| -|46|Permutations |[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Permutations%20)|Medium| +|46|Permutations|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Permutations)|Medium| |66|Plus One|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Plus%20One)|Easy| |88|Merge Sorted Array|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Merge%20Sorted%20Array)|Easy| -|100|SameTree|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/SameTree)|Easy| -|104|Maximum Depth of Binary Tree |[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Maximum%20Depth%20of%20Binary%20Tree%20)|Easy| -|112|PathSum|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/PathSum)|Easy| -|151|Reverse Words in a String |[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Reverse%20Words%20in%20a%20String%20)|Easy| -|165|Compare Version Numbers |[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Compare%20Version%20Numbers%20)|Easy| -|179|Largest Number |[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Largest%20Number%20)|Medium| +|100|Same Tree|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Same%20Tree)|Easy| +|104|Maximum Depth of Binary Tree|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Maximum%20Depth%20of%20Binary%20Tree)|Easy| +|112|Path Sum|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Path%20Sum)|Easy| +|151|Reverse Words in a String|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Reverse%20Words%20in%20a%20String)|Easy| +|165|Compare Version Numbers|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Compare%20Version%20Numbers)|Easy| +|179|Largest Number|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Largest%20Number)|Medium| |235|Lowest Common Ancestor of a Binary Search Tree|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree)|Easy| From ff922be1eddf289239f881725950224f05734460 Mon Sep 17 00:00:00 2001 From: Du Teng Date: Fri, 21 Aug 2015 03:03:17 +0800 Subject: [PATCH 017/126] add file --- Algorithms/Binary Tree Paths/README.md | 43 +++++++++++++++ .../README.md | 54 +++++++++++++++++++ Algorithms/Min Stack/README.md | 53 ++++++++++++++++++ 3 files changed, 150 insertions(+) create mode 100644 Algorithms/Binary Tree Paths/README.md create mode 100644 Algorithms/Intersection of Two Linked Lists/README.md create mode 100644 Algorithms/Min Stack/README.md diff --git a/Algorithms/Binary Tree Paths/README.md b/Algorithms/Binary Tree Paths/README.md new file mode 100644 index 0000000..f6f31aa --- /dev/null +++ b/Algorithms/Binary Tree Paths/README.md @@ -0,0 +1,43 @@ +#[Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/) +######No:`257` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {string[]} + */ +var binaryTreePaths = function(root) { + if(root === null) { + return []; + } + var ret = []; + var str = arguments[1] ? arguments[1] : ''; + if(str) { + str = str + '->' + root.val; + } else { + str = root.val + ''; + } + if(root.left === null && root.right === null) { + ret.push(str); + } + + if(root.left) { + var leftRet = binaryTreePaths(root.left, str); + Array.prototype.push.apply(ret, leftRet); + } + if(root.right) { + var rightRet = binaryTreePaths(root.right, str); + Array.prototype.push.apply(ret, rightRet); + } + return ret; +}; +``` diff --git a/Algorithms/Intersection of Two Linked Lists/README.md b/Algorithms/Intersection of Two Linked Lists/README.md new file mode 100644 index 0000000..f8ffa3b --- /dev/null +++ b/Algorithms/Intersection of Two Linked Lists/README.md @@ -0,0 +1,54 @@ +#[Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/) +######No:`160` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ + +/** + * @param {ListNode} headA + * @param {ListNode} headB + * @return {ListNode} + */ +var getIntersectionNode = function(headA, headB) { + if(headA === null || headB === null) { + return null; + } + var pointA = headA; + var pointB = headB; + var i = 0; + var j = 0; + var k = 0; + while(pointA.next !== null) { + pointA = pointA.next; + i++; + } + while(pointB.next !== null) { + pointB = pointB.next; + j++; + } + if(pointB != pointA) { + return null; + } + pointA = headA; + pointB = headB; + + if(i > j) { + while(k < i - j){pointA = pointA.next;k++;} + } else { + while(k < j - i){pointB = pointB.next;k++;} + } + while(pointA != pointB) { + pointA = pointA.next; + pointB = pointB.next; + } + return pointA; +}; +``` diff --git a/Algorithms/Min Stack/README.md b/Algorithms/Min Stack/README.md new file mode 100644 index 0000000..adfd0f6 --- /dev/null +++ b/Algorithms/Min Stack/README.md @@ -0,0 +1,53 @@ +#[Min Stack](https://leetcode.com/problems/min-stack/) +######No:`155` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @constructor + */ +var MinStack = function() { + this.stack = []; + this.min = Number.MAX_SAFE_INTEGER; +}; + +/** + * @param {number} x + * @returns {void} + */ +MinStack.prototype.push = function(x) { + if(x < this.min) { + this.min = x; + } + this.stack.push(x); +}; + +/** + * @returns {void} + */ +MinStack.prototype.pop = function() { + var number = this.stack.pop(); + if(number == this.min) { + this.min = Math.min.apply(null, this.stack); + } +}; + +/** + * @returns {number} + */ +MinStack.prototype.top = function() { + if(this.stack.length > 0) { + return this.stack[this.stack.length - 1]; + } else { + return undefined; + } +}; + +/** + * @returns {number} + */ +MinStack.prototype.getMin = function() { + return this.min; +}; +``` From ffab61b70fe76e04e871c7788c4b3e652ecdde6c Mon Sep 17 00:00:00 2001 From: Du Teng Date: Fri, 21 Aug 2015 03:03:41 +0800 Subject: [PATCH 018/126] add file --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 225fcfc..b06c5a9 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,9 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |104|Maximum Depth of Binary Tree|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Maximum%20Depth%20of%20Binary%20Tree)|Easy| |112|Path Sum|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Path%20Sum)|Easy| |151|Reverse Words in a String|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Reverse%20Words%20in%20a%20String)|Easy| +|155|Min Stack|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Min%20Stack)|Easy| +|160|Intersection of Two Linked Lists|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Intersection%20of%20Two%20Linked%20Lists)|Easy| |165|Compare Version Numbers|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Compare%20Version%20Numbers)|Easy| |179|Largest Number|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Largest%20Number)|Medium| |235|Lowest Common Ancestor of a Binary Search Tree|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree)|Easy| +|257|Binary Tree Paths|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Binary%20Tree%20Paths)|Easy| From 79988506a20f1b6649e16f510324decf6b460171 Mon Sep 17 00:00:00 2001 From: Du Teng Date: Sat, 22 Aug 2015 03:08:59 +0800 Subject: [PATCH 019/126] add --- Algorithms/Reverse Integer/README.md | 27 ++++++++++ Algorithms/String to Integer (atoi)/README.md | 24 +++++++++ Algorithms/Ugly Number/README.md | 20 +++++++ Algorithms/Valid Palindrome/README.md | 30 +++++++++++ Algorithms/ZigZag Conversion/README.md | 54 +++++++++++++++++++ README.md | 7 ++- 6 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 Algorithms/Reverse Integer/README.md create mode 100644 Algorithms/String to Integer (atoi)/README.md create mode 100644 Algorithms/Ugly Number/README.md create mode 100644 Algorithms/Valid Palindrome/README.md create mode 100644 Algorithms/ZigZag Conversion/README.md diff --git a/Algorithms/Reverse Integer/README.md b/Algorithms/Reverse Integer/README.md new file mode 100644 index 0000000..f3ff987 --- /dev/null +++ b/Algorithms/Reverse Integer/README.md @@ -0,0 +1,27 @@ +#[Reverse Integer](https://leetcode.com/problems/reverse-integer/) +######No:`7` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {number} x + * @return {number} + */ +var reverse = function(x) { + var flag = (x < 0 ? true : false); + x = Math.abs(x); + var reverse = parseInt((x + '').split('').reverse().join(''), 10); + if(reverse > Math.pow(2, 31)) { + return 0; + } + if(flag) { + return 0 - reverse; + } else { + return reverse; + } +}; +``` +######PS: +Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases? +For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. diff --git a/Algorithms/String to Integer (atoi)/README.md b/Algorithms/String to Integer (atoi)/README.md new file mode 100644 index 0000000..fe59c95 --- /dev/null +++ b/Algorithms/String to Integer (atoi)/README.md @@ -0,0 +1,24 @@ +#[String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/) +######No:`8` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {string} str + * @return {number} + */ +var myAtoi = function(str) { + var ret = parseInt(str, 10); + if(Number.isNaN(ret)) { + return 0; + } + if(ret > 2147483647) { + return 2147483647; + } else if(ret < -2147483648) { + return -2147483648; + } else { + return ret; + } +}; +``` diff --git a/Algorithms/Ugly Number/README.md b/Algorithms/Ugly Number/README.md new file mode 100644 index 0000000..10726cd --- /dev/null +++ b/Algorithms/Ugly Number/README.md @@ -0,0 +1,20 @@ +#[Ugly Number](https://leetcode.com/problems/ugly-number/) +######No:`263` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {number} num + * @return {boolean} + */ +var isUgly = function(num) { + if(num <= 0) { + return false; + } + while(num % 2 === 0) {num /= 2;} + while(num % 3 === 0) {num /= 3;} + while(num % 5 === 0) {num /= 5;} + return num === 1 ? true : false; +}; +``` diff --git a/Algorithms/Valid Palindrome/README.md b/Algorithms/Valid Palindrome/README.md new file mode 100644 index 0000000..2a72f80 --- /dev/null +++ b/Algorithms/Valid Palindrome/README.md @@ -0,0 +1,30 @@ +#[Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) +######No:`125` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {string} s + * @return {boolean} + */ +var isPalindrome = function(s) { + var str = s.split('').filter(function(c) { + return c.match(/[\w\d]/); + }).join('').toLowerCase(); + + if(str === '') { + return true; + } + var i = 0; + var j = str.length - 1; + while(i < j) { + if(str[i] != str[j]) { + return false; + } + i++; + j--; + } + return true; +}; +``` diff --git a/Algorithms/ZigZag Conversion/README.md b/Algorithms/ZigZag Conversion/README.md new file mode 100644 index 0000000..7825704 --- /dev/null +++ b/Algorithms/ZigZag Conversion/README.md @@ -0,0 +1,54 @@ +#[ZigZag Conversion](https://leetcode.com/problems/zigzag-conversion/) +######No:`165` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {string} s + * @param {number} numRows + * @return {string} + */ +var convert = function(s, numRows) { + var chars = s.split(''); + var arrs = []; + for(var k = 0; k < numRows; k++) { + arrs.push([]); + } + + var i = 0; + while(i < chars.length) { + for(var x = 0; x < numRows && i < chars.length; x++) { + arrs[x].push(chars[i++]); + } + for(var x = numRows - 2; x > 0 && i < chars.length; x--) { + arrs[x].push(chars[i++]); + } + } + + var ret = ''; + arrs.map(function(item) { + ret = ret.concat(item.join('')); + }); + return ret; +}; +``` +###How to solve? +``` +1 9 17 +2 8 10 16 18 +3 7 11 15 +4 6 12 14 +5 13 +``` + +######1.Create `arrs = [[],[],[],[],[]]` + +######2.traverse `chars` than save the every char like the following: +arrs[0] = [1, 9, 17]; +arrs[1] = [2, 8, 10, 16, 18]; +arrs[2] = [3, 7, 11, 15]; +arrs[3] = [4, 6, 12, 14]; +arrs[4] = [5, 13]; + +###### 3.concat arrs diff --git a/README.md b/README.md index b06c5a9..0c442a4 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,9 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) | No | Title | Source Code | Difficulty | -|----| ----- | -------- | ---------- ||20|Valid Parentheses|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Valid%20Parentheses)|Easy| +|----| ----- | -------- | ---------- ||7|Reverse Integer|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Reverse%20Integer)|Easy| +|8|String to Integer (atoi)|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/String%20to%20Integer%20(atoi))|Easy| +|20|Valid Parentheses|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Valid%20Parentheses)|Easy| |26|Remove Duplicates from Sorted Array|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Remove%20Duplicates%20from%20Sorted%20Array)|Easy| |27|Remove Element|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Remove%20Element)|Easy| |46|Permutations|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Permutations)|Medium| @@ -13,10 +15,13 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |100|Same Tree|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Same%20Tree)|Easy| |104|Maximum Depth of Binary Tree|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Maximum%20Depth%20of%20Binary%20Tree)|Easy| |112|Path Sum|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Path%20Sum)|Easy| +|125|Valid Palindrome|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Valid%20Palindrome)|Easy| |151|Reverse Words in a String|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Reverse%20Words%20in%20a%20String)|Easy| |155|Min Stack|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Min%20Stack)|Easy| |160|Intersection of Two Linked Lists|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Intersection%20of%20Two%20Linked%20Lists)|Easy| |165|Compare Version Numbers|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Compare%20Version%20Numbers)|Easy| +|165|ZigZag Conversion|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/ZigZag%20Conversion)|Easy| |179|Largest Number|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Largest%20Number)|Medium| |235|Lowest Common Ancestor of a Binary Search Tree|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree)|Easy| |257|Binary Tree Paths|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Binary%20Tree%20Paths)|Easy| +|263|Ugly Number|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Ugly%20Number)|Easy| From 4994c4a75f58d98604ad5c8bc6a63a7db6286991 Mon Sep 17 00:00:00 2001 From: Du Teng Date: Sat, 22 Aug 2015 04:46:51 +0800 Subject: [PATCH 020/126] add --- .../README.md | 36 +++++++++++++++++++ .../README.md | 34 ++++++++++++++++++ Algorithms/Set Matrix Zeroes/README.md | 34 ++++++++++++++++++ README.md | 5 ++- 4 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 Algorithms/Best Time to Buy and Sell Stock II/README.md create mode 100644 Algorithms/Evaluate Reverse Polish Notation/README.md create mode 100644 Algorithms/Set Matrix Zeroes/README.md diff --git a/Algorithms/Best Time to Buy and Sell Stock II/README.md b/Algorithms/Best Time to Buy and Sell Stock II/README.md new file mode 100644 index 0000000..74d1e38 --- /dev/null +++ b/Algorithms/Best Time to Buy and Sell Stock II/README.md @@ -0,0 +1,36 @@ +#[Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) +######No:`122` +######Difficulty:`Medium` +##JavaScript + +```javascript +/** + * @param {number[]} prices + * @return {number} + */ +var maxProfit = function(prices) { + var minsum = 0; + var maxsum = 0; + var isUp; + var lastIsUp; + + for(var i = 0; i < prices.length - 1; i++) { + if(prices[i] < prices[i+1]) { + isUp = true; + } else { + isUp = false; + } + if(isUp && (i === 0 || lastIsUp === false)) { + minsum += prices[i]; + } + if(lastIsUp && isUp === false) { + maxsum += prices[i]; + } + if(isUp && i === prices.length - 2) { + maxsum += prices[i + 1]; + } + lastIsUp = isUp; + } + return maxsum - minsum; +}; +``` diff --git a/Algorithms/Evaluate Reverse Polish Notation/README.md b/Algorithms/Evaluate Reverse Polish Notation/README.md new file mode 100644 index 0000000..488bfe9 --- /dev/null +++ b/Algorithms/Evaluate Reverse Polish Notation/README.md @@ -0,0 +1,34 @@ +#[Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/) +######No:`150` +######Difficulty:`Medium` +##JavaScript + +```javascript +/** + * @param {string[]} tokens + * @return {number} + */ + var evalRPN = function(tokens) { + var stack = []; + var a, b, result; + for(var i = 0; i < tokens.length; i++) { + if(Number.isNaN(parseInt(tokens[i]))) { + b = stack.pop(); + a = stack.pop(); + if(tokens[i] == '+') { + result = a + b; + } else if(tokens[i] == '-') { + result = a - b; + } else if(tokens[i] == '*') { + result = a * b; + } else if(tokens[i] == '/') { + result = a / b; + } + stack.push(parseInt(result, 10)); + } else { + stack.push(parseInt(tokens[i], 10)); + } + } + return stack.pop(); +}; +``` diff --git a/Algorithms/Set Matrix Zeroes/README.md b/Algorithms/Set Matrix Zeroes/README.md new file mode 100644 index 0000000..91f19d6 --- /dev/null +++ b/Algorithms/Set Matrix Zeroes/README.md @@ -0,0 +1,34 @@ +#[Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/) +######No:`73` +######Difficulty:`Medium` +##JavaScript + +```javascript +/** + * @param {number[][]} matrix + * @return {void} Do not return anything, modify matrix in-place instead. + */ +var setZeroes = function(matrix) { + var hasZeroFirstCol = false; + for(var i = 0; i < matrix.length; i++) { + if(matrix[i][0] === 0) { + hasZeroFirstCol = true; + } + for(var j = 1; j < matrix[i].length; j++) { + if(matrix[i][j] === 0) { + matrix[0][j] = matrix[i][0] = 0; + } + } + } + for(var y = matrix.length - 1; y >= 0; y--) { + for(var x = matrix[y].length - 1; x >= 1; x--) { + if(matrix[0][x] === 0 || matrix[y][0] === 0) { + matrix[y][x] = 0; + } + } + if(hasZeroFirstCol) { + matrix[y][0] = 0; + } + } +}; +``` diff --git a/README.md b/README.md index 0c442a4..360a374 100644 --- a/README.md +++ b/README.md @@ -11,16 +11,19 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |27|Remove Element|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Remove%20Element)|Easy| |46|Permutations|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Permutations)|Medium| |66|Plus One|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Plus%20One)|Easy| +|73|Set Matrix Zeroes|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Set%20Matrix%20Zeroes)|Medium| |88|Merge Sorted Array|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Merge%20Sorted%20Array)|Easy| |100|Same Tree|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Same%20Tree)|Easy| |104|Maximum Depth of Binary Tree|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Maximum%20Depth%20of%20Binary%20Tree)|Easy| |112|Path Sum|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Path%20Sum)|Easy| +|122|Best Time to Buy and Sell Stock II|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Best%20Time%20to%20Buy%20and%20Sell%20Stock%20II)|Medium| |125|Valid Palindrome|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Valid%20Palindrome)|Easy| +|150|Evaluate Reverse Polish Notation|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Evaluate%20Reverse%20Polish%20Notation)|Medium| |151|Reverse Words in a String|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Reverse%20Words%20in%20a%20String)|Easy| |155|Min Stack|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Min%20Stack)|Easy| |160|Intersection of Two Linked Lists|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Intersection%20of%20Two%20Linked%20Lists)|Easy| -|165|Compare Version Numbers|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Compare%20Version%20Numbers)|Easy| |165|ZigZag Conversion|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/ZigZag%20Conversion)|Easy| +|165|Compare Version Numbers|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Compare%20Version%20Numbers)|Easy| |179|Largest Number|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Largest%20Number)|Medium| |235|Lowest Common Ancestor of a Binary Search Tree|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree)|Easy| |257|Binary Tree Paths|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Binary%20Tree%20Paths)|Easy| From db632ebe49a8d31930dba93699bbd712d9213928 Mon Sep 17 00:00:00 2001 From: Du Teng Date: Sat, 22 Aug 2015 23:48:52 +0800 Subject: [PATCH 021/126] add --- Algorithms/Add Digits/README.md | 35 +++++++++++++++ Algorithms/Balanced Binary Tree/README.md | 44 +++++++++++++++++++ Algorithms/Contains Duplicate/README.md | 21 +++++++++ .../Excel Sheet Column Number/README.md | 21 +++++++++ Algorithms/Excel Sheet Column Title/README.md | 20 +++++++++ Algorithms/Invert Binary Tree/README.md | 30 +++++++++++++ Algorithms/Majority Element/README.md | 28 ++++++++++++ Algorithms/Palindrome Number/README.md | 24 ++++++++++ Algorithms/Pascal's Triangle/README.md | 30 +++++++++++++ Algorithms/Rotate Array/README.md | 17 +++++++ Algorithms/Single Number/README.md | 20 +++++++++ README.md | 11 +++++ 12 files changed, 301 insertions(+) create mode 100644 Algorithms/Add Digits/README.md create mode 100644 Algorithms/Balanced Binary Tree/README.md create mode 100644 Algorithms/Contains Duplicate/README.md create mode 100644 Algorithms/Excel Sheet Column Number/README.md create mode 100644 Algorithms/Excel Sheet Column Title/README.md create mode 100644 Algorithms/Invert Binary Tree/README.md create mode 100644 Algorithms/Majority Element/README.md create mode 100644 Algorithms/Palindrome Number/README.md create mode 100644 Algorithms/Pascal's Triangle/README.md create mode 100644 Algorithms/Rotate Array/README.md create mode 100644 Algorithms/Single Number/README.md diff --git a/Algorithms/Add Digits/README.md b/Algorithms/Add Digits/README.md new file mode 100644 index 0000000..e8ef4df --- /dev/null +++ b/Algorithms/Add Digits/README.md @@ -0,0 +1,35 @@ +#[Add Digits](https://leetcode.com/problems/add-digits/) +######No:`258` +######Difficulty:`Easy` +##JavaScript + +####Solution 1: +```javascript +/** + * @param {number} num + * @return {number} + */ +var addDigits = function(num) { + if(num < 10) { + return num; + } else { + var arr = (num + '').split(''); + var ret = arr.reduce(function(prev, cur) { + return parseInt(prev, 10) + parseInt(cur, 10); + }); + return addDigits(ret); + } +}; +``` + + +####Solution 2: +```javascript +/** + * @param {number} num + * @return {number} + */ +var addDigits = function(num) { + return 1 + (num - 1) % 9; +}; +``` diff --git a/Algorithms/Balanced Binary Tree/README.md b/Algorithms/Balanced Binary Tree/README.md new file mode 100644 index 0000000..b973b82 --- /dev/null +++ b/Algorithms/Balanced Binary Tree/README.md @@ -0,0 +1,44 @@ +#[Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/) +######No:`110` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {boolean} + */ +var maxDeepth = function(root) { + if(root === null) { + return 0; + } else { + return Math.max(maxDeepth(root.left), maxDeepth(root.right)) + 1; + } +} +var isBalanced = function(root) { + if(root === null) { + return true; + } + var leftDeepth = maxDeepth(root.left); + var rightDeepth = maxDeepth(root.right); + + if(Math.abs(leftDeepth - rightDeepth) <= 1 + && isBalanced(root.left) + && isBalanced(root.right) + ) { + return true; + } else { + return false; + } +}; +``` + +####Description: +Calculate the deepth of the Binary Tree is also a problem in here: [https://leetcode.com/problems/maximum-depth-of-binary-tree](https://leetcode.com/problems/maximum-depth-of-binary-tree) diff --git a/Algorithms/Contains Duplicate/README.md b/Algorithms/Contains Duplicate/README.md new file mode 100644 index 0000000..fa010f4 --- /dev/null +++ b/Algorithms/Contains Duplicate/README.md @@ -0,0 +1,21 @@ +#[Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) +######No:`217` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {number[]} nums + * @return {boolean} + */ +var containsDuplicate = function(nums) { + for(var i = 0; i < nums.length; i++) { + for(var j = i + 1 ; j < nums.length; j++) { + if(nums[i] == nums[j]) { + return true; + } + } + } + return false; +}; +``` diff --git a/Algorithms/Excel Sheet Column Number/README.md b/Algorithms/Excel Sheet Column Number/README.md new file mode 100644 index 0000000..c070dd4 --- /dev/null +++ b/Algorithms/Excel Sheet Column Number/README.md @@ -0,0 +1,21 @@ +#[Excel Sheet Column Number](https://leetcode.com/problems/excel-sheet-column-number/) +######No:`171` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {string} s + * @return {number} + */ +var titleToNumber = function(s) { + var arr = s.split(''); + var digit; + var sum = 0; + for(var i = arr.length - 1, j = 0; i >= 0; i--, j++) { + digit = arr[i].charCodeAt(0) - 64; + sum += digit * Math.pow(26, j) + } + return sum; +}; +``` diff --git a/Algorithms/Excel Sheet Column Title/README.md b/Algorithms/Excel Sheet Column Title/README.md new file mode 100644 index 0000000..a3092ba --- /dev/null +++ b/Algorithms/Excel Sheet Column Title/README.md @@ -0,0 +1,20 @@ +#[Excel Sheet Column Title ](https://leetcode.com/problems/excel-sheet-column-title/) +######No:`168` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {number} n + * @return {string} + */ +var convertToTitle = function(n) { + var ret = '' + while(n > 0) { + n--; + ret = String.fromCharCode(65 + n % 26) + ret; + n = Math.floor(n / 26); + } + return ret; +}; +``` diff --git a/Algorithms/Invert Binary Tree/README.md b/Algorithms/Invert Binary Tree/README.md new file mode 100644 index 0000000..827d7cd --- /dev/null +++ b/Algorithms/Invert Binary Tree/README.md @@ -0,0 +1,30 @@ +#[Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/) +######No:`226` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {TreeNode} + */ +var invertTree = function(root) { + if(root !== null) { + var tmp; + tmp = root.left; + root.left = root.right; + root.right = tmp; + + invertTree(root.left); + invertTree(root.right); + } + return root; +}; +``` diff --git a/Algorithms/Majority Element/README.md b/Algorithms/Majority Element/README.md new file mode 100644 index 0000000..c676e7b --- /dev/null +++ b/Algorithms/Majority Element/README.md @@ -0,0 +1,28 @@ +#[Majority Element](https://leetcode.com/problems/majority-element/) +######No:`169` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {number[]} nums + * @return {number} + */ +var majorityElement = function(nums) { + var count = 1; + var item = nums[0]; + for(var i = 1; i < nums.length; i++) { + if(count === 0) { + count = 1; + item = nums[i]; + } else { + if(nums[i] == item) { + count++; + } else { + count--; + } + } + } + return item; +}; +``` diff --git a/Algorithms/Palindrome Number/README.md b/Algorithms/Palindrome Number/README.md new file mode 100644 index 0000000..0d6d70c --- /dev/null +++ b/Algorithms/Palindrome Number/README.md @@ -0,0 +1,24 @@ +#[Palindrome Number](https://leetcode.com/problems/palindrome-number/) +######No:`9` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {number} x + * @return {boolean} + */ +var isPalindrome = function(x) { + x += ''; + var i = 0; + var j = x.length - 1; + while(i < j) { + if(x[i] != x[j]) { + return false + } + i++; + j--; + } + return true; +}; +``` diff --git a/Algorithms/Pascal's Triangle/README.md b/Algorithms/Pascal's Triangle/README.md new file mode 100644 index 0000000..dbd07b6 --- /dev/null +++ b/Algorithms/Pascal's Triangle/README.md @@ -0,0 +1,30 @@ +#[Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/) +######No:`118` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {number} numRows + * @return {number[][]} + */ +var generate = function(numRows) { + if(numRows === 0) { + return []; + } + if(numRows === 1) { + return [[1]]; + } + + var ret = [[1]]; + for(var i = 1; i < numRows; i++) { + ret.push([]); + for(var j = 0; j < ret[i - 1].length - 1; j++) { + ret[i].push(ret[i-1][j] + ret[i-1][j + 1]); + } + ret[i].unshift(1); + ret[i].push(1); + } + return ret; +}; +``` diff --git a/Algorithms/Rotate Array/README.md b/Algorithms/Rotate Array/README.md new file mode 100644 index 0000000..e97a5d9 --- /dev/null +++ b/Algorithms/Rotate Array/README.md @@ -0,0 +1,17 @@ +#[Rotate Array](https://leetcode.com/problems/rotate-array/) +######No:`189` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {number[]} nums + * @param {number} k + * @return {void} Do not return anything, modify nums in-place instead. + */ +var rotate = function(nums, k) { + for(var i = 0; i < k; i++) { + nums.unshift(nums.pop()); + } +}; +``` diff --git a/Algorithms/Single Number/README.md b/Algorithms/Single Number/README.md new file mode 100644 index 0000000..b1bfe1a --- /dev/null +++ b/Algorithms/Single Number/README.md @@ -0,0 +1,20 @@ +#[Single Number](https://leetcode.com/problems/single-number/) +######No:`136` +######Difficulty:`Medium` +##JavaScript + +```javascript +/** + * @param {number[]} nums + * @return {number} + */ +var singleNumber = function(nums) { + return nums.reduce(function(prev, cur) { + return prev ^ cur; + }); +}; +``` + +####Description: +Any number that XOR itselt will be 0; + diff --git a/README.md b/README.md index 360a374..30d2cde 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) | No | Title | Source Code | Difficulty | |----| ----- | -------- | ---------- ||7|Reverse Integer|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Reverse%20Integer)|Easy| |8|String to Integer (atoi)|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/String%20to%20Integer%20(atoi))|Easy| +|9|Palindrome Number|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Palindrome%20Number)|Easy| |20|Valid Parentheses|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Valid%20Parentheses)|Easy| |26|Remove Duplicates from Sorted Array|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Remove%20Duplicates%20from%20Sorted%20Array)|Easy| |27|Remove Element|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Remove%20Element)|Easy| @@ -15,16 +16,26 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |88|Merge Sorted Array|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Merge%20Sorted%20Array)|Easy| |100|Same Tree|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Same%20Tree)|Easy| |104|Maximum Depth of Binary Tree|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Maximum%20Depth%20of%20Binary%20Tree)|Easy| +|110|Balanced Binary Tree|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Balanced%20Binary%20Tree)|Easy| |112|Path Sum|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Path%20Sum)|Easy| +|118|Pascal's Triangle|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Pascal's%20Triangle)|Easy| |122|Best Time to Buy and Sell Stock II|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Best%20Time%20to%20Buy%20and%20Sell%20Stock%20II)|Medium| |125|Valid Palindrome|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Valid%20Palindrome)|Easy| +|136|Single Number|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Single%20Number)|Medium| |150|Evaluate Reverse Polish Notation|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Evaluate%20Reverse%20Polish%20Notation)|Medium| |151|Reverse Words in a String|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Reverse%20Words%20in%20a%20String)|Easy| |155|Min Stack|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Min%20Stack)|Easy| |160|Intersection of Two Linked Lists|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Intersection%20of%20Two%20Linked%20Lists)|Easy| |165|ZigZag Conversion|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/ZigZag%20Conversion)|Easy| |165|Compare Version Numbers|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Compare%20Version%20Numbers)|Easy| +|168|Excel Sheet Column Title |[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Excel%20Sheet%20Column%20Title%20)|Easy| +|169|Majority Element|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Majority%20Element)|Easy| +|171|Excel Sheet Column Number|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Excel%20Sheet%20Column%20Number)|Easy| |179|Largest Number|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Largest%20Number)|Medium| +|189|Rotate Array|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Rotate%20Array)|Easy| +|217|Contains Duplicate|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Contains%20Duplicate)|Easy| +|226|Invert Binary Tree|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Invert%20Binary%20Tree)|Easy| |235|Lowest Common Ancestor of a Binary Search Tree|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree)|Easy| |257|Binary Tree Paths|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Binary%20Tree%20Paths)|Easy| +|258|Add Digits|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Add%20Digits)|Easy| |263|Ugly Number|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Ugly%20Number)|Easy| From cc44394ce5d08ea2294f61bcb314edbe5c883f44 Mon Sep 17 00:00:00 2001 From: Du Teng Date: Sun, 23 Aug 2015 18:44:33 +0800 Subject: [PATCH 022/126] add --- .../README.md | 34 ++++++++++++++++ .../READMD.md | 35 +++++++++++++++++ .../Minimum Depth of Binary Tree/README.md | 37 ++++++++++++++++++ Algorithms/Symmetric Tree/READMD.md | 39 +++++++++++++++++++ 4 files changed, 145 insertions(+) create mode 100644 Algorithms/Binary Tree Level Order Traversal II/README.md create mode 100644 Algorithms/Binary Tree Level Order Traversal/READMD.md create mode 100644 Algorithms/Minimum Depth of Binary Tree/README.md create mode 100644 Algorithms/Symmetric Tree/READMD.md diff --git a/Algorithms/Binary Tree Level Order Traversal II/README.md b/Algorithms/Binary Tree Level Order Traversal II/README.md new file mode 100644 index 0000000..4137fb5 --- /dev/null +++ b/Algorithms/Binary Tree Level Order Traversal II/README.md @@ -0,0 +1,34 @@ +#[Binary Tree Level Order Traversal II](https://leetcode.com/problems/binary-tree-level-order-traversal-ii/) +######No:`107` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number[][]} + */ +var traversal = function(ret, root, depth) { + if(root === null) { + return; + } + if(!Array.isArray(ret[depth])) { + ret[depth] = []; + } + ret[depth].push(root.val); + traversal(ret, root.left, depth + 1); + traversal(ret, root.right, depth + 1); +} +var levelOrderBottom = function(root) { + var ret = []; + traversal(ret, root, 0); + return ret.reverse(); +}; +``` diff --git a/Algorithms/Binary Tree Level Order Traversal/READMD.md b/Algorithms/Binary Tree Level Order Traversal/READMD.md new file mode 100644 index 0000000..160efb1 --- /dev/null +++ b/Algorithms/Binary Tree Level Order Traversal/READMD.md @@ -0,0 +1,35 @@ +#[Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/) +######No:`102` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number[][]} + */ +var traversal = function(ret, root, depth) { + if(root === null) { + return; + } + if(!Array.isArray(ret[depth])) { + ret[depth] = []; + } + ret[depth].push(root.val); + traversal(ret, root.left, depth + 1); + traversal(ret, root.right, depth + 1); +} + +var levelOrder = function(root) { + var ret = []; + traversal(ret, root, 0); + return ret; +}; +``` diff --git a/Algorithms/Minimum Depth of Binary Tree/README.md b/Algorithms/Minimum Depth of Binary Tree/README.md new file mode 100644 index 0000000..1fa1291 --- /dev/null +++ b/Algorithms/Minimum Depth of Binary Tree/README.md @@ -0,0 +1,37 @@ +#[Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/) +######No:`111` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number} + */ +var minDepth = function(root) { + if(root === null) { + return 0; + } + var leftDepth = minDepth(root.left); + var rightDepth = minDepth(root.right); + + /* + * As a shot, the follow can be write: + * return (leftDepth == 0 || rightDepth == 0) ? leftDepth + rightDepth + 1: Math.min(leftDepth,rightDepth) + 1; + */ + if(leftDepth !== 0 && rightDepth !== 0) { + return Math.min(leftDepth, rightDepth) + 1; + } else if(leftDepth === 0) { + return rightDepth + 1; + } else { + return leftDepth + 1; + } +}; +``` diff --git a/Algorithms/Symmetric Tree/READMD.md b/Algorithms/Symmetric Tree/READMD.md new file mode 100644 index 0000000..9479daa --- /dev/null +++ b/Algorithms/Symmetric Tree/READMD.md @@ -0,0 +1,39 @@ +#[Symmetric Tree](https://leetcode.com/problems/symmetric-tree/) +######No:`101` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {boolean} + */ + +var isSymmetric = function(root) { + function isSymmetricNode(left, right) { + if(!left && !right) { + return true; + } + if((left && !right) || (!left && right)) { + return false; + } + return (left.val == right.val) + && isSymmetricNode(left.left, right.right) + && isSymmetricNode(left.right, right.left); + } + if(root === null) { + return true; + } + var left = root.left; + var right = root.right; + return isSymmetricNode(left, right); +}; + +``` From a064427da657fdc5c2a04e887f1b191694e8115c Mon Sep 17 00:00:00 2001 From: Du Teng Date: Sun, 23 Aug 2015 18:50:27 +0800 Subject: [PATCH 023/126] rename --- .../{READMD.md => README.md} | 0 Algorithms/Symmetric Tree/{READMD.md => README.md} | 0 README.md | 4 ++++ generate.js | 1 + 4 files changed, 5 insertions(+) rename Algorithms/Binary Tree Level Order Traversal/{READMD.md => README.md} (100%) rename Algorithms/Symmetric Tree/{READMD.md => README.md} (100%) diff --git a/Algorithms/Binary Tree Level Order Traversal/READMD.md b/Algorithms/Binary Tree Level Order Traversal/README.md similarity index 100% rename from Algorithms/Binary Tree Level Order Traversal/READMD.md rename to Algorithms/Binary Tree Level Order Traversal/README.md diff --git a/Algorithms/Symmetric Tree/READMD.md b/Algorithms/Symmetric Tree/README.md similarity index 100% rename from Algorithms/Symmetric Tree/READMD.md rename to Algorithms/Symmetric Tree/README.md diff --git a/README.md b/README.md index 30d2cde..9da3547 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,12 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |73|Set Matrix Zeroes|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Set%20Matrix%20Zeroes)|Medium| |88|Merge Sorted Array|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Merge%20Sorted%20Array)|Easy| |100|Same Tree|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Same%20Tree)|Easy| +|101|Symmetric Tree|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Symmetric%20Tree)|Easy| +|102|Binary Tree Level Order Traversal|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Binary%20Tree%20Level%20Order%20Traversal)|Easy| |104|Maximum Depth of Binary Tree|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Maximum%20Depth%20of%20Binary%20Tree)|Easy| +|107|Binary Tree Level Order Traversal II|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Binary%20Tree%20Level%20Order%20Traversal%20II)|Easy| |110|Balanced Binary Tree|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Balanced%20Binary%20Tree)|Easy| +|111|Minimum Depth of Binary Tree|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Minimum%20Depth%20of%20Binary%20Tree)|Easy| |112|Path Sum|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Path%20Sum)|Easy| |118|Pascal's Triangle|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Pascal's%20Triangle)|Easy| |122|Best Time to Buy and Sell Stock II|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Best%20Time%20to%20Buy%20and%20Sell%20Stock%20II)|Medium| diff --git a/generate.js b/generate.js index 92784cc..9e8636d 100644 --- a/generate.js +++ b/generate.js @@ -10,6 +10,7 @@ targetDirs = fs.readdir(dir, function(err, files) { var count = files.length; files.forEach(function(value, index, arr) { fs.readFile(dir + '/' + value + '/README.md', {encoding: 'utf8'}, function(err, data) { + console.log(value); var no = parseInt(data.match(/######No:`(\d+)`/)[1], 10); var difficulty = data.match(/######Difficulty:`(\w+)`/)[1]; var title = data.match(/#\[(.*)\]/)[1]; From 03ada50ac6dab57ed16f030ed08e974d5a1a1793 Mon Sep 17 00:00:00 2001 From: Du Teng Date: Sun, 23 Aug 2015 18:56:14 +0800 Subject: [PATCH 024/126] readme --- README.md | 2 +- generate.js | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 9da3547..cd1fcaa 100644 --- a/README.md +++ b/README.md @@ -30,8 +30,8 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |151|Reverse Words in a String|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Reverse%20Words%20in%20a%20String)|Easy| |155|Min Stack|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Min%20Stack)|Easy| |160|Intersection of Two Linked Lists|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Intersection%20of%20Two%20Linked%20Lists)|Easy| -|165|ZigZag Conversion|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/ZigZag%20Conversion)|Easy| |165|Compare Version Numbers|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Compare%20Version%20Numbers)|Easy| +|165|ZigZag Conversion|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/ZigZag%20Conversion)|Easy| |168|Excel Sheet Column Title |[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Excel%20Sheet%20Column%20Title%20)|Easy| |169|Majority Element|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Majority%20Element)|Easy| |171|Excel Sheet Column Number|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Excel%20Sheet%20Column%20Number)|Easy| diff --git a/generate.js b/generate.js index 9e8636d..92784cc 100644 --- a/generate.js +++ b/generate.js @@ -10,7 +10,6 @@ targetDirs = fs.readdir(dir, function(err, files) { var count = files.length; files.forEach(function(value, index, arr) { fs.readFile(dir + '/' + value + '/README.md', {encoding: 'utf8'}, function(err, data) { - console.log(value); var no = parseInt(data.match(/######No:`(\d+)`/)[1], 10); var difficulty = data.match(/######Difficulty:`(\w+)`/)[1]; var title = data.match(/#\[(.*)\]/)[1]; From 6c4340a0241f5f805ede6099f8065e82acaa29be Mon Sep 17 00:00:00 2001 From: Du Teng Date: Tue, 25 Aug 2015 12:05:39 +0800 Subject: [PATCH 025/126] add sort list --- Algorithms/Merge Two Sorted Lists/README.md | 51 ++++++++++++++ Algorithms/Sort List/README.md | 75 +++++++++++++++++++++ Algorithms/Valid Anagram/README.md | 25 +++++++ 3 files changed, 151 insertions(+) create mode 100644 Algorithms/Merge Two Sorted Lists/README.md create mode 100644 Algorithms/Sort List/README.md create mode 100644 Algorithms/Valid Anagram/README.md diff --git a/Algorithms/Merge Two Sorted Lists/README.md b/Algorithms/Merge Two Sorted Lists/README.md new file mode 100644 index 0000000..0a9fd0e --- /dev/null +++ b/Algorithms/Merge Two Sorted Lists/README.md @@ -0,0 +1,51 @@ +#[Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) +######No:`21` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} l1 + * @param {ListNode} l2 + * @return {ListNode} + */ +var mergeTwoLists = function(l1, l2) { + if(!l1) return l2; + if(!l2) return l1; + var head = null + if(l1.val < l2.val) { + head = l1; + l1 = l1.next; + } else { + head = l2; + l2 = l2.next; + } + + var newlist = head; + + while(l1 && l2) { + if(l1.val < l2.val) { + newlist.next = l1; + l1 = l1.next; + } else { + newlist.next = l2; + l2 = l2.next; + } + newlist = newlist.next; + } + if(!l1) { + newlist.next = l2; + } else { + newlist.next = l1; + } + + return head; +}; +``` diff --git a/Algorithms/Sort List/README.md b/Algorithms/Sort List/README.md new file mode 100644 index 0000000..78ba3fb --- /dev/null +++ b/Algorithms/Sort List/README.md @@ -0,0 +1,75 @@ +#[Sort List](https://leetcode.com/problems/sort-list/) +######No:`148` +######Difficulty:`Medium` +##JavaScript + +```javascript +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @return {ListNode} + */ +var sortList = function(head) { + function merge(l1, l2) { + if(!l1) return l2; + if(!l2) return l1; + var head = null + if(l1.val < l2.val) { + head = l1; + l1 = l1.next; + } else { + head = l2; + l2 = l2.next; + } + var newlist = head; + while(l1 && l2) { + if(l1.val < l2.val) { + newlist.next = l1; + l1 = l1.next; + } else { + newlist.next = l2; + l2 = l2.next; + } + newlist = newlist.next; + } + if(!l1) { + newlist.next = l2; + } else { + newlist.next = l1; + } + return head; + } + + if(!head || !head.next) { + return head; + } + + var p1 = head; + var p2 = head; + + // p1 go step 1 + // p2 go step 2 + while(p1 && p2) { + p2 = p2.next; + if(p2) { + p2 = p2.next; + } + if(!p2) { + break; + } + p1 = p1.next; + } + + var right = p1.next; + p1.next = null; + var left = head; + + return merge(sortList(left), sortList(right)); +} +``` diff --git a/Algorithms/Valid Anagram/README.md b/Algorithms/Valid Anagram/README.md new file mode 100644 index 0000000..6675140 --- /dev/null +++ b/Algorithms/Valid Anagram/README.md @@ -0,0 +1,25 @@ +#[Valid Anagram](https://leetcode.com/problems/valid-anagram/) +######No:`242` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {string} s + * @param {string} t + * @return {boolean} + */ +var isAnagram = function(s, t) { + var arr1 = s.split('').sort(); + var arr2 = t.split('').sort(); + if(arr1.length !== arr2.length) { + return false; + } + for(var i = 0; i < arr1.length; i++) { + if(arr1[i] !== arr2[i]) { + return false; + } + } + return true; +}; +``` From 252b164d21166aa28e5d97f3a31352546e3f7097 Mon Sep 17 00:00:00 2001 From: Du Teng Date: Tue, 25 Aug 2015 12:05:58 +0800 Subject: [PATCH 026/126] update readme --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index cd1fcaa..73b966e 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |8|String to Integer (atoi)|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/String%20to%20Integer%20(atoi))|Easy| |9|Palindrome Number|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Palindrome%20Number)|Easy| |20|Valid Parentheses|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Valid%20Parentheses)|Easy| +|21|Merge Two Sorted Lists|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Merge%20Two%20Sorted%20Lists)|Easy| |26|Remove Duplicates from Sorted Array|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Remove%20Duplicates%20from%20Sorted%20Array)|Easy| |27|Remove Element|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Remove%20Element)|Easy| |46|Permutations|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Permutations)|Medium| @@ -26,12 +27,13 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |122|Best Time to Buy and Sell Stock II|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Best%20Time%20to%20Buy%20and%20Sell%20Stock%20II)|Medium| |125|Valid Palindrome|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Valid%20Palindrome)|Easy| |136|Single Number|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Single%20Number)|Medium| +|148|Sort List|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Sort%20List)|Medium| |150|Evaluate Reverse Polish Notation|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Evaluate%20Reverse%20Polish%20Notation)|Medium| |151|Reverse Words in a String|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Reverse%20Words%20in%20a%20String)|Easy| |155|Min Stack|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Min%20Stack)|Easy| |160|Intersection of Two Linked Lists|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Intersection%20of%20Two%20Linked%20Lists)|Easy| -|165|Compare Version Numbers|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Compare%20Version%20Numbers)|Easy| |165|ZigZag Conversion|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/ZigZag%20Conversion)|Easy| +|165|Compare Version Numbers|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Compare%20Version%20Numbers)|Easy| |168|Excel Sheet Column Title |[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Excel%20Sheet%20Column%20Title%20)|Easy| |169|Majority Element|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Majority%20Element)|Easy| |171|Excel Sheet Column Number|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Excel%20Sheet%20Column%20Number)|Easy| @@ -40,6 +42,7 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |217|Contains Duplicate|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Contains%20Duplicate)|Easy| |226|Invert Binary Tree|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Invert%20Binary%20Tree)|Easy| |235|Lowest Common Ancestor of a Binary Search Tree|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree)|Easy| +|242|Valid Anagram|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Valid%20Anagram)|Easy| |257|Binary Tree Paths|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Binary%20Tree%20Paths)|Easy| |258|Add Digits|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Add%20Digits)|Easy| |263|Ugly Number|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Ugly%20Number)|Easy| From 5d22494f60405c46ae1791da6674ba8c7f3ef9b8 Mon Sep 17 00:00:00 2001 From: Du Teng Date: Wed, 26 Aug 2015 17:23:37 +0800 Subject: [PATCH 027/126] update readme --- Algorithms/Excel Sheet Column Title/README.md | 2 +- README.md | 86 +++++++++---------- generate.js | 4 +- 3 files changed, 46 insertions(+), 46 deletions(-) diff --git a/Algorithms/Excel Sheet Column Title/README.md b/Algorithms/Excel Sheet Column Title/README.md index a3092ba..c6bd25e 100644 --- a/Algorithms/Excel Sheet Column Title/README.md +++ b/Algorithms/Excel Sheet Column Title/README.md @@ -1,4 +1,4 @@ -#[Excel Sheet Column Title ](https://leetcode.com/problems/excel-sheet-column-title/) +#[Excel Sheet Column Title](https://leetcode.com/problems/excel-sheet-column-title/) ######No:`168` ######Difficulty:`Easy` ##JavaScript diff --git a/README.md b/README.md index 73b966e..ac3116a 100644 --- a/README.md +++ b/README.md @@ -1,48 +1,48 @@ -leedcode +leetcode ======== My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) | No | Title | Source Code | Difficulty | -|----| ----- | -------- | ---------- ||7|Reverse Integer|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Reverse%20Integer)|Easy| -|8|String to Integer (atoi)|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/String%20to%20Integer%20(atoi))|Easy| -|9|Palindrome Number|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Palindrome%20Number)|Easy| -|20|Valid Parentheses|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Valid%20Parentheses)|Easy| -|21|Merge Two Sorted Lists|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Merge%20Two%20Sorted%20Lists)|Easy| -|26|Remove Duplicates from Sorted Array|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Remove%20Duplicates%20from%20Sorted%20Array)|Easy| -|27|Remove Element|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Remove%20Element)|Easy| -|46|Permutations|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Permutations)|Medium| -|66|Plus One|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Plus%20One)|Easy| -|73|Set Matrix Zeroes|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Set%20Matrix%20Zeroes)|Medium| -|88|Merge Sorted Array|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Merge%20Sorted%20Array)|Easy| -|100|Same Tree|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Same%20Tree)|Easy| -|101|Symmetric Tree|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Symmetric%20Tree)|Easy| -|102|Binary Tree Level Order Traversal|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Binary%20Tree%20Level%20Order%20Traversal)|Easy| -|104|Maximum Depth of Binary Tree|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Maximum%20Depth%20of%20Binary%20Tree)|Easy| -|107|Binary Tree Level Order Traversal II|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Binary%20Tree%20Level%20Order%20Traversal%20II)|Easy| -|110|Balanced Binary Tree|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Balanced%20Binary%20Tree)|Easy| -|111|Minimum Depth of Binary Tree|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Minimum%20Depth%20of%20Binary%20Tree)|Easy| -|112|Path Sum|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Path%20Sum)|Easy| -|118|Pascal's Triangle|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Pascal's%20Triangle)|Easy| -|122|Best Time to Buy and Sell Stock II|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Best%20Time%20to%20Buy%20and%20Sell%20Stock%20II)|Medium| -|125|Valid Palindrome|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Valid%20Palindrome)|Easy| -|136|Single Number|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Single%20Number)|Medium| -|148|Sort List|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Sort%20List)|Medium| -|150|Evaluate Reverse Polish Notation|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Evaluate%20Reverse%20Polish%20Notation)|Medium| -|151|Reverse Words in a String|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Reverse%20Words%20in%20a%20String)|Easy| -|155|Min Stack|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Min%20Stack)|Easy| -|160|Intersection of Two Linked Lists|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Intersection%20of%20Two%20Linked%20Lists)|Easy| -|165|ZigZag Conversion|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/ZigZag%20Conversion)|Easy| -|165|Compare Version Numbers|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Compare%20Version%20Numbers)|Easy| -|168|Excel Sheet Column Title |[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Excel%20Sheet%20Column%20Title%20)|Easy| -|169|Majority Element|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Majority%20Element)|Easy| -|171|Excel Sheet Column Number|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Excel%20Sheet%20Column%20Number)|Easy| -|179|Largest Number|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Largest%20Number)|Medium| -|189|Rotate Array|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Rotate%20Array)|Easy| -|217|Contains Duplicate|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Contains%20Duplicate)|Easy| -|226|Invert Binary Tree|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Invert%20Binary%20Tree)|Easy| -|235|Lowest Common Ancestor of a Binary Search Tree|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree)|Easy| -|242|Valid Anagram|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Valid%20Anagram)|Easy| -|257|Binary Tree Paths|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Binary%20Tree%20Paths)|Easy| -|258|Add Digits|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Add%20Digits)|Easy| -|263|Ugly Number|[JavaScript](https://github.com/duteng/leedcode/tree/master/Algorithms/Ugly%20Number)|Easy| +|----| ----- | -------- | ---------- ||7|Reverse Integer|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Integer)|Easy| +|8|String to Integer (atoi)|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/String%20to%20Integer%20(atoi))|Easy| +|9|Palindrome Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Palindrome%20Number)|Easy| +|20|Valid Parentheses|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Parentheses)|Easy| +|21|Merge Two Sorted Lists|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Merge%20Two%20Sorted%20Lists)|Easy| +|26|Remove Duplicates from Sorted Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Duplicates%20from%20Sorted%20Array)|Easy| +|27|Remove Element|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Element)|Easy| +|46|Permutations|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Permutations)|Medium| +|66|Plus One|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Plus%20One)|Easy| +|73|Set Matrix Zeroes|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Set%20Matrix%20Zeroes)|Medium| +|88|Merge Sorted Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Merge%20Sorted%20Array)|Easy| +|100|Same Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Same%20Tree)|Easy| +|101|Symmetric Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Symmetric%20Tree)|Easy| +|102|Binary Tree Level Order Traversal|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Binary%20Tree%20Level%20Order%20Traversal)|Easy| +|104|Maximum Depth of Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Maximum%20Depth%20of%20Binary%20Tree)|Easy| +|107|Binary Tree Level Order Traversal II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Binary%20Tree%20Level%20Order%20Traversal%20II)|Easy| +|110|Balanced Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Balanced%20Binary%20Tree)|Easy| +|111|Minimum Depth of Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Minimum%20Depth%20of%20Binary%20Tree)|Easy| +|112|Path Sum|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Path%20Sum)|Easy| +|118|Pascal's Triangle|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Pascal's%20Triangle)|Easy| +|122|Best Time to Buy and Sell Stock II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Best%20Time%20to%20Buy%20and%20Sell%20Stock%20II)|Medium| +|125|Valid Palindrome|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Palindrome)|Easy| +|136|Single Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Single%20Number)|Medium| +|148|Sort List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Sort%20List)|Medium| +|150|Evaluate Reverse Polish Notation|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Evaluate%20Reverse%20Polish%20Notation)|Medium| +|151|Reverse Words in a String|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Words%20in%20a%20String)|Easy| +|155|Min Stack|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Min%20Stack)|Easy| +|160|Intersection of Two Linked Lists|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Intersection%20of%20Two%20Linked%20Lists)|Easy| +|165|Compare Version Numbers|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Compare%20Version%20Numbers)|Easy| +|165|ZigZag Conversion|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/ZigZag%20Conversion)|Easy| +|168|Excel Sheet Column Title|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Excel%20Sheet%20Column%20Title)|Easy| +|169|Majority Element|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Majority%20Element)|Easy| +|171|Excel Sheet Column Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Excel%20Sheet%20Column%20Number)|Easy| +|179|Largest Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Largest%20Number)|Medium| +|189|Rotate Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Rotate%20Array)|Easy| +|217|Contains Duplicate|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Contains%20Duplicate)|Easy| +|226|Invert Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Invert%20Binary%20Tree)|Easy| +|235|Lowest Common Ancestor of a Binary Search Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree)|Easy| +|242|Valid Anagram|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Anagram)|Easy| +|257|Binary Tree Paths|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Binary%20Tree%20Paths)|Easy| +|258|Add Digits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Add%20Digits)|Easy| +|263|Ugly Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Ugly%20Number)|Easy| diff --git a/generate.js b/generate.js index 92784cc..9a4a003 100644 --- a/generate.js +++ b/generate.js @@ -3,7 +3,7 @@ var events = require('events'); var proxy = new events.EventEmitter(); var dir = './Algorithms'; -var repo_url = 'https://github.com/duteng/leedcode/tree/master/Algorithms/'; +var repo_url = 'https://github.com/duteng/leetcode/tree/master/Algorithms/'; var ret = []; targetDirs = fs.readdir(dir, function(err, files) { @@ -34,7 +34,7 @@ proxy.once('readDone', function() { }); var content = [ - 'leedcode', + 'leetcode', '========', 'My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/)', '', From 636758ae8a6eafefea5254163f3e4cdccd4d4501 Mon Sep 17 00:00:00 2001 From: Du Teng Date: Thu, 27 Aug 2015 03:01:36 +0800 Subject: [PATCH 028/126] add reverse linklist --- Algorithms/Reverse Linked List II/README.md | 78 +++++++++++++++++++++ Algorithms/Reverse Linked List/README.md | 32 +++++++++ 2 files changed, 110 insertions(+) create mode 100644 Algorithms/Reverse Linked List II/README.md create mode 100644 Algorithms/Reverse Linked List/README.md diff --git a/Algorithms/Reverse Linked List II/README.md b/Algorithms/Reverse Linked List II/README.md new file mode 100644 index 0000000..50e59c3 --- /dev/null +++ b/Algorithms/Reverse Linked List II/README.md @@ -0,0 +1,78 @@ +#[Reverse Linked List II](https://leetcode.com/problems/reverse-linked-list-ii/) +######No:`92` +######Difficulty:`Medium` +##JavaScript + +```javascript +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @param {number} m + * @param {number} n + * @return {ListNode} + */ + +var reverseBetween = function(head, m, n) { + function reverseList(head) { + if(!head) { + return head; + } + var prev = null; + var next = null; + while(head) { + next = head.next; + head.next = prev; + prev = head; + head = next; + } + return prev; + } + if(m === n) { + return head; + } + var middleLeft = null; + var middleRight = null; + var left = null; + var right = null; + + var i = 1; + var point = head; + while(point) { + if(i + 1 === m) { + left = point; + } + if(i === m) { + middleLeft = point; + } + if(i === n) { + middleRight = point; + } + if(i === n + 1) { + right = point; + } + i++; + point = point.next; + } + if(left) { + left.next = null; + } + + middleRight.next = null; + reverseList(middleLeft); + middleLeft.next = right; + + if(left) { + left.next = middleRight; + return head; + } else { + return middleRight; + } +}; + +``` diff --git a/Algorithms/Reverse Linked List/README.md b/Algorithms/Reverse Linked List/README.md new file mode 100644 index 0000000..368e363 --- /dev/null +++ b/Algorithms/Reverse Linked List/README.md @@ -0,0 +1,32 @@ +#[Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) +######No:`206` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @return {ListNode} + */ +var reverseList = function(head) { + if(!head) { + return head; + } + var prev = null; + var next = null; + while(head) { + next = head.next; + head.next = prev; + prev = head; + head = next; + } + return prev; +}; +``` From 0e0d78da00e9faaf02e124faf440fbd677953eaa Mon Sep 17 00:00:00 2001 From: Du Teng Date: Thu, 27 Aug 2015 03:02:32 +0800 Subject: [PATCH 029/126] add reverse linklist --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ac3116a..ffa4b77 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |66|Plus One|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Plus%20One)|Easy| |73|Set Matrix Zeroes|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Set%20Matrix%20Zeroes)|Medium| |88|Merge Sorted Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Merge%20Sorted%20Array)|Easy| +|92|Reverse Linked List II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Linked%20List%20II)|Medium| |100|Same Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Same%20Tree)|Easy| |101|Symmetric Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Symmetric%20Tree)|Easy| |102|Binary Tree Level Order Traversal|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Binary%20Tree%20Level%20Order%20Traversal)|Easy| @@ -32,13 +33,14 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |151|Reverse Words in a String|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Words%20in%20a%20String)|Easy| |155|Min Stack|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Min%20Stack)|Easy| |160|Intersection of Two Linked Lists|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Intersection%20of%20Two%20Linked%20Lists)|Easy| -|165|Compare Version Numbers|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Compare%20Version%20Numbers)|Easy| |165|ZigZag Conversion|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/ZigZag%20Conversion)|Easy| +|165|Compare Version Numbers|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Compare%20Version%20Numbers)|Easy| |168|Excel Sheet Column Title|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Excel%20Sheet%20Column%20Title)|Easy| |169|Majority Element|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Majority%20Element)|Easy| |171|Excel Sheet Column Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Excel%20Sheet%20Column%20Number)|Easy| |179|Largest Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Largest%20Number)|Medium| |189|Rotate Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Rotate%20Array)|Easy| +|206|Reverse Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Linked%20List)|Easy| |217|Contains Duplicate|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Contains%20Duplicate)|Easy| |226|Invert Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Invert%20Binary%20Tree)|Easy| |235|Lowest Common Ancestor of a Binary Search Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree)|Easy| From a1ef1496e6818df789020398f3187d36e8bc254b Mon Sep 17 00:00:00 2001 From: Du Teng Date: Thu, 3 Sep 2015 18:31:20 +0800 Subject: [PATCH 030/126] add Delete Node in a Linked List --- .../Delete Node in a Linked List/README.md | 22 +++++++++++++++++++ README.md | 3 ++- 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 Algorithms/Delete Node in a Linked List/README.md diff --git a/Algorithms/Delete Node in a Linked List/README.md b/Algorithms/Delete Node in a Linked List/README.md new file mode 100644 index 0000000..75f5014 --- /dev/null +++ b/Algorithms/Delete Node in a Linked List/README.md @@ -0,0 +1,22 @@ +#[Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/) +######No:`237` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} node + * @return {void} Do not return anything, modify node in-place instead. + */ +var deleteNode = function(node) { + node.val = node.next.val; + node.next = node.next.next; +}; +``` diff --git a/README.md b/README.md index ffa4b77..389ac77 100644 --- a/README.md +++ b/README.md @@ -33,8 +33,8 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |151|Reverse Words in a String|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Words%20in%20a%20String)|Easy| |155|Min Stack|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Min%20Stack)|Easy| |160|Intersection of Two Linked Lists|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Intersection%20of%20Two%20Linked%20Lists)|Easy| -|165|ZigZag Conversion|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/ZigZag%20Conversion)|Easy| |165|Compare Version Numbers|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Compare%20Version%20Numbers)|Easy| +|165|ZigZag Conversion|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/ZigZag%20Conversion)|Easy| |168|Excel Sheet Column Title|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Excel%20Sheet%20Column%20Title)|Easy| |169|Majority Element|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Majority%20Element)|Easy| |171|Excel Sheet Column Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Excel%20Sheet%20Column%20Number)|Easy| @@ -44,6 +44,7 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |217|Contains Duplicate|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Contains%20Duplicate)|Easy| |226|Invert Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Invert%20Binary%20Tree)|Easy| |235|Lowest Common Ancestor of a Binary Search Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree)|Easy| +|237|Delete Node in a Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Delete%20Node%20in%20a%20Linked%20List)|Easy| |242|Valid Anagram|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Anagram)|Easy| |257|Binary Tree Paths|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Binary%20Tree%20Paths)|Easy| |258|Add Digits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Add%20Digits)|Easy| From 85512072b0d70b6141a58ac7d3a74f3e07dcf5fb Mon Sep 17 00:00:00 2001 From: Du Teng Date: Fri, 4 Sep 2015 17:24:50 +0800 Subject: [PATCH 031/126] add --- Algorithms/Palindrome Linked List/README.md | 56 +++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 Algorithms/Palindrome Linked List/README.md diff --git a/Algorithms/Palindrome Linked List/README.md b/Algorithms/Palindrome Linked List/README.md new file mode 100644 index 0000000..ec81729 --- /dev/null +++ b/Algorithms/Palindrome Linked List/README.md @@ -0,0 +1,56 @@ +#[Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list/) +######No:`234` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @return {boolean} + */ +var isPalindrome = function(head) { + function reverseList(head) { + if(!head) { + return head; + } + var prev = null; + var next = null; + while(head) { + next = head.next; + head.next = prev; + prev = head; + head = next; + } + return prev; + } + if(!head || !head.next ) { + return true; + } + var slow = head; + var fast = head; + while(fast && fast.next && fast.next.next) { + slow = slow.next; + fast = fast.next.next; + } + + slow.next = reverseList(slow.next); + slow = slow.next; + + while(slow) { + if(head.val !== slow.val) { + return false; + } + head = head.next; + slow = slow.next; + } + + return true; +}; +``` From 28eaf8ebae6bc85f8232616da3567bd657f6558e Mon Sep 17 00:00:00 2001 From: Du Teng Date: Fri, 4 Sep 2015 18:14:09 +0800 Subject: [PATCH 032/126] Remove Linked List Elements --- .../Remove Linked List Elements/README.md | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Algorithms/Remove Linked List Elements/README.md diff --git a/Algorithms/Remove Linked List Elements/README.md b/Algorithms/Remove Linked List Elements/README.md new file mode 100644 index 0000000..93ee4c2 --- /dev/null +++ b/Algorithms/Remove Linked List Elements/README.md @@ -0,0 +1,42 @@ +#[Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements/) +######No:`203` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @param {number} val + * @return {ListNode} + */ +var removeElements = function(head, val) { + while(head && head.val == val) { + head = head.next; + } + + if(!head) { + return head; + } + + var prev = head; + var iter = head.next; + + while(iter) { + if(iter.val == val) { + prev.next = iter.next; + } else { + prev = iter; + } + iter = iter.next; + } + + return head; +}; +``` From e876f0feed82c38d9ebaeac2a687e68c2524d4bb Mon Sep 17 00:00:00 2001 From: Du Teng Date: Fri, 4 Sep 2015 23:40:02 +0800 Subject: [PATCH 033/126] update readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 389ac77..723a7bc 100644 --- a/README.md +++ b/README.md @@ -40,9 +40,11 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |171|Excel Sheet Column Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Excel%20Sheet%20Column%20Number)|Easy| |179|Largest Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Largest%20Number)|Medium| |189|Rotate Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Rotate%20Array)|Easy| +|203|Remove Linked List Elements|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Linked%20List%20Elements)|Easy| |206|Reverse Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Linked%20List)|Easy| |217|Contains Duplicate|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Contains%20Duplicate)|Easy| |226|Invert Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Invert%20Binary%20Tree)|Easy| +|234|Palindrome Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Palindrome%20Linked%20List)|Easy| |235|Lowest Common Ancestor of a Binary Search Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree)|Easy| |237|Delete Node in a Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Delete%20Node%20in%20a%20Linked%20List)|Easy| |242|Valid Anagram|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Anagram)|Easy| From 4596f4d2c06bc6cbe02f0e472bde5aaa3b4ac273 Mon Sep 17 00:00:00 2001 From: Du Teng Date: Sat, 5 Sep 2015 00:23:07 +0800 Subject: [PATCH 034/126] Remove Nth Node From End of List --- .../README.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Algorithms/Remove Nth Node From End of List/README.md diff --git a/Algorithms/Remove Nth Node From End of List/README.md b/Algorithms/Remove Nth Node From End of List/README.md new file mode 100644 index 0000000..d687f94 --- /dev/null +++ b/Algorithms/Remove Nth Node From End of List/README.md @@ -0,0 +1,38 @@ +#[Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/) +######No:`19` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @param {number} n + * @return {ListNode} + */ +var removeNthFromEnd = function(head, n) { + var front = head; + var end = head; + for(var i = 0; i <= n && front; i++) { + front = front.next; + } + + // delete first node + if(i < n + 1) { + return head.next; + } + + while(front) { + front = front.next; + end = end.next; + } + end.next = end.next.next; + return head; +}; +``` From 0e9baca88506dd82d375447ef5633a97f663c876 Mon Sep 17 00:00:00 2001 From: Du Teng Date: Sat, 5 Sep 2015 00:28:15 +0800 Subject: [PATCH 035/126] /** --- .../README.md | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Algorithms/Remove Duplicates from Sorted List/README.md diff --git a/Algorithms/Remove Duplicates from Sorted List/README.md b/Algorithms/Remove Duplicates from Sorted List/README.md new file mode 100644 index 0000000..adf47bd --- /dev/null +++ b/Algorithms/Remove Duplicates from Sorted List/README.md @@ -0,0 +1,29 @@ +#[Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list/) +######No:`83` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @return {ListNode} + */ +var deleteDuplicates = function(head) { + var iter = head; + while(iter) { + if(iter.next && iter.val === iter.next.val) { + iter.next = iter.next.next; + } else { + iter = iter.next; + } + } + return head; +}; +``` From 1d618383936927c375605b4c8effe0f91d7f4e01 Mon Sep 17 00:00:00 2001 From: Du Teng Date: Sat, 5 Sep 2015 02:30:10 +0800 Subject: [PATCH 036/126] Summary Ranges --- Algorithms/Summary Ranges/README.md | 37 +++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Algorithms/Summary Ranges/README.md diff --git a/Algorithms/Summary Ranges/README.md b/Algorithms/Summary Ranges/README.md new file mode 100644 index 0000000..9134dec --- /dev/null +++ b/Algorithms/Summary Ranges/README.md @@ -0,0 +1,37 @@ +#[Summary Ranges](https://leetcode.com/problems/summary-ranges/) +######No:`228` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {number[]} nums + * @return {string[]} + */ +var summaryRanges = function(nums) { + if(nums.length === 0) { + return nums; + } + + var ret = []; + var start = 0; + + for(var i = 1; i < nums.length; i++) { + if(nums[i] - nums[i-1] != 1) { + ret.push(nums.slice(start, i)); + start = i; + } + } + ret.push(nums.slice(start)); + + ret = ret.map(function(item) { + if(item.length > 1) { + return item[0] + '->' + item[item.length - 1]; + } else { + return item[0] + ''; + } + }); + + return ret; +}; +``` From f1ab8c31603c36ceabb80a0f29e8d0bf4a7d73f5 Mon Sep 17 00:00:00 2001 From: Du Teng Date: Sat, 5 Sep 2015 03:03:38 +0800 Subject: [PATCH 037/126] Pascal's Triangle II --- Algorithms/Pascal's Triangle II/README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Algorithms/Pascal's Triangle II/README.md diff --git a/Algorithms/Pascal's Triangle II/README.md b/Algorithms/Pascal's Triangle II/README.md new file mode 100644 index 0000000..35b5f56 --- /dev/null +++ b/Algorithms/Pascal's Triangle II/README.md @@ -0,0 +1,22 @@ +#[Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii/) +######No:`119` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {number} rowIndex + * @return {number[]} + */ +var getRow = function(rowIndex) { + var ret = []; + ret[0] = 1; + for(var i = 1; i <= rowIndex; i++ ) { + ret[i] = 1; + for(j = i - 1; j > 0; j--) { + ret[j] = ret[j] + ret[j - 1]; + } + } + return ret; +}; +``` From 08249d25c2af9d4a7e200e216cbf7c0f4b54bf8b Mon Sep 17 00:00:00 2001 From: Du Teng Date: Sat, 5 Sep 2015 03:04:37 +0800 Subject: [PATCH 038/126] readme --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 723a7bc..342bfaf 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |----| ----- | -------- | ---------- ||7|Reverse Integer|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Integer)|Easy| |8|String to Integer (atoi)|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/String%20to%20Integer%20(atoi))|Easy| |9|Palindrome Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Palindrome%20Number)|Easy| +|19|Remove Nth Node From End of List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Nth%20Node%20From%20End%20of%20List)|Easy| |20|Valid Parentheses|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Parentheses)|Easy| |21|Merge Two Sorted Lists|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Merge%20Two%20Sorted%20Lists)|Easy| |26|Remove Duplicates from Sorted Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Duplicates%20from%20Sorted%20Array)|Easy| @@ -14,6 +15,7 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |46|Permutations|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Permutations)|Medium| |66|Plus One|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Plus%20One)|Easy| |73|Set Matrix Zeroes|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Set%20Matrix%20Zeroes)|Medium| +|83|Remove Duplicates from Sorted List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Duplicates%20from%20Sorted%20List)|Easy| |88|Merge Sorted Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Merge%20Sorted%20Array)|Easy| |92|Reverse Linked List II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Linked%20List%20II)|Medium| |100|Same Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Same%20Tree)|Easy| @@ -25,6 +27,7 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |111|Minimum Depth of Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Minimum%20Depth%20of%20Binary%20Tree)|Easy| |112|Path Sum|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Path%20Sum)|Easy| |118|Pascal's Triangle|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Pascal's%20Triangle)|Easy| +|119|Pascal's Triangle II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Pascal's%20Triangle%20II)|Easy| |122|Best Time to Buy and Sell Stock II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Best%20Time%20to%20Buy%20and%20Sell%20Stock%20II)|Medium| |125|Valid Palindrome|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Palindrome)|Easy| |136|Single Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Single%20Number)|Medium| @@ -33,8 +36,8 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |151|Reverse Words in a String|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Words%20in%20a%20String)|Easy| |155|Min Stack|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Min%20Stack)|Easy| |160|Intersection of Two Linked Lists|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Intersection%20of%20Two%20Linked%20Lists)|Easy| -|165|Compare Version Numbers|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Compare%20Version%20Numbers)|Easy| |165|ZigZag Conversion|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/ZigZag%20Conversion)|Easy| +|165|Compare Version Numbers|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Compare%20Version%20Numbers)|Easy| |168|Excel Sheet Column Title|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Excel%20Sheet%20Column%20Title)|Easy| |169|Majority Element|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Majority%20Element)|Easy| |171|Excel Sheet Column Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Excel%20Sheet%20Column%20Number)|Easy| @@ -44,6 +47,7 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |206|Reverse Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Linked%20List)|Easy| |217|Contains Duplicate|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Contains%20Duplicate)|Easy| |226|Invert Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Invert%20Binary%20Tree)|Easy| +|228|Summary Ranges|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Summary%20Ranges)|Easy| |234|Palindrome Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Palindrome%20Linked%20List)|Easy| |235|Lowest Common Ancestor of a Binary Search Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree)|Easy| |237|Delete Node in a Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Delete%20Node%20in%20a%20Linked%20List)|Easy| From a89601ee36f027969e9eed8159e7a59d4c49b407 Mon Sep 17 00:00:00 2001 From: Teng DU Date: Wed, 9 Mar 2016 19:19:09 +0800 Subject: [PATCH 039/126] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 342bfaf..317aefb 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,8 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) | No | Title | Source Code | Difficulty | -|----| ----- | -------- | ---------- ||7|Reverse Integer|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Integer)|Easy| +|----| ----- | -------- | ---------- | +|7|Reverse Integer|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Integer)|Easy| |8|String to Integer (atoi)|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/String%20to%20Integer%20(atoi))|Easy| |9|Palindrome Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Palindrome%20Number)|Easy| |19|Remove Nth Node From End of List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Nth%20Node%20From%20End%20of%20List)|Easy| From 6a776186f6264534539da44d510a702e9a78d104 Mon Sep 17 00:00:00 2001 From: Teng DU Date: Sun, 20 Mar 2016 02:40:37 +0800 Subject: [PATCH 040/126] add new line breaks --- Algorithms/ZigZag Conversion/README.md | 2 +- README.md | 2 +- generate.js | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Algorithms/ZigZag Conversion/README.md b/Algorithms/ZigZag Conversion/README.md index 7825704..8496ac0 100644 --- a/Algorithms/ZigZag Conversion/README.md +++ b/Algorithms/ZigZag Conversion/README.md @@ -1,5 +1,5 @@ #[ZigZag Conversion](https://leetcode.com/problems/zigzag-conversion/) -######No:`165` +######No:`6` ######Difficulty:`Easy` ##JavaScript diff --git a/README.md b/README.md index 317aefb..75afd7e 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) | No | Title | Source Code | Difficulty | |----| ----- | -------- | ---------- | +|6|ZigZag Conversion|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/ZigZag%20Conversion)|Easy| |7|Reverse Integer|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Integer)|Easy| |8|String to Integer (atoi)|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/String%20to%20Integer%20(atoi))|Easy| |9|Palindrome Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Palindrome%20Number)|Easy| @@ -37,7 +38,6 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |151|Reverse Words in a String|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Words%20in%20a%20String)|Easy| |155|Min Stack|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Min%20Stack)|Easy| |160|Intersection of Two Linked Lists|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Intersection%20of%20Two%20Linked%20Lists)|Easy| -|165|ZigZag Conversion|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/ZigZag%20Conversion)|Easy| |165|Compare Version Numbers|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Compare%20Version%20Numbers)|Easy| |168|Excel Sheet Column Title|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Excel%20Sheet%20Column%20Title)|Easy| |169|Majority Element|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Majority%20Element)|Easy| diff --git a/generate.js b/generate.js index 9a4a003..50c36cc 100644 --- a/generate.js +++ b/generate.js @@ -40,7 +40,8 @@ proxy.once('readDone', function() { '', '', '| No | Title | Source Code | Difficulty |', - '|----| ----- | -------- | ---------- |'].join('\r\n'); + '|----| ----- | -------- | ---------- |' + ].join('\r\n') + '\r\n'; for(var i = 0; i < ret.length; i++) { content += '|' + ret[i].no + '|' + ret[i].title + '|' + ret[i].link + '|' + ret[i].difficulty + '|' + '\r\n'; From 56eface1134851b321e2dafb294f3fdc80363a70 Mon Sep 17 00:00:00 2001 From: Teng DU Date: Sat, 2 Apr 2016 18:30:36 +0800 Subject: [PATCH 041/126] add --- .gitignore | 1 + Algorithms/Move Zeroes/README.md | 21 ++++++++++++ Algorithms/Nim Game/README.md | 14 ++++++++ Algorithms/Number of 1 Bits/README.md | 28 +++++++++++++++ Algorithms/Roman to Integer/README.md | 49 +++++++++++++++++++++++++++ 5 files changed, 113 insertions(+) create mode 100644 .gitignore create mode 100644 Algorithms/Move Zeroes/README.md create mode 100644 Algorithms/Nim Game/README.md create mode 100644 Algorithms/Number of 1 Bits/README.md create mode 100644 Algorithms/Roman to Integer/README.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dfe8e2e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +Algorithms/README.md diff --git a/Algorithms/Move Zeroes/README.md b/Algorithms/Move Zeroes/README.md new file mode 100644 index 0000000..c56236e --- /dev/null +++ b/Algorithms/Move Zeroes/README.md @@ -0,0 +1,21 @@ +#[Move Zeroes](https://leetcode.com/problems/move-zeroes/) +######No:`283` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {number[]} nums + * @return {void} Do not return anything, modify nums in-place instead. + */ +var moveZeroes = function(nums) { + var sum = 0; + for(var i = nums.length - 1; i >=0; i--) { + if(nums[i] === 0) { + nums.splice(i,1); + sum++; + } + } + [].push.apply(nums, new Array(sum).fill(0)) +}; +``` diff --git a/Algorithms/Nim Game/README.md b/Algorithms/Nim Game/README.md new file mode 100644 index 0000000..8b3aa46 --- /dev/null +++ b/Algorithms/Nim Game/README.md @@ -0,0 +1,14 @@ +#[Nim Game](https://leetcode.com/problems/nim-game/) +######No:`292` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {number} n + * @return {boolean} + */ +var canWinNim = function(n) { + return !(n !== 0 && n%4 === 0); +}; +``` diff --git a/Algorithms/Number of 1 Bits/README.md b/Algorithms/Number of 1 Bits/README.md new file mode 100644 index 0000000..eaf6d79 --- /dev/null +++ b/Algorithms/Number of 1 Bits/README.md @@ -0,0 +1,28 @@ +#[Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/) +######No:`191` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {number} n - a positive integer + * @return {number} + */ +var hammingWeight = function(n) { + var ret = 0; + const base = 2; + const bitnum = 32; + var power = bitnum -1; + + for(var i = 0; i < bitnum; i++) { + if(n >= Math.pow(base, power)) { + ret++; + n -= Math.pow(base, power); + } + power--; + } + + return ret; +}; + +``` diff --git a/Algorithms/Roman to Integer/README.md b/Algorithms/Roman to Integer/README.md new file mode 100644 index 0000000..522efb4 --- /dev/null +++ b/Algorithms/Roman to Integer/README.md @@ -0,0 +1,49 @@ +#[Roman to Integer](https://leetcode.com/problems/roman-to-integer/) +######No:`13` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {string} s + * @return {number} + * + * Symbol Value + * I 1 + * V 5 + * X 10 + * L 50 + * C 100 + * D 500 + * M 1,000 + + */ +var romanToInt = function(s) { + var map = { + 'I':1, + 'V':5, + 'X':10, + 'L':50, + 'C':100, + 'D':500, + 'M':1000 + }; + + var sum = 0; + var right = 'I'; + + for(var i = s.length - 1; i >=0; i--) { + if(i < s.length - 1) { + right = s[i + 1]; + } + if(map[s[i]] < map[right]) { + sum -= map[s[i]]; + } else { + sum += map[s[i]]; + right = s[i]; + } + } + return sum; +}; + +``` From bdf2283c0683fed0f3dd592071c2ad1c72c8daf6 Mon Sep 17 00:00:00 2001 From: Teng DU Date: Thu, 7 Apr 2016 01:24:22 +0800 Subject: [PATCH 042/126] add --- Algorithms/Climbing Stairs/README.md | 24 +++++++++++++++ Algorithms/Happy Number/README.md | 29 ++++++++++++++++++ Algorithms/Odd Even Linked List/README.md | 37 +++++++++++++++++++++++ README.md | 7 +++++ 4 files changed, 97 insertions(+) create mode 100644 Algorithms/Climbing Stairs/README.md create mode 100644 Algorithms/Happy Number/README.md create mode 100644 Algorithms/Odd Even Linked List/README.md diff --git a/Algorithms/Climbing Stairs/README.md b/Algorithms/Climbing Stairs/README.md new file mode 100644 index 0000000..712e782 --- /dev/null +++ b/Algorithms/Climbing Stairs/README.md @@ -0,0 +1,24 @@ +#[Climbing Stairs](https://leetcode.com/problems/climbing-stairs/) +######No:`70` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {number} n + * @return + * + * s[n] = s[n-1] + s[n] + * s[1] = 1; + * s[2] = 2; + */ +var climbStairs = function(n) { + var s = {}; + s[1] = 1; + s[2] = 2; + for(var i = 3; i<=n; i++) { + s[i] = s[i-1] + s[i-2]; + } + return s[n]; +}; +``` diff --git a/Algorithms/Happy Number/README.md b/Algorithms/Happy Number/README.md new file mode 100644 index 0000000..fa3d5ab --- /dev/null +++ b/Algorithms/Happy Number/README.md @@ -0,0 +1,29 @@ +#[Happy Number](https://leetcode.com/problems/happy-number/) +######No:`202` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {number} n + * @return {boolean} + */ + +var isHappy = function(n) { + var nums = []; + var ret = n; + + var squire = x => Math.pow(parseInt(x), 2); + var add = (prev, cur) => prev + cur; + while(ret !== 1) { + if(nums.indexOf(ret) > -1) { + return false; + } else { + nums.push(ret); + } + digits = ret.toString().split('').map(squire); + ret = digits.reduce(add); + } + return true; +} +``` diff --git a/Algorithms/Odd Even Linked List/README.md b/Algorithms/Odd Even Linked List/README.md new file mode 100644 index 0000000..d074807 --- /dev/null +++ b/Algorithms/Odd Even Linked List/README.md @@ -0,0 +1,37 @@ +#[Odd Even Linked List](https://leetcode.com/problems/odd-even-linked-list/) +######No:`328` +######Difficulty:`Medium` +##JavaScript + +```javascript +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @return {ListNode} + */ +var oddEvenList = function(head) { + if(head === null) { + return null; + } + var second = head.next; + + var odd = head; + var even = head.next; + while(odd && odd.next && odd.next.next) { + odd.next = even.next; + odd = odd.next; + if(odd) { + even.next = odd.next; + even = even.next; + } + } + odd.next = second; + return head; +}; +``` diff --git a/README.md b/README.md index 75afd7e..a7f1995 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |7|Reverse Integer|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Integer)|Easy| |8|String to Integer (atoi)|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/String%20to%20Integer%20(atoi))|Easy| |9|Palindrome Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Palindrome%20Number)|Easy| +|13|Roman to Integer|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Roman%20to%20Integer)|Easy| |19|Remove Nth Node From End of List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Nth%20Node%20From%20End%20of%20List)|Easy| |20|Valid Parentheses|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Parentheses)|Easy| |21|Merge Two Sorted Lists|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Merge%20Two%20Sorted%20Lists)|Easy| @@ -16,6 +17,7 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |27|Remove Element|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Element)|Easy| |46|Permutations|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Permutations)|Medium| |66|Plus One|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Plus%20One)|Easy| +|70|Climbing Stairs|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Climbing%20Stairs)|Easy| |73|Set Matrix Zeroes|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Set%20Matrix%20Zeroes)|Medium| |83|Remove Duplicates from Sorted List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Duplicates%20from%20Sorted%20List)|Easy| |88|Merge Sorted Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Merge%20Sorted%20Array)|Easy| @@ -44,6 +46,8 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |171|Excel Sheet Column Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Excel%20Sheet%20Column%20Number)|Easy| |179|Largest Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Largest%20Number)|Medium| |189|Rotate Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Rotate%20Array)|Easy| +|191|Number of 1 Bits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Number%20of%201%20Bits)|Easy| +|202|Happy Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Happy%20Number)|Easy| |203|Remove Linked List Elements|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Linked%20List%20Elements)|Easy| |206|Reverse Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Linked%20List)|Easy| |217|Contains Duplicate|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Contains%20Duplicate)|Easy| @@ -56,3 +60,6 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |257|Binary Tree Paths|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Binary%20Tree%20Paths)|Easy| |258|Add Digits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Add%20Digits)|Easy| |263|Ugly Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Ugly%20Number)|Easy| +|283|Move Zeroes|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Move%20Zeroes)|Easy| +|292|Nim Game|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Nim%20Game)|Easy| +|328|Odd Even Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Odd%20Even%20Linked%20List)|Medium| From a9e608f700dcb3676b5fc74887a5c51667da6554 Mon Sep 17 00:00:00 2001 From: Teng DU Date: Thu, 7 Apr 2016 21:36:18 +0800 Subject: [PATCH 043/126] add --- Algorithms/Power of Two/README.md | 23 +++++++++++++ Algorithms/Swap Nodes in Pairs/README.md | 43 ++++++++++++++++++++++++ README.md | 2 ++ 3 files changed, 68 insertions(+) create mode 100644 Algorithms/Power of Two/README.md create mode 100644 Algorithms/Swap Nodes in Pairs/README.md diff --git a/Algorithms/Power of Two/README.md b/Algorithms/Power of Two/README.md new file mode 100644 index 0000000..18ffe0b --- /dev/null +++ b/Algorithms/Power of Two/README.md @@ -0,0 +1,23 @@ +#[Power of Two](https://leetcode.com/problems/power-of-two/) +######No:`231` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {number} n + * @return {boolean} + */ +var isPowerOfTwo = function(n) { + if(n === 0) return false; + while(n !== 1) { + if(n % 2 === 0) { + n = n / 2; + } else { + return false; + } + } + return true; +}; +``` + diff --git a/Algorithms/Swap Nodes in Pairs/README.md b/Algorithms/Swap Nodes in Pairs/README.md new file mode 100644 index 0000000..42ee251 --- /dev/null +++ b/Algorithms/Swap Nodes in Pairs/README.md @@ -0,0 +1,43 @@ +#[Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) +######No:`24` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @return {ListNode} + */ +var swapPairs = function(head) { + if(head === null || head.next === null) { + return head; + } + + var newhead = null; + var tmp1 = head.next; + var tmp2 = head.next.next; + head.next.next = head; + head.next = tmp2; + + newhead = tmp1; + + var p = head; + while(p && p.next && p.next.next) { + tmp1 = p.next.next; + tmp2 = p.next.next.next; + p.next.next.next = p.next; + p.next.next = tmp2; + p.next = tmp1; + p = p.next.next; + } + + return newhead; +}; +``` diff --git a/README.md b/README.md index a7f1995..92730b2 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |19|Remove Nth Node From End of List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Nth%20Node%20From%20End%20of%20List)|Easy| |20|Valid Parentheses|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Parentheses)|Easy| |21|Merge Two Sorted Lists|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Merge%20Two%20Sorted%20Lists)|Easy| +|24|Swap Nodes in Pairs|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Swap%20Nodes%20in%20Pairs)|Easy| |26|Remove Duplicates from Sorted Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Duplicates%20from%20Sorted%20Array)|Easy| |27|Remove Element|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Element)|Easy| |46|Permutations|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Permutations)|Medium| @@ -53,6 +54,7 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |217|Contains Duplicate|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Contains%20Duplicate)|Easy| |226|Invert Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Invert%20Binary%20Tree)|Easy| |228|Summary Ranges|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Summary%20Ranges)|Easy| +|231|Power of Two|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Power%20of%20Two)|Easy| |234|Palindrome Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Palindrome%20Linked%20List)|Easy| |235|Lowest Common Ancestor of a Binary Search Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree)|Easy| |237|Delete Node in a Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Delete%20Node%20in%20a%20Linked%20List)|Easy| From 612344767b7fcd8f01a63f879cf5e4b47eda438e Mon Sep 17 00:00:00 2001 From: Teng DU Date: Thu, 7 Apr 2016 23:37:37 +0800 Subject: [PATCH 044/126] add --- Algorithms/Max Points on a Line/README.md | 54 +++++++++++++++++++++++ README.md | 1 + 2 files changed, 55 insertions(+) create mode 100644 Algorithms/Max Points on a Line/README.md diff --git a/Algorithms/Max Points on a Line/README.md b/Algorithms/Max Points on a Line/README.md new file mode 100644 index 0000000..6437b5a --- /dev/null +++ b/Algorithms/Max Points on a Line/README.md @@ -0,0 +1,54 @@ +#[Max Points on a Line](https://leetcode.com/problems/max-points-on-a-line/) +######No:`149` +######Difficulty:`Hard` +##CPP + +```cpp +/** + * Definition for a point. + * struct Point { + * int x; + * int y; + * Point() : x(0), y(0) {} + * Point(int a, int b) : x(a), y(b) {} + * }; + */ +class Solution { +public: + int maxPoints(vector &points) { + if(points.size() < 3) { + return points.size(); + } + int maxPoints = 0; //the max point in line + int size = points.size(); + map count; + map::iterator iter; + for(int i = 0; i < size; i++ ) { + int x1 = points[i].x; + int y1 = points[i].y; + int coincideCount = 0; //number of duplicate points + count.clear(); + count[(double)INT_MIN] = 0; + for(int j = i + 1; j < size; j++) { + int x2 = points[j].x; + int y2 = points[j].y; + if(x1 == x2 && y1 == y2) { + coincideCount++; + } else if(x1 == x2){ + count[(double)INT_MIN]++; + } else { + double slope = 1.0*(y1-y2)/(x1-x2); + count[slope]++; + } + } + for(iter = count.begin(); iter != count.end(); iter++) { + if(iter->second + coincideCount > maxPoints) { + maxPoints = iter->second + coincideCount; + } + } + } + maxPoints = maxPoints + 1; + return maxPoints; + } +}; +``` diff --git a/README.md b/README.md index 92730b2..107b0e4 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |125|Valid Palindrome|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Palindrome)|Easy| |136|Single Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Single%20Number)|Medium| |148|Sort List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Sort%20List)|Medium| +|149|Max Points on a Line|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Max%20Points%20on%20a%20Line)|Hard| |150|Evaluate Reverse Polish Notation|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Evaluate%20Reverse%20Polish%20Notation)|Medium| |151|Reverse Words in a String|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Words%20in%20a%20String)|Easy| |155|Min Stack|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Min%20Stack)|Easy| From cc412897b14d6710e3185254ad2ad859e4c477f7 Mon Sep 17 00:00:00 2001 From: Teng DU Date: Fri, 8 Apr 2016 09:38:51 +0800 Subject: [PATCH 045/126] add --- Algorithms/House Robber/README.md | 35 +++++++++++++++++++++++++++++ Algorithms/Power of Three/README.md | 27 ++++++++++++++++++++++ Algorithms/Two Sum/README.md | 24 ++++++++++++++++++++ README.md | 3 +++ 4 files changed, 89 insertions(+) create mode 100644 Algorithms/House Robber/README.md create mode 100644 Algorithms/Power of Three/README.md create mode 100644 Algorithms/Two Sum/README.md diff --git a/Algorithms/House Robber/README.md b/Algorithms/House Robber/README.md new file mode 100644 index 0000000..3224308 --- /dev/null +++ b/Algorithms/House Robber/README.md @@ -0,0 +1,35 @@ +#[House Robber](https://leetcode.com/problems/house-robber/) +######No:`198` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {number[]} nums + * @return {number} + */ +var rob = function(nums) { + if(nums.length === 0) { + return 0; + } + if(nums.length === 1) { + return nums[0]; + } + + var max = {}; + max[0] = nums[0]; + max[1] = Math.max(nums[0], nums[1]); + for(var i = 2; i < nums.length; i++) { + max[i] = Math.max(max[i - 2] + nums[i], max[i -1]); + } + return max[nums.length - 1]; +}; +``` + +``` +###How to solve? +``` +#####Using DP. +max[0] = nums[0]; +max[1] = Math.max(nums[0], nums[1]); +max[n] = Math.max(max[n] + max[n - 2], max[n -1]); diff --git a/Algorithms/Power of Three/README.md b/Algorithms/Power of Three/README.md new file mode 100644 index 0000000..b235d80 --- /dev/null +++ b/Algorithms/Power of Three/README.md @@ -0,0 +1,27 @@ +#[Power of Three](https://leetcode.com/problems/power-of-three/) +######No:`326` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {number} n + * @return {boolean} + */ +var isPowerOfThree = function(n) { + while(true) { + if(n === 1) { + return true; + } + if(n === 0 || n === 2) { + return false; + } + if(n % 3 !== 0) { + return false; + } else { + n = n / 3; + } + } + return true; +}; +``` diff --git a/Algorithms/Two Sum/README.md b/Algorithms/Two Sum/README.md new file mode 100644 index 0000000..148aaa8 --- /dev/null +++ b/Algorithms/Two Sum/README.md @@ -0,0 +1,24 @@ +#[Two Sum](https://leetcode.com/problems/two-sum/) +######No:`1` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {number[]} nums + * @param {number} target + * @return {number[]} + */ +var twoSum = function(nums, target) { + var ret = []; + for(var i = 0; i < nums.length; i++) { + for(var j = i + 1; j < nums.length; j++) { + if(nums[i] + nums[j] === target) { + ret.push(i); + ret.push(j); + } + } + } + return ret; +}; +``` diff --git a/README.md b/README.md index 107b0e4..82ae9ec 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) | No | Title | Source Code | Difficulty | |----| ----- | -------- | ---------- | +|1|Two Sum|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Two%20Sum)|Easy| |6|ZigZag Conversion|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/ZigZag%20Conversion)|Easy| |7|Reverse Integer|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Integer)|Easy| |8|String to Integer (atoi)|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/String%20to%20Integer%20(atoi))|Easy| @@ -49,6 +50,7 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |179|Largest Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Largest%20Number)|Medium| |189|Rotate Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Rotate%20Array)|Easy| |191|Number of 1 Bits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Number%20of%201%20Bits)|Easy| +|198|House Robber|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/House%20Robber)|Easy| |202|Happy Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Happy%20Number)|Easy| |203|Remove Linked List Elements|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Linked%20List%20Elements)|Easy| |206|Reverse Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Linked%20List)|Easy| @@ -65,4 +67,5 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |263|Ugly Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Ugly%20Number)|Easy| |283|Move Zeroes|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Move%20Zeroes)|Easy| |292|Nim Game|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Nim%20Game)|Easy| +|326|Power of Three|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Power%20of%20Three)|Easy| |328|Odd Even Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Odd%20Even%20Linked%20List)|Medium| From 052c9b67ecafb74faefc47c42140f548c20a673e Mon Sep 17 00:00:00 2001 From: Teng DU Date: Sun, 10 Apr 2016 00:56:47 +0800 Subject: [PATCH 046/126] add --- Algorithms/Contains Duplicate II/README.md | 37 ++++++++++++ .../Factorial Trailing Zeroes/README.md | 29 ++++++++++ .../Implement Queue using Stacks/README.md | 58 +++++++++++++++++++ Algorithms/Rectangle Area/README.md | 34 +++++++++++ Algorithms/Valid Sudoku/README.md | 43 ++++++++++++++ README.md | 5 ++ 6 files changed, 206 insertions(+) create mode 100644 Algorithms/Contains Duplicate II/README.md create mode 100644 Algorithms/Factorial Trailing Zeroes/README.md create mode 100644 Algorithms/Implement Queue using Stacks/README.md create mode 100644 Algorithms/Rectangle Area/README.md create mode 100644 Algorithms/Valid Sudoku/README.md diff --git a/Algorithms/Contains Duplicate II/README.md b/Algorithms/Contains Duplicate II/README.md new file mode 100644 index 0000000..962b735 --- /dev/null +++ b/Algorithms/Contains Duplicate II/README.md @@ -0,0 +1,37 @@ +#[Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/) +######No:`219` +######Difficulty:`Easy` +##JavaScript + +####Solution 1: (slower) +```javascript +var containsNearbyDuplicate = function(nums, k) { + for(var i = 0; i < nums.length; i++) { + for(var j = i + 1; j < nums.length && j - i <= k; j++) { + if(nums[i] === nums[j]) { + return true; + } + } + } + return false; +}; +``` + +####Solution 2: (faster) +```javascript +var containsNearbyDuplicate = function(nums, k) { + var index = {}; + var value; + for(var i = 0; i < nums.length; i++) { + value = nums[i]; + if(index[value] === undefined) { + index[value] = [i]; + } else if(i - index[value][index[value].length - 1] <= k){ + return true; + } else { + index[value].push(i); + } + } + return false; +} +``` diff --git a/Algorithms/Factorial Trailing Zeroes/README.md b/Algorithms/Factorial Trailing Zeroes/README.md new file mode 100644 index 0000000..160b2c3 --- /dev/null +++ b/Algorithms/Factorial Trailing Zeroes/README.md @@ -0,0 +1,29 @@ +#[Factorial Trailing Zeroes](https://leetcode.com/problems/factorial-trailing-zeroes/) +######No:`172` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {number} n + * @return {number} + */ +var trailingZeroes = function(n) { + var sum = 0; + var divisor = 5; + while(n >= divisor) { + sum += Math.floor(n / divisor); + divisor *= 5; + } + return sum; +}; +``` + +###How to solve? +Only 2 * 5 can make 0. +4, 6, 8 can represent as 2 * 2, 2 * 3, 2 * 2 * 2. So 2 is more then 5.You need only count the number 5. +5 catains one 5; +25 contains two 5; +125.. + +So count the number 5 is the answer. diff --git a/Algorithms/Implement Queue using Stacks/README.md b/Algorithms/Implement Queue using Stacks/README.md new file mode 100644 index 0000000..c7dacaa --- /dev/null +++ b/Algorithms/Implement Queue using Stacks/README.md @@ -0,0 +1,58 @@ +#[Implement Queue using Stacks](https://leetcode.com/problems/implement-queue-using-stacks/) +######No:`232` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @constructor + */ +var Queue = function() { + this.stack1 = []; + this.stack2 =[]; +}; + +/** + * @param {number} x + * @returns {void} + */ +Queue.prototype.push = function(x) { + this.stack1.push(x); +}; + +/** + * @returns {void} + */ +Queue.prototype.pop = function() { + if(this.stack2.length > 0) { + return this.stack2.pop(); + } + if(this.stack1.length > 0) { + while(this.stack1.length > 0) { + this.stack2.push(this.stack1.pop()); + } + return this.stack2.pop(); + } + return null; +}; + +/** + * @returns {number} + */ +Queue.prototype.peek = function() { + if(this.stack2.length > 0) { + return this.stack2[this.stack2.length - 1]; + } + if(this.stack1.length > 0) { + return this.stack1[0]; + } + return null; +}; + +/** + * @returns {boolean} + */ +Queue.prototype.empty = function() { + return this.stack1.length === 0 && this.stack2.length === 0; +}; +``` diff --git a/Algorithms/Rectangle Area/README.md b/Algorithms/Rectangle Area/README.md new file mode 100644 index 0000000..00a2999 --- /dev/null +++ b/Algorithms/Rectangle Area/README.md @@ -0,0 +1,34 @@ +#[Rectangle Area](https://leetcode.com/problems/rectangle-area/) +######No:`223` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {number} A + * @param {number} B + * @param {number} C + * @param {number} D + * @param {number} E + * @param {number} F + * @param {number} G + * @param {number} H + * @return {number} + */ +var computeArea = function(A, B, C, D, E, F, G, H) { + var areaA = (C-A) * (D-B); + var areaB = (G-E) * (H-F); + + var left = Math.max(A, E); + var right = Math.min(G, C); + var bottom = Math.max(F, B); + var top = Math.min(D, H); + + var overlap = 0; + + if(right > left && top > bottom) { + overlap = (right - left) * (top - bottom); + } + return areaA + areaB - overlap; +}; +``` diff --git a/Algorithms/Valid Sudoku/README.md b/Algorithms/Valid Sudoku/README.md new file mode 100644 index 0000000..2ea5736 --- /dev/null +++ b/Algorithms/Valid Sudoku/README.md @@ -0,0 +1,43 @@ +#[Valid Sudoku](https://leetcode.com/problems/valid-sudoku/) +######No:`36` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {character[][]} board + * @return {boolean} + */ +var isValidSudoku = function(board) { + var rowExist = {}; + var colExist = {}; + var boxExist = {}; + var k, value; + for (var i = 0; i < board.length; i++) { + for (var j = 0; j < board[i].length; j++) { + value = board[i][j]; + k = parseInt(i / 3, 10) * 3 + parseInt(j / 3, 10); //the number of the box + if (!rowExist[i]) { + rowExist[i] = {}; + } + if (!colExist[j]) { + colExist[j] = {}; + } + if (!boxExist[k]) { + boxExist[k] = {}; + } + + if (value !== '.') { + if (rowExist[i][value] || colExist[j][value] || boxExist[k][value]) { + return false; + } else { + rowExist[i][value] = true; + colExist[j][value] = true; + boxExist[k][value] = true; + } + } + } + } + return true; +}; +``` diff --git a/README.md b/README.md index 82ae9ec..cd20f28 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |24|Swap Nodes in Pairs|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Swap%20Nodes%20in%20Pairs)|Easy| |26|Remove Duplicates from Sorted Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Duplicates%20from%20Sorted%20Array)|Easy| |27|Remove Element|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Element)|Easy| +|36|Valid Sudoku|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Sudoku)|Easy| |46|Permutations|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Permutations)|Medium| |66|Plus One|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Plus%20One)|Easy| |70|Climbing Stairs|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Climbing%20Stairs)|Easy| @@ -47,6 +48,7 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |168|Excel Sheet Column Title|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Excel%20Sheet%20Column%20Title)|Easy| |169|Majority Element|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Majority%20Element)|Easy| |171|Excel Sheet Column Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Excel%20Sheet%20Column%20Number)|Easy| +|172|Factorial Trailing Zeroes|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Factorial%20Trailing%20Zeroes)|Easy| |179|Largest Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Largest%20Number)|Medium| |189|Rotate Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Rotate%20Array)|Easy| |191|Number of 1 Bits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Number%20of%201%20Bits)|Easy| @@ -55,9 +57,12 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |203|Remove Linked List Elements|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Linked%20List%20Elements)|Easy| |206|Reverse Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Linked%20List)|Easy| |217|Contains Duplicate|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Contains%20Duplicate)|Easy| +|219|Contains Duplicate II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Contains%20Duplicate%20II)|Easy| +|223|Rectangle Area|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Rectangle%20Area)|Easy| |226|Invert Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Invert%20Binary%20Tree)|Easy| |228|Summary Ranges|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Summary%20Ranges)|Easy| |231|Power of Two|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Power%20of%20Two)|Easy| +|232|Implement Queue using Stacks|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Implement%20Queue%20using%20Stacks)|Easy| |234|Palindrome Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Palindrome%20Linked%20List)|Easy| |235|Lowest Common Ancestor of a Binary Search Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree)|Easy| |237|Delete Node in a Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Delete%20Node%20in%20a%20Linked%20List)|Easy| From 36b0f3c34cf28e2527d54796ebb016ea526aa8bf Mon Sep 17 00:00:00 2001 From: Teng DU Date: Sun, 29 May 2016 19:22:19 +0800 Subject: [PATCH 047/126] add --- Algorithms/Counting Bits/README.md | 25 ++++++++ .../Intersection of Two Arrays/README.md | 25 ++++++++ .../Product of Array Except Self/README.md | 30 +++++++++ Algorithms/Reverse String/README.md | 14 ++++ Algorithms/Top K Frequent Elements/README.md | 64 +++++++++++++++++++ Algorithms/Valid Parentheses/README.md | 11 ++-- 6 files changed, 163 insertions(+), 6 deletions(-) create mode 100644 Algorithms/Counting Bits/README.md create mode 100644 Algorithms/Intersection of Two Arrays/README.md create mode 100644 Algorithms/Product of Array Except Self/README.md create mode 100644 Algorithms/Reverse String/README.md create mode 100644 Algorithms/Top K Frequent Elements/README.md diff --git a/Algorithms/Counting Bits/README.md b/Algorithms/Counting Bits/README.md new file mode 100644 index 0000000..56afc44 --- /dev/null +++ b/Algorithms/Counting Bits/README.md @@ -0,0 +1,25 @@ +#[Counting Bits](https://leetcode.com/problems/counting-bits/) +######No:`338` +######Difficulty:`Medium` +##JavaScript + +```javascript +var countBits = function(num) { + var ret = [0]; + var j = 1; + for(var i = 1; i <= num; i++) { + if(i == 1) {ret.push(1);continue;} + if(i == Math.pow(2, j+1)) j++; + ret.push(ret[i - Math.pow(2, j)] + 1); + } + return ret; +}; +``` + +####How to solve? +DP: +countBits[0] = 0; +countBits[1] = 1; + +condition: max(j) && n>= Math.pow(2, j) +countBits[n] = countBits[n - Math.pow(2, j)] + 1; diff --git a/Algorithms/Intersection of Two Arrays/README.md b/Algorithms/Intersection of Two Arrays/README.md new file mode 100644 index 0000000..64ecf4f --- /dev/null +++ b/Algorithms/Intersection of Two Arrays/README.md @@ -0,0 +1,25 @@ +#[Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/) +######No:`349` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {number[]} nums1 + * @param {number[]} nums2 + * @return {number[]} + */ + +var intersection = function (nums1, nums2) { + var ret = []; + for (var i = 0; i < nums1.length; i++) { + for (var j = 0; j < nums2.length; j++) { + if (nums1[i] == nums2[j] && ret.indexOf(nums1[i]) === -1) { + ret.push(nums1[i]); + break; + } + } + } + return ret; +} +``` diff --git a/Algorithms/Product of Array Except Self/README.md b/Algorithms/Product of Array Except Self/README.md new file mode 100644 index 0000000..c5e25d8 --- /dev/null +++ b/Algorithms/Product of Array Except Self/README.md @@ -0,0 +1,30 @@ +#[Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/) +######No:`238` +######Difficulty:`Medium` +##JavaScript + +```javascript +/** + * @param {number[]} nums + * @return {number[]} + */ +var productExceptSelf = function(nums) { + var zeroCount = 0; + var products = nums + .filter((num) => { + if(num === 0) zeroCount++; + return num !== 0; + }) + .reduce((prev, curr) => prev * curr, 1); + return nums.map((num) => { + if(zeroCount >= 2) { + return 0; + } + if(zeroCount === 1) { + return num === 0 ? products : 0; + } + return products / num; + + }); +}; +``` diff --git a/Algorithms/Reverse String/README.md b/Algorithms/Reverse String/README.md new file mode 100644 index 0000000..d57cb4b --- /dev/null +++ b/Algorithms/Reverse String/README.md @@ -0,0 +1,14 @@ +#[Reverse String](https://leetcode.com/problems/reverse-string/) +######No:`344` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {string} s + * @return {string} + */ +var reverseString = function(s) { + return s.split('').reverse().join(''); +}; +``` diff --git a/Algorithms/Top K Frequent Elements/README.md b/Algorithms/Top K Frequent Elements/README.md new file mode 100644 index 0000000..47e517b --- /dev/null +++ b/Algorithms/Top K Frequent Elements/README.md @@ -0,0 +1,64 @@ +#[Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) +######No:`347` +######Difficulty:`Medium` +##JavaScript + +```javascript +/** + * @param {number[]} nums + * @param {number} k + * @return {number[]} + */ + +function SortNumber(size) { + this.size = size; + this.container = []; +} +SortNumber.prototype.setCompare = function(compare) { + this.compare = compare; +} +SortNumber.prototype.getMax = function() { + return this.max; +} +SortNumber.prototype.getMin = function() { + return this.min; +} +SortNumber.prototype.getAll = function() { + return this.container; +} +SortNumber.prototype.setBigNumber = function(num) { + if (this.container.length < this.size) { + this.container.push(num); + } else if (this.compare(this.min, num) < 0) { + this.container.shift(); + this.container.push(num); + } else { + return; + } + + this.container.sort(this.compare); + this.max = this.container[this.container.length - 1]; + this.min = this.container[0]; +} + +var topKFrequent = function(nums, k) { + var topNumber = new SortNumber(k); + topNumber.setCompare((a, b) => a.count - b.count); + var showTimes = {}; + nums.map((num) => { + if(showTimes[num]) { + showTimes[num]++; + } else { + showTimes[num] = 1; + } + }); + for(var num in showTimes) { + topNumber.setBigNumber({ + value: num, + count: showTimes[num], + }); + } + + return topNumber.getAll().map((item) => parseInt(item.value)); +}; +``` diff --git a/Algorithms/Valid Parentheses/README.md b/Algorithms/Valid Parentheses/README.md index 91e6816..54a82b4 100644 --- a/Algorithms/Valid Parentheses/README.md +++ b/Algorithms/Valid Parentheses/README.md @@ -11,20 +11,19 @@ var isValid = function(s) { var stack = []; var match = { - '_)': '(', - '_]': '[', - '_}': '{' + ')': '(', + ']': '[', + '}': '{' }; var len; for(var i = 0; i < s.length; i++) { len = stack.length; - if(len > 0 && stack[len-1] == match['_' + s[i]]) { + if(len > 0 && stack[len-1] == match[s[i]]) { stack.pop(); } else { stack.push(s[i]); } } - return stack.length == 0 ? true : false; + return stack.length === 0; }; - ``` From c0326eca9c6935f6e36eeaeed0cb9fa42e999aa7 Mon Sep 17 00:00:00 2001 From: Teng DU Date: Sun, 29 May 2016 20:19:42 +0800 Subject: [PATCH 048/126] add --- Algorithms/Bulb Switcher/README.md | 42 ++++++++++++++++++++++++++++++ README.md | 6 +++++ 2 files changed, 48 insertions(+) create mode 100644 Algorithms/Bulb Switcher/README.md diff --git a/Algorithms/Bulb Switcher/README.md b/Algorithms/Bulb Switcher/README.md new file mode 100644 index 0000000..88c6231 --- /dev/null +++ b/Algorithms/Bulb Switcher/README.md @@ -0,0 +1,42 @@ +#[Add Digits](https://leetcode.com/problems/add-digits/) +######No:`319` +######Difficulty:`Medium` +##JavaScript + +####Solution 1: +refer: https://leetcode.com/discuss/91371/share-my-o-1-solution-with-explanation +```js +/** + * @param {number} n + * @return {number} + */ +var bulbSwitch = function(n) { + return parseInt(Math.sqrt(n)); +}; +``` + + +####Solution 2: +#### Time Limit Exceeded. Input: 9999999 +```js +/** + * @param {number} n + * @return {number} + */ +var bulbSwitch = function(n) { + var bulbs = new Array(n).fill(0); + for(i = 1; i <= n; i++) { + for(var j = i; j <= n; j = j + i) { + bulbs[j-1] = 1 - bulbs[j-1]; + } + } + var sum = 0; + + for(var i = 0; i < bulbs.length; i++) { + if(bulbs[i] == 1) { + sum++; + } + } + return sum; +}; +``` diff --git a/README.md b/README.md index cd20f28..be64e1d 100644 --- a/README.md +++ b/README.md @@ -66,11 +66,17 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |234|Palindrome Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Palindrome%20Linked%20List)|Easy| |235|Lowest Common Ancestor of a Binary Search Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree)|Easy| |237|Delete Node in a Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Delete%20Node%20in%20a%20Linked%20List)|Easy| +|238|Product of Array Except Self|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Product%20of%20Array%20Except%20Self)|Medium| |242|Valid Anagram|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Anagram)|Easy| |257|Binary Tree Paths|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Binary%20Tree%20Paths)|Easy| |258|Add Digits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Add%20Digits)|Easy| |263|Ugly Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Ugly%20Number)|Easy| |283|Move Zeroes|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Move%20Zeroes)|Easy| |292|Nim Game|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Nim%20Game)|Easy| +|319|Add Digits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Add%20Digits)|Medium| |326|Power of Three|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Power%20of%20Three)|Easy| |328|Odd Even Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Odd%20Even%20Linked%20List)|Medium| +|338|Counting Bits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Counting%20Bits)|Medium| +|344|Reverse String|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20String)|Easy| +|347|Top K Frequent Elements|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Top%20K%20Frequent%20Elements)|Medium| +|349|Intersection of Two Arrays|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Intersection%20of%20Two%20Arrays)|Easy| From 3dd337e561f84b7c3749aa92452a1f9589014784 Mon Sep 17 00:00:00 2001 From: Teng DU Date: Sat, 4 Jun 2016 15:33:17 +0800 Subject: [PATCH 049/126] add --- .../Binary Tree Preorder Traversal/README.md | 34 +++++++++++++++++++ Algorithms/Integer Break/README.md | 32 +++++++++++++++++ Algorithms/Missing Number/README.md | 14 ++++++++ 3 files changed, 80 insertions(+) create mode 100644 Algorithms/Binary Tree Preorder Traversal/README.md create mode 100644 Algorithms/Integer Break/README.md create mode 100644 Algorithms/Missing Number/README.md diff --git a/Algorithms/Binary Tree Preorder Traversal/README.md b/Algorithms/Binary Tree Preorder Traversal/README.md new file mode 100644 index 0000000..24b6127 --- /dev/null +++ b/Algorithms/Binary Tree Preorder Traversal/README.md @@ -0,0 +1,34 @@ +#[Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/) +######No:`144` +######Difficulty:`Medium` +##JavaScript + +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number[]} + */ + +var preorderTraversal = function(root) { + if(root === null) return []; + var ret = []; + + function pre(root) { + if(root) { + ret.push(root.val); + pre(root.left); + pre(root.right); + } + } + + pre(root); + return ret; +}; +``` diff --git a/Algorithms/Integer Break/README.md b/Algorithms/Integer Break/README.md new file mode 100644 index 0000000..53c75cd --- /dev/null +++ b/Algorithms/Integer Break/README.md @@ -0,0 +1,32 @@ +#[Integer Break](https://leetcode.com/problems/integer-break/) +######No:`343` +######Difficulty:`Medium` +##JavaScript + +```javascript +/** + * @param {number} n + * @return {number} + */ +var integerBreak = function(n) { + var maxArr = { + 1: 1, + 2: 1, + }; + var maxValue = null; + var value = null; + + for(var i = 3; i <= n; i++) { + maxValue = Number.MIN_SAFE_INTEGER; + for(var j = 1; j <= i / 2; j++) { + value = Math.max(j, maxArr[j]) * Math.max(i - j, maxArr[i - j]); + if(value > maxValue) { + maxValue = value; + } + } + maxArr[i] = maxValue; + } + + return maxArr[n]; +}; +``` diff --git a/Algorithms/Missing Number/README.md b/Algorithms/Missing Number/README.md new file mode 100644 index 0000000..2ac46eb --- /dev/null +++ b/Algorithms/Missing Number/README.md @@ -0,0 +1,14 @@ +#[Missing Number](https://leetcode.com/problems/missing-number/) +######No:`268` +######Difficulty:`Medium` +##JavaScript + +```javascript +/** + * @param {number[]} nums + * @return {number} + */ +var missingNumber = function(nums) { + return (0 + nums.length) * (nums.length + 1) / 2 - nums.reduce((p, c) => p + c, 0); +}; +``` From 0502e5097cb94f0e224a30f5e2f5c01fbb03efcf Mon Sep 17 00:00:00 2001 From: Teng DU Date: Mon, 19 Dec 2016 01:24:50 +0800 Subject: [PATCH 050/126] add --- Algorithms/Add Strings/README.md | 47 +++++++++++++++++++ Algorithms/Assign Cookies/README.md | 33 +++++++++++++ Algorithms/Binary Watch/README.md | 36 ++++++++++++++ Algorithms/Find the Difference/README.md | 18 +++++++ .../README.md | 25 ++++++++++ Algorithms/Fizz Buzz/README.md | 28 +++++++++++ .../Intersection of Two Arrays II/README.md | 27 +++++++++++ Algorithms/Island Perimeter/README.md | 29 ++++++++++++ Algorithms/Longest Palindrome/README.md | 33 +++++++++++++ .../README.md | 20 ++++++++ Algorithms/Number of Boomerangs/README.md | 43 +++++++++++++++++ Algorithms/Ransom Note/README.md | 27 +++++++++++ Algorithms/Sum of Left Leaves/README.md | 27 +++++++++++ README.md | 16 +++++++ 14 files changed, 409 insertions(+) create mode 100644 Algorithms/Add Strings/README.md create mode 100644 Algorithms/Assign Cookies/README.md create mode 100644 Algorithms/Binary Watch/README.md create mode 100644 Algorithms/Find the Difference/README.md create mode 100644 Algorithms/First Unique Character in a String/README.md create mode 100644 Algorithms/Fizz Buzz/README.md create mode 100644 Algorithms/Intersection of Two Arrays II/README.md create mode 100644 Algorithms/Island Perimeter/README.md create mode 100644 Algorithms/Longest Palindrome/README.md create mode 100644 Algorithms/Minimum Moves to Equal Array Elements/README.md create mode 100644 Algorithms/Number of Boomerangs/README.md create mode 100644 Algorithms/Ransom Note/README.md create mode 100644 Algorithms/Sum of Left Leaves/README.md diff --git a/Algorithms/Add Strings/README.md b/Algorithms/Add Strings/README.md new file mode 100644 index 0000000..b8bd183 --- /dev/null +++ b/Algorithms/Add Strings/README.md @@ -0,0 +1,47 @@ +#[Add Strings](https://leetcode.com/problems/add-strings/) +######No:`415` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {string} num1 + * @param {string} num2 + * @return {string} + */ +var addStrings = function(num1, num2) { + var zeroString = Array(Math.abs(num1.length - num2.length) + 1).join(0); + if (num1.length > num2.length) { + num2 = zeroString + num2; + } else { + num1 = zeroString + num1; + } + + var d1 = num1.split(''); + var d2 = num2.split(''); + var ret = []; + var sum ; + var hasCarryOver = false; + + for(var i = d1.length - 1; i >= 0 ; i--) { + sum = parseInt(d1[i]) + parseInt(d2[i]); + if (hasCarryOver) { + sum++; + } + if(sum >= 10) { + sum -= 10; + hasCarryOver = true; + } else { + hasCarryOver = false; + } + ret.unshift(sum); + } + + if(hasCarryOver) { + ret.unshift(1); + } + + return ret.join(''); +}; + +``` diff --git a/Algorithms/Assign Cookies/README.md b/Algorithms/Assign Cookies/README.md new file mode 100644 index 0000000..000c061 --- /dev/null +++ b/Algorithms/Assign Cookies/README.md @@ -0,0 +1,33 @@ +#[Assign Cookies](https://leetcode.com/problems/assign-cookies/) +######No:`455` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {number[]} g + * @param {number[]} s + * @return {number} + */ +var findContentChildren = function(g, s) { + var compare = (a, b) => a - b; + g = g.sort(compare); + s = s.sort(compare); + + var i = s.length - 1; // person index + var j = g.length - 1; // cookie index + var count = 0; + + while (i >= 0 && j >= 0) { + if(s[i] >= g[j]) { + i--; + j--; + count++ + } else { + j--; + } + } + + return count; +}; +``` diff --git a/Algorithms/Binary Watch/README.md b/Algorithms/Binary Watch/README.md new file mode 100644 index 0000000..58a1801 --- /dev/null +++ b/Algorithms/Binary Watch/README.md @@ -0,0 +1,36 @@ +#[Binary Watch](https://leetcode.com/problems/binary-watch/) +######No:`401` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {number} num + * @return {string[]} + */ + +var bitcount1 = function (num) { + return (num).toString(2).split('').filter(i => i === '1').length; +} +var formatTime = function(h, m) { + if (m < 10) { + return `${h}:0${m}` + } else { + return `${h}:${m}` + } +} + +var readBinaryWatch = function(num) { + var ret = []; + + for (var h = 0; h < 12; h++) { + for (var m = 0; m < 60; m++) { + if(bitcount1(h << 6 | m) === num) { + ret.push(formatTime(h, m)); + } + } + } + + return ret; +}; +``` diff --git a/Algorithms/Find the Difference/README.md b/Algorithms/Find the Difference/README.md new file mode 100644 index 0000000..8dc35b9 --- /dev/null +++ b/Algorithms/Find the Difference/README.md @@ -0,0 +1,18 @@ +#[Find the Difference](https://leetcode.com/problems/find-the-difference/) +######No:`389` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {string} s + * @param {string} t + * @return {character} + */ +var findTheDifference = function(s, t) { + if(!s) return t; + var sSum = s.split('').map(i => i.charCodeAt(0)).reduce((p, c) => p + c); + var tSum = t.split('').map(i => i.charCodeAt(0)).reduce((p, c) => p + c); + return String.fromCharCode(tSum - sSum); +}; +``` diff --git a/Algorithms/First Unique Character in a String/README.md b/Algorithms/First Unique Character in a String/README.md new file mode 100644 index 0000000..42787c9 --- /dev/null +++ b/Algorithms/First Unique Character in a String/README.md @@ -0,0 +1,25 @@ +#[First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) +######No:`387` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {string} s + * @return {number} + */ +var firstUniqChar = function(s) { + var map = {}; + + s.split('').forEach(i => map[i] === undefined ? map[i] = 1 : map[i]++); + + for(var i = 0; i < s.length; i++) { + if(map[s[i]] === 1) { + return i; + } + } + + return -1; + +}; +``` diff --git a/Algorithms/Fizz Buzz/README.md b/Algorithms/Fizz Buzz/README.md new file mode 100644 index 0000000..d155559 --- /dev/null +++ b/Algorithms/Fizz Buzz/README.md @@ -0,0 +1,28 @@ +#[Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) +######No:`412` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {number} n + * @return {string[]} + */ +var fizzBuzz = function(n) { + var ret = []; + + for (var i = 1; i <= n; i++) { + if (i % 15 === 0) { + ret.push('FizzBuzz'); + } else if (i % 3 === 0) { + ret.push('Fizz'); + } else if (i % 5 === 0) { + ret.push('Buzz'); + } else { + ret.push(i + ''); + } + } + + return ret; +}; +``` diff --git a/Algorithms/Intersection of Two Arrays II/README.md b/Algorithms/Intersection of Two Arrays II/README.md new file mode 100644 index 0000000..ee184ce --- /dev/null +++ b/Algorithms/Intersection of Two Arrays II/README.md @@ -0,0 +1,27 @@ +#[Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) +######No:`350` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {number[]} nums1 + * @param {number[]} nums2 + * @return {number[]} + */ +var intersect = function(nums1, nums2) { + var map = {}; + var ret = []; + + nums1.forEach(i => map[i] === undefined ? map[i] = 1 : map[i]++); + + nums2.forEach(i => { + if(map[i] !== undefined && map[i] > 0) { + ret.push(i); + map[i]--; + } + }); + + return ret; +}; +``` diff --git a/Algorithms/Island Perimeter/README.md b/Algorithms/Island Perimeter/README.md new file mode 100644 index 0000000..9934e0a --- /dev/null +++ b/Algorithms/Island Perimeter/README.md @@ -0,0 +1,29 @@ +#[Add Digits](https://leetcode.com/problems/island-perimeter/) +######No:`463` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {number[][]} grid + * @return {number} + */ +var islandPerimeter = function(grid) { + var perimeter = 4 * grid.reduce((p, c) => p.concat(c)).reduce((p, c) => p + c); + + for(var i = 0; i < grid.length; i++) { + for(var j = 0; j < grid[i].length; j++) { + if(grid[i][j] === 1) { + if(i + 1 < grid.length && grid[i+1][j] === 1) { + perimeter -= 2 ; + } + if(j + 1 < grid[i].length && grid[i][j+1] === 1) { + perimeter -= 2; + } + } + } + } + + return perimeter; +}; +``` diff --git a/Algorithms/Longest Palindrome/README.md b/Algorithms/Longest Palindrome/README.md new file mode 100644 index 0000000..2f6b9a0 --- /dev/null +++ b/Algorithms/Longest Palindrome/README.md @@ -0,0 +1,33 @@ +#[Longest Palindrome](https://leetcode.com/problems/longest-palindrome/) +######No:`409` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {string} s + * @return {number} + */ +var longestPalindrome = function(s) { + var map = {}; + var number = 0; + var hasOdd = false; + + s.split('').forEach(i => map[i] === undefined ? map[i] = 1 : map[i]++); + + for(var i in map) { + if(map[i] % 2 === 0) { + number += map[i]; + } else if(map[i] > 2) { + number += map[i] - 1; + hasOdd = true; + } else { + hasOdd = true; + } + } + + if(hasOdd) number++; + + return number; +}; +``` diff --git a/Algorithms/Minimum Moves to Equal Array Elements/README.md b/Algorithms/Minimum Moves to Equal Array Elements/README.md new file mode 100644 index 0000000..fe7aa4e --- /dev/null +++ b/Algorithms/Minimum Moves to Equal Array Elements/README.md @@ -0,0 +1,20 @@ +#[Minimum Moves to Equal Array Elements](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/) +######No:`453` +######Difficulty:`Easy` +##JavaScript + +### Solution: +Increasing n-i elements by 1 is same as decrease 1 elements by 1. +Get the minimum number and calculate the sum of other elements minus minimum. + +```javascript +/** + * @param {number[]} nums + * @return {number} + */ +var minMoves = function(nums) { + var min = Math.min.apply(null, nums) + + return nums.reduce((p, c) => p + c - min, 0); +}; +``` diff --git a/Algorithms/Number of Boomerangs/README.md b/Algorithms/Number of Boomerangs/README.md new file mode 100644 index 0000000..795ad25 --- /dev/null +++ b/Algorithms/Number of Boomerangs/README.md @@ -0,0 +1,43 @@ +#[Number of Boomerangs](https://leetcode.com/problems/number-of-boomerangs/) +######No:`447` +######Difficulty:`Easy` +##JavaScript + +### Solution: +Get each 2 points' distance. For one point, save the distance as the key and count as the value into a map. The Boomerangs which contains this point will be count * (count - 1). + +```javascript +/** + * @param {number[][]} points + * @return {number} + */ + +var getDistance = function(a, b) { + var dx = a[0] - b[0]; + var dy = a[1] - b[1]; + + return dx * dx + dy * dy; +} + +var numberOfBoomerangs = function(points) { + var map; + var distance; + var ret = 0; + + for(var i = 0; i < points.length; i++) { + map = {}; + for(var j = 0; j < points.length; j++) { + if(i !== j) { + distance = getDistance(points[i], points[j]); + map[distance] === undefined ? map[distance] = 1 : map[distance] += 1; + } + } + + for(var d in map) { + ret += map[d] * (map[d] - 1); + } + } + + return ret; +}; +``` diff --git a/Algorithms/Ransom Note/README.md b/Algorithms/Ransom Note/README.md new file mode 100644 index 0000000..975341d --- /dev/null +++ b/Algorithms/Ransom Note/README.md @@ -0,0 +1,27 @@ +#[Ransom Note](https://leetcode.com/problems/ransom-note/) +######No:`383` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {string} ransomNote + * @param {string} magazine + * @return {boolean} + */ +var canConstruct = function(ransomNote, magazine) { + var map = {}; + var flag = true; + + magazine.split('').forEach(i => map[i] === undefined ? map[i] = 1 : map[i]++); + ransomNote.split('').forEach(i => { + if(map[i] === undefined || map[i] === 0) { + flag = false; + } else { + map[i]--; + } + }); + + return flag; +}; +``` diff --git a/Algorithms/Sum of Left Leaves/README.md b/Algorithms/Sum of Left Leaves/README.md new file mode 100644 index 0000000..734d18f --- /dev/null +++ b/Algorithms/Sum of Left Leaves/README.md @@ -0,0 +1,27 @@ +#[Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) +######No:`404` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number} + */ +var sumOfLeftLeaves = function(root) { + if(root === null) return 0; + + if(root.left !== null && root.left.left === null && root.left.right === null) { + return root.left.val + sumOfLeftLeaves(root.right); + } else { + return sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right); + } +}; +``` diff --git a/README.md b/README.md index be64e1d..7964dad 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |122|Best Time to Buy and Sell Stock II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Best%20Time%20to%20Buy%20and%20Sell%20Stock%20II)|Medium| |125|Valid Palindrome|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Palindrome)|Easy| |136|Single Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Single%20Number)|Medium| +|144|Binary Tree Preorder Traversal|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Binary%20Tree%20Preorder%20Traversal)|Medium| |148|Sort List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Sort%20List)|Medium| |149|Max Points on a Line|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Max%20Points%20on%20a%20Line)|Hard| |150|Evaluate Reverse Polish Notation|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Evaluate%20Reverse%20Polish%20Notation)|Medium| @@ -71,12 +72,27 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |257|Binary Tree Paths|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Binary%20Tree%20Paths)|Easy| |258|Add Digits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Add%20Digits)|Easy| |263|Ugly Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Ugly%20Number)|Easy| +|268|Missing Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Missing%20Number)|Medium| |283|Move Zeroes|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Move%20Zeroes)|Easy| |292|Nim Game|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Nim%20Game)|Easy| |319|Add Digits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Add%20Digits)|Medium| |326|Power of Three|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Power%20of%20Three)|Easy| |328|Odd Even Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Odd%20Even%20Linked%20List)|Medium| |338|Counting Bits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Counting%20Bits)|Medium| +|343|Integer Break|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Integer%20Break)|Medium| |344|Reverse String|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20String)|Easy| |347|Top K Frequent Elements|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Top%20K%20Frequent%20Elements)|Medium| |349|Intersection of Two Arrays|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Intersection%20of%20Two%20Arrays)|Easy| +|350|Intersection of Two Arrays II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Intersection%20of%20Two%20Arrays%20II)|Easy| +|383|Ransom Note|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Ransom%20Note)|Easy| +|387|First Unique Character in a String|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/First%20Unique%20Character%20in%20a%20String)|Easy| +|389|Find the Difference|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Find%20the%20Difference)|Easy| +|401|Binary Watch|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Binary%20Watch)|Easy| +|404|Sum of Left Leaves|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Sum%20of%20Left%20Leaves)|Easy| +|409|Longest Palindrome|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Longest%20Palindrome)|Easy| +|412|Fizz Buzz|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Fizz%20Buzz)|Easy| +|415|Add Strings|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Add%20Strings)|Easy| +|447|Number of Boomerangs|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Number%20of%20Boomerangs)|Easy| +|453|Minimum Moves to Equal Array Elements|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Minimum%20Moves%20to%20Equal%20Array%20Elements)|Easy| +|455|Assign Cookies|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Assign%20Cookies)|Easy| +|463|Add Digits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Add%20Digits)|Easy| From 47886ab291184a68534c6553f3eae514ec4e17b1 Mon Sep 17 00:00:00 2001 From: Teng DU Date: Mon, 20 Mar 2017 23:58:12 +0800 Subject: [PATCH 051/126] add --- Algorithms/3Sum Closest/README.md | 44 +++++++++ Algorithms/3Sum/README.md | 96 +++++++++++++++++++ Algorithms/Generate Parentheses/README.md | 32 +++++++ Algorithms/Implement strStr()/README.md | 25 +++++ Algorithms/Integer to Roman/README.md | 24 +++++ .../README.md | 41 ++++++++ 6 files changed, 262 insertions(+) create mode 100644 Algorithms/3Sum Closest/README.md create mode 100644 Algorithms/3Sum/README.md create mode 100644 Algorithms/Generate Parentheses/README.md create mode 100644 Algorithms/Implement strStr()/README.md create mode 100644 Algorithms/Integer to Roman/README.md create mode 100644 Algorithms/Letter Combinations of a Phone Number/README.md diff --git a/Algorithms/3Sum Closest/README.md b/Algorithms/3Sum Closest/README.md new file mode 100644 index 0000000..ca36223 --- /dev/null +++ b/Algorithms/3Sum Closest/README.md @@ -0,0 +1,44 @@ +#[3Sum Closest](https://leetcode.com/problems/3sum-closest/) +######No:`16` +######Difficulty:`Medium` +##JavaScript + +```javascript +/** + * @param {number[]} nums + * @param {number} target + * @return {number} + */ +var threeSumClosest = function(nums, target) { + var closet = Number.MAX_SAFE_INTEGER; + var closetTarget; + + nums = nums.sort((a, b) => a - b); + + for (var i = 0; i + 2 < nums.length; i++) { + if (i > 0 && nums[i] == nums[i - 1]) { + continue; + } + + var j = i + 1, k = nums.length - 1; + + while (j < k) { + if (Math.abs(nums[j] + nums[k] + nums[i] - target) < closet) { + closet = Math.abs(nums[j] + nums[k] + nums[i] - target); + closetTarget = nums[j] + nums[k] + nums[i]; + } + + if (nums[j] + nums[k] + nums[i] === target) { + return target; + } else if (nums[j] + nums[k] + nums[i] > target) { + k--; + } else { + j++; + } + } + } + + return closetTarget; +}; + +``` diff --git a/Algorithms/3Sum/README.md b/Algorithms/3Sum/README.md new file mode 100644 index 0000000..f366143 --- /dev/null +++ b/Algorithms/3Sum/README.md @@ -0,0 +1,96 @@ +#[3SumAdd Digits](https://leetcode.com/problems/3sum/) +######No:`15` +######Difficulty:`Medium` +##JavaScript + +### Solution 1 +Result: `timeout` +How: Use the data structure SumNode. Make sure the key is unique. +```javascript +/** + * @param {number[]} nums + * @return {number[][]} + */ +var SumNode = function(nums, i, j) { + this.i = i; + this.j = j; + this.sum = nums[i] + nums[j]; + + var max = Math.max(nums[i], nums[j], -this.sum); + var min = Math.min(nums[i], nums[j], -this.sum); + var mid = 0 - max - min; + this.key = [max, mid, min].join('_'); + this.isUsed = false; + +} + +var threeSum = function (nums) { + var ret = []; + var sumNodes = {}; + var node = null; + + for (var i = 0; i < nums.length; i++) { + for (var j = i + 1; j < nums.length; j++) { + node = new SumNode(nums, i, j); + sumNodes[node.key] = node; + } + } + + for (var key in sumNodes) { + node = sumNodes[key]; + for(var k = 0; k < nums.length; k++) { + if (k !== node.i && + k !== node.j && + node.sum + nums[k] === 0 && + !node.isUsed + ) { + delete sumNodes[key]; + node.isUsed = true; + ret.push([nums[k], nums[node.i], nums[node.j]]); + } + } + } + + return ret; +}; + +``` + +### Solution 2 +Result: timeout +```javascript +/** + * @param {number[]} nums + * @return {number[][]} + */ + +var threeSum = function(nums) { + var ret = []; + + nums = nums.sort((a, b) => a - b); + for (var i = 0; i + 2 < nums.length; i++) { + if (i > 0 && nums[i] == nums[i - 1]) { + continue; + } + + var j = i + 1, k = nums.length - 1; + var target = -nums[i]; + + while (j < k) { + if (nums[j] + nums[k] == target) { + ret.push([nums[i], nums[j], nums[k]]); + j++; + k--; + while (j < k && nums[j] == nums[j - 1]) j++; + while (j < k && nums[k] == nums[k + 1]) k--; + } else if (nums[j] + nums[k] > target) { + k--; + } else { + j++; + } + } + } + + return ret; +}; +``` diff --git a/Algorithms/Generate Parentheses/README.md b/Algorithms/Generate Parentheses/README.md new file mode 100644 index 0000000..c1c0b0d --- /dev/null +++ b/Algorithms/Generate Parentheses/README.md @@ -0,0 +1,32 @@ +#[Generate Parentheses](https://leetcode.com/problems/generate-parentheses/) +######No:`22` +######Difficulty:`Medium` +##JavaScript + +```javascript +/** + * @param {number} n + * @return {string[]} + */ +var generateParenthesis = function(n) { + if(n === 0) { + return []; + } else if(n === 1){ + return ['()']; + } else { + var parenthesis = generateParenthesis(n - 1); + var retObj = {}; + + parenthesis.forEach(p => { + for(var i = 0; i < p.length + 1; i++) { + var charArr = p.split(''); + + charArr.splice(i, 0, '()'); + retObj[charArr.join('')] = true; + } + }); + + return Object.keys(retObj); + } +}; +``` diff --git a/Algorithms/Implement strStr()/README.md b/Algorithms/Implement strStr()/README.md new file mode 100644 index 0000000..3ecab39 --- /dev/null +++ b/Algorithms/Implement strStr()/README.md @@ -0,0 +1,25 @@ +#[Implement strStr()](https://leetcode.com/problems/implement-strstr) +######No:`28` +######Difficulty:`Easy` +##JavaScript + +```javascript +/** + * @param {string} haystack + * @param {string} needle + * @return {number} + */ + +var strStr = function(haystack, needle) { + if (needle === '') return 0; + for(var i = 0;; i++) { + for(var j = 0;; j++) { + if (j == needle.length) return i; + if (i + j >= haystack.length) return -1; + if (haystack[i + j] != needle[j]) break; + } + } + return -1; +}; + +``` diff --git a/Algorithms/Integer to Roman/README.md b/Algorithms/Integer to Roman/README.md new file mode 100644 index 0000000..5b04555 --- /dev/null +++ b/Algorithms/Integer to Roman/README.md @@ -0,0 +1,24 @@ +#[Integer to Roman](https://leetcode.com/problems/integer-to-roman/) +######No:`12` +######Difficulty:`Medium` +##JavaScript + +```javascript +/** + * @param {number} num + * @return {string} + */ +var intToRoman = function(num) { + var M = ["", "M", "MM", "MMM"]; + var C = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"]; + var X = ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"]; + var I = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"]; + + return [ + M[parseInt(num / 1000)], + C[parseInt((num % 1000) / 100)], + X[parseInt((num % 100) / 10)], + I[num % 10] + ].join(''); +}; +``` diff --git a/Algorithms/Letter Combinations of a Phone Number/README.md b/Algorithms/Letter Combinations of a Phone Number/README.md new file mode 100644 index 0000000..a82d71a --- /dev/null +++ b/Algorithms/Letter Combinations of a Phone Number/README.md @@ -0,0 +1,41 @@ +#[Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/) +######No:`17` +######Difficulty:`Medium` +##JavaScript + +```javascript +/** + * @param {string} digits + * @return {string[]} + */ +var letterCombinations = function(digits) { + var map = { + 2: ['a', 'b', 'c'], + 3: ['d', 'e', 'f'], + 4: ['g', 'h', 'i'], + 5: ['j', 'k', 'l'], + 6: ['m', 'n', 'o'], + 7: ['p', 'q', 'r', 's'], + 8: ['t', 'u', 'v'], + 9: ['w', 'x', 'y', 'z'], + }; + + if(digits.length === 0) { + return []; + } else if(digits.length === 1) { + return map[digits]; + } else { + var front = map[digits.slice(0, 1)]; + var back = letterCombinations(digits.slice(1)); + var ret = []; + + front.forEach(f => { + back.forEach(b => { + ret.push(f + b); + }); + }); + + return ret; + } +}; +``` From 474e31a987ad8af66a683ab3f263b0797cd6e398 Mon Sep 17 00:00:00 2001 From: Teng DU Date: Tue, 21 Mar 2017 01:02:58 +0800 Subject: [PATCH 052/126] update --- Algorithms/3Sum Closest/README.md | 8 ++++---- Algorithms/3Sum/README.md | 12 ++++++------ Algorithms/Add Digits/README.md | 12 ++++++------ Algorithms/Add Strings/README.md | 8 ++++---- Algorithms/Assign Cookies/README.md | 8 ++++---- Algorithms/Balanced Binary Tree/README.md | 10 +++++----- .../Best Time to Buy and Sell Stock II/README.md | 8 ++++---- .../README.md | 8 ++++---- .../Binary Tree Level Order Traversal/README.md | 8 ++++---- Algorithms/Binary Tree Paths/README.md | 8 ++++---- .../Binary Tree Preorder Traversal/README.md | 8 ++++---- Algorithms/Binary Watch/README.md | 8 ++++---- Algorithms/Bulb Switcher/README.md | 16 ++++++++-------- Algorithms/Climbing Stairs/README.md | 8 ++++---- Algorithms/Compare Version Numbers/README.md | 8 ++++---- Algorithms/Contains Duplicate II/README.md | 12 ++++++------ Algorithms/Contains Duplicate/README.md | 8 ++++---- Algorithms/Counting Bits/README.md | 10 +++++----- .../Delete Node in a Linked List/README.md | 8 ++++---- .../Evaluate Reverse Polish Notation/README.md | 8 ++++---- Algorithms/Excel Sheet Column Number/README.md | 8 ++++---- Algorithms/Excel Sheet Column Title/README.md | 8 ++++---- Algorithms/Factorial Trailing Zeroes/README.md | 10 +++++----- Algorithms/Find the Difference/README.md | 8 ++++---- .../First Unique Character in a String/README.md | 8 ++++---- Algorithms/Fizz Buzz/README.md | 8 ++++---- Algorithms/Generate Parentheses/README.md | 8 ++++---- Algorithms/Happy Number/README.md | 8 ++++---- Algorithms/House Robber/README.md | 12 ++++++------ .../Implement Queue using Stacks/README.md | 8 ++++---- Algorithms/Implement strStr()/README.md | 8 ++++---- Algorithms/Integer Break/README.md | 8 ++++---- Algorithms/Integer to Roman/README.md | 8 ++++---- .../Intersection of Two Arrays II/README.md | 8 ++++---- Algorithms/Intersection of Two Arrays/README.md | 8 ++++---- .../Intersection of Two Linked Lists/README.md | 8 ++++---- Algorithms/Invert Binary Tree/README.md | 8 ++++---- Algorithms/Island Perimeter/README.md | 8 ++++---- Algorithms/Largest Number/README.md | 8 ++++---- .../README.md | 8 ++++---- Algorithms/Longest Palindrome/README.md | 8 ++++---- .../README.md | 8 ++++---- Algorithms/Majority Element/README.md | 8 ++++---- Algorithms/Max Points on a Line/README.md | 8 ++++---- .../Maximum Depth of Binary Tree/README.md | 8 ++++---- Algorithms/Merge Sorted Array/README.md | 8 ++++---- Algorithms/Merge Two Sorted Lists/README.md | 8 ++++---- Algorithms/Min Stack/README.md | 8 ++++---- .../Minimum Depth of Binary Tree/README.md | 8 ++++---- .../README.md | 8 ++++---- Algorithms/Missing Number/README.md | 8 ++++---- Algorithms/Move Zeroes/README.md | 8 ++++---- Algorithms/Nim Game/README.md | 8 ++++---- Algorithms/Number of 1 Bits/README.md | 8 ++++---- Algorithms/Number of Boomerangs/README.md | 8 ++++---- Algorithms/Odd Even Linked List/README.md | 8 ++++---- Algorithms/Palindrome Linked List/README.md | 8 ++++---- Algorithms/Palindrome Number/README.md | 8 ++++---- Algorithms/Pascal's Triangle II/README.md | 8 ++++---- Algorithms/Pascal's Triangle/README.md | 8 ++++---- Algorithms/Path Sum/README.md | 8 ++++---- Algorithms/Permutations/README.md | 8 ++++---- Algorithms/Plus One/README.md | 8 ++++---- Algorithms/Power of Three/README.md | 8 ++++---- Algorithms/Power of Two/README.md | 8 ++++---- .../Product of Array Except Self/README.md | 8 ++++---- Algorithms/Ransom Note/README.md | 8 ++++---- Algorithms/Rectangle Area/README.md | 8 ++++---- .../README.md | 8 ++++---- .../Remove Duplicates from Sorted List/README.md | 8 ++++---- Algorithms/Remove Element/README.md | 8 ++++---- Algorithms/Remove Linked List Elements/README.md | 8 ++++---- .../Remove Nth Node From End of List/README.md | 8 ++++---- Algorithms/Reverse Integer/README.md | 10 +++++----- Algorithms/Reverse Linked List II/README.md | 8 ++++---- Algorithms/Reverse Linked List/README.md | 8 ++++---- Algorithms/Reverse String/README.md | 8 ++++---- Algorithms/Reverse Words in a String/README.md | 8 ++++---- Algorithms/Roman to Integer/README.md | 8 ++++---- Algorithms/Rotate Array/README.md | 8 ++++---- Algorithms/Same Tree/README.md | 8 ++++---- Algorithms/Set Matrix Zeroes/README.md | 8 ++++---- Algorithms/Single Number/README.md | 10 +++++----- Algorithms/Sort List/README.md | 8 ++++---- Algorithms/String to Integer (atoi)/README.md | 8 ++++---- Algorithms/Sum of Left Leaves/README.md | 8 ++++---- Algorithms/Summary Ranges/README.md | 8 ++++---- Algorithms/Swap Nodes in Pairs/README.md | 8 ++++---- Algorithms/Symmetric Tree/README.md | 8 ++++---- Algorithms/Top K Frequent Elements/README.md | 8 ++++---- Algorithms/Two Sum/README.md | 8 ++++---- Algorithms/Ugly Number/README.md | 8 ++++---- Algorithms/Valid Anagram/README.md | 8 ++++---- Algorithms/Valid Palindrome/README.md | 8 ++++---- Algorithms/Valid Parentheses/README.md | 8 ++++---- Algorithms/Valid Sudoku/README.md | 8 ++++---- Algorithms/ZigZag Conversion/README.md | 14 +++++++------- 97 files changed, 408 insertions(+), 408 deletions(-) diff --git a/Algorithms/3Sum Closest/README.md b/Algorithms/3Sum Closest/README.md index ca36223..4de9cb6 100644 --- a/Algorithms/3Sum Closest/README.md +++ b/Algorithms/3Sum Closest/README.md @@ -1,7 +1,7 @@ -#[3Sum Closest](https://leetcode.com/problems/3sum-closest/) -######No:`16` -######Difficulty:`Medium` -##JavaScript +# [3Sum Closest](https://leetcode.com/problems/3sum-closest/) +###### No:`16` +###### Difficulty:`Medium` +## JavaScript ```javascript /** diff --git a/Algorithms/3Sum/README.md b/Algorithms/3Sum/README.md index f366143..307dde7 100644 --- a/Algorithms/3Sum/README.md +++ b/Algorithms/3Sum/README.md @@ -1,9 +1,9 @@ -#[3SumAdd Digits](https://leetcode.com/problems/3sum/) -######No:`15` -######Difficulty:`Medium` -##JavaScript +# [3SumAdd Digits](https://leetcode.com/problems/3sum/) +###### No:`15` +###### Difficulty:`Medium` +## JavaScript -### Solution 1 +### Solution 1 Result: `timeout` How: Use the data structure SumNode. Make sure the key is unique. ```javascript @@ -56,7 +56,7 @@ var threeSum = function (nums) { ``` -### Solution 2 +### Solution 2 Result: timeout ```javascript /** diff --git a/Algorithms/Add Digits/README.md b/Algorithms/Add Digits/README.md index e8ef4df..9a05afb 100644 --- a/Algorithms/Add Digits/README.md +++ b/Algorithms/Add Digits/README.md @@ -1,9 +1,9 @@ -#[Add Digits](https://leetcode.com/problems/add-digits/) -######No:`258` -######Difficulty:`Easy` -##JavaScript +# [Add Digits](https://leetcode.com/problems/add-digits/) +###### No:`258` +###### Difficulty:`Easy` +## JavaScript -####Solution 1: +#### Solution 1: ```javascript /** * @param {number} num @@ -23,7 +23,7 @@ var addDigits = function(num) { ``` -####Solution 2: +#### Solution 2: ```javascript /** * @param {number} num diff --git a/Algorithms/Add Strings/README.md b/Algorithms/Add Strings/README.md index b8bd183..5ac91c9 100644 --- a/Algorithms/Add Strings/README.md +++ b/Algorithms/Add Strings/README.md @@ -1,7 +1,7 @@ -#[Add Strings](https://leetcode.com/problems/add-strings/) -######No:`415` -######Difficulty:`Easy` -##JavaScript +# [Add Strings](https://leetcode.com/problems/add-strings/) +###### No:`415` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Assign Cookies/README.md b/Algorithms/Assign Cookies/README.md index 000c061..1638849 100644 --- a/Algorithms/Assign Cookies/README.md +++ b/Algorithms/Assign Cookies/README.md @@ -1,7 +1,7 @@ -#[Assign Cookies](https://leetcode.com/problems/assign-cookies/) -######No:`455` -######Difficulty:`Easy` -##JavaScript +# [Assign Cookies](https://leetcode.com/problems/assign-cookies/) +###### No:`455` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Balanced Binary Tree/README.md b/Algorithms/Balanced Binary Tree/README.md index b973b82..727dead 100644 --- a/Algorithms/Balanced Binary Tree/README.md +++ b/Algorithms/Balanced Binary Tree/README.md @@ -1,7 +1,7 @@ -#[Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/) -######No:`110` -######Difficulty:`Easy` -##JavaScript +# [Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/) +###### No:`110` +###### Difficulty:`Easy` +## JavaScript ```javascript /** @@ -40,5 +40,5 @@ var isBalanced = function(root) { }; ``` -####Description: +#### Description: Calculate the deepth of the Binary Tree is also a problem in here: [https://leetcode.com/problems/maximum-depth-of-binary-tree](https://leetcode.com/problems/maximum-depth-of-binary-tree) diff --git a/Algorithms/Best Time to Buy and Sell Stock II/README.md b/Algorithms/Best Time to Buy and Sell Stock II/README.md index 74d1e38..2099072 100644 --- a/Algorithms/Best Time to Buy and Sell Stock II/README.md +++ b/Algorithms/Best Time to Buy and Sell Stock II/README.md @@ -1,7 +1,7 @@ -#[Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) -######No:`122` -######Difficulty:`Medium` -##JavaScript +# [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) +###### No:`122` +###### Difficulty:`Medium` +## JavaScript ```javascript /** diff --git a/Algorithms/Binary Tree Level Order Traversal II/README.md b/Algorithms/Binary Tree Level Order Traversal II/README.md index 4137fb5..d57f3d0 100644 --- a/Algorithms/Binary Tree Level Order Traversal II/README.md +++ b/Algorithms/Binary Tree Level Order Traversal II/README.md @@ -1,7 +1,7 @@ -#[Binary Tree Level Order Traversal II](https://leetcode.com/problems/binary-tree-level-order-traversal-ii/) -######No:`107` -######Difficulty:`Easy` -##JavaScript +# [Binary Tree Level Order Traversal II](https://leetcode.com/problems/binary-tree-level-order-traversal-ii/) +###### No:`107` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Binary Tree Level Order Traversal/README.md b/Algorithms/Binary Tree Level Order Traversal/README.md index 160efb1..a79282c 100644 --- a/Algorithms/Binary Tree Level Order Traversal/README.md +++ b/Algorithms/Binary Tree Level Order Traversal/README.md @@ -1,7 +1,7 @@ -#[Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/) -######No:`102` -######Difficulty:`Easy` -##JavaScript +# [Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/) +###### No:`102` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Binary Tree Paths/README.md b/Algorithms/Binary Tree Paths/README.md index f6f31aa..b911ef6 100644 --- a/Algorithms/Binary Tree Paths/README.md +++ b/Algorithms/Binary Tree Paths/README.md @@ -1,7 +1,7 @@ -#[Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/) -######No:`257` -######Difficulty:`Easy` -##JavaScript +# [Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/) +###### No:`257` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Binary Tree Preorder Traversal/README.md b/Algorithms/Binary Tree Preorder Traversal/README.md index 24b6127..97ca4aa 100644 --- a/Algorithms/Binary Tree Preorder Traversal/README.md +++ b/Algorithms/Binary Tree Preorder Traversal/README.md @@ -1,7 +1,7 @@ -#[Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/) -######No:`144` -######Difficulty:`Medium` -##JavaScript +# [Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/) +###### No:`144` +###### Difficulty:`Medium` +## JavaScript ```javascript /** diff --git a/Algorithms/Binary Watch/README.md b/Algorithms/Binary Watch/README.md index 58a1801..428d7f0 100644 --- a/Algorithms/Binary Watch/README.md +++ b/Algorithms/Binary Watch/README.md @@ -1,7 +1,7 @@ -#[Binary Watch](https://leetcode.com/problems/binary-watch/) -######No:`401` -######Difficulty:`Easy` -##JavaScript +# [Binary Watch](https://leetcode.com/problems/binary-watch/) +###### No:`401` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Bulb Switcher/README.md b/Algorithms/Bulb Switcher/README.md index 88c6231..35e20e2 100644 --- a/Algorithms/Bulb Switcher/README.md +++ b/Algorithms/Bulb Switcher/README.md @@ -1,9 +1,9 @@ -#[Add Digits](https://leetcode.com/problems/add-digits/) -######No:`319` -######Difficulty:`Medium` -##JavaScript +# [Add Digits](https://leetcode.com/problems/add-digits/) +###### No:`319` +###### Difficulty:`Medium` +## JavaScript -####Solution 1: +#### Solution 1: refer: https://leetcode.com/discuss/91371/share-my-o-1-solution-with-explanation ```js /** @@ -16,7 +16,7 @@ var bulbSwitch = function(n) { ``` -####Solution 2: +#### Solution 2: #### Time Limit Exceeded. Input: 9999999 ```js /** @@ -24,14 +24,14 @@ var bulbSwitch = function(n) { * @return {number} */ var bulbSwitch = function(n) { - var bulbs = new Array(n).fill(0); + var bulbs = new Array(n).fill(0); for(i = 1; i <= n; i++) { for(var j = i; j <= n; j = j + i) { bulbs[j-1] = 1 - bulbs[j-1]; } } var sum = 0; - + for(var i = 0; i < bulbs.length; i++) { if(bulbs[i] == 1) { sum++; diff --git a/Algorithms/Climbing Stairs/README.md b/Algorithms/Climbing Stairs/README.md index 712e782..c30c2f4 100644 --- a/Algorithms/Climbing Stairs/README.md +++ b/Algorithms/Climbing Stairs/README.md @@ -1,7 +1,7 @@ -#[Climbing Stairs](https://leetcode.com/problems/climbing-stairs/) -######No:`70` -######Difficulty:`Easy` -##JavaScript +# [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/) +###### No:`70` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Compare Version Numbers/README.md b/Algorithms/Compare Version Numbers/README.md index 924493a..dbbc3e8 100644 --- a/Algorithms/Compare Version Numbers/README.md +++ b/Algorithms/Compare Version Numbers/README.md @@ -1,7 +1,7 @@ -#[Compare Version Numbers](https://leetcode.com/problems/compare-version-numbers/) -######No:`165` -######Difficulty:`Easy` -##JavaScript +# [Compare Version Numbers](https://leetcode.com/problems/compare-version-numbers/) +###### No:`165` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Contains Duplicate II/README.md b/Algorithms/Contains Duplicate II/README.md index 962b735..a2c7bc9 100644 --- a/Algorithms/Contains Duplicate II/README.md +++ b/Algorithms/Contains Duplicate II/README.md @@ -1,9 +1,9 @@ -#[Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/) -######No:`219` -######Difficulty:`Easy` -##JavaScript +# [Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/) +###### No:`219` +###### Difficulty:`Easy` +## JavaScript -####Solution 1: (slower) +#### Solution 1: (slower) ```javascript var containsNearbyDuplicate = function(nums, k) { for(var i = 0; i < nums.length; i++) { @@ -17,7 +17,7 @@ var containsNearbyDuplicate = function(nums, k) { }; ``` -####Solution 2: (faster) +#### Solution 2: (faster) ```javascript var containsNearbyDuplicate = function(nums, k) { var index = {}; diff --git a/Algorithms/Contains Duplicate/README.md b/Algorithms/Contains Duplicate/README.md index fa010f4..a57eec3 100644 --- a/Algorithms/Contains Duplicate/README.md +++ b/Algorithms/Contains Duplicate/README.md @@ -1,7 +1,7 @@ -#[Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) -######No:`217` -######Difficulty:`Easy` -##JavaScript +# [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) +###### No:`217` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Counting Bits/README.md b/Algorithms/Counting Bits/README.md index 56afc44..dc718cd 100644 --- a/Algorithms/Counting Bits/README.md +++ b/Algorithms/Counting Bits/README.md @@ -1,7 +1,7 @@ -#[Counting Bits](https://leetcode.com/problems/counting-bits/) -######No:`338` -######Difficulty:`Medium` -##JavaScript +# [Counting Bits](https://leetcode.com/problems/counting-bits/) +###### No:`338` +###### Difficulty:`Medium` +## JavaScript ```javascript var countBits = function(num) { @@ -16,7 +16,7 @@ var countBits = function(num) { }; ``` -####How to solve? +#### How to solve? DP: countBits[0] = 0; countBits[1] = 1; diff --git a/Algorithms/Delete Node in a Linked List/README.md b/Algorithms/Delete Node in a Linked List/README.md index 75f5014..255f49e 100644 --- a/Algorithms/Delete Node in a Linked List/README.md +++ b/Algorithms/Delete Node in a Linked List/README.md @@ -1,7 +1,7 @@ -#[Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/) -######No:`237` -######Difficulty:`Easy` -##JavaScript +# [Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/) +###### No:`237` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Evaluate Reverse Polish Notation/README.md b/Algorithms/Evaluate Reverse Polish Notation/README.md index 488bfe9..ef6b689 100644 --- a/Algorithms/Evaluate Reverse Polish Notation/README.md +++ b/Algorithms/Evaluate Reverse Polish Notation/README.md @@ -1,7 +1,7 @@ -#[Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/) -######No:`150` -######Difficulty:`Medium` -##JavaScript +# [Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/) +###### No:`150` +###### Difficulty:`Medium` +## JavaScript ```javascript /** diff --git a/Algorithms/Excel Sheet Column Number/README.md b/Algorithms/Excel Sheet Column Number/README.md index c070dd4..79e7c17 100644 --- a/Algorithms/Excel Sheet Column Number/README.md +++ b/Algorithms/Excel Sheet Column Number/README.md @@ -1,7 +1,7 @@ -#[Excel Sheet Column Number](https://leetcode.com/problems/excel-sheet-column-number/) -######No:`171` -######Difficulty:`Easy` -##JavaScript +# [Excel Sheet Column Number](https://leetcode.com/problems/excel-sheet-column-number/) +###### No:`171` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Excel Sheet Column Title/README.md b/Algorithms/Excel Sheet Column Title/README.md index c6bd25e..ec7c1a9 100644 --- a/Algorithms/Excel Sheet Column Title/README.md +++ b/Algorithms/Excel Sheet Column Title/README.md @@ -1,7 +1,7 @@ -#[Excel Sheet Column Title](https://leetcode.com/problems/excel-sheet-column-title/) -######No:`168` -######Difficulty:`Easy` -##JavaScript +# [Excel Sheet Column Title](https://leetcode.com/problems/excel-sheet-column-title/) +###### No:`168` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Factorial Trailing Zeroes/README.md b/Algorithms/Factorial Trailing Zeroes/README.md index 160b2c3..6dcbe10 100644 --- a/Algorithms/Factorial Trailing Zeroes/README.md +++ b/Algorithms/Factorial Trailing Zeroes/README.md @@ -1,7 +1,7 @@ -#[Factorial Trailing Zeroes](https://leetcode.com/problems/factorial-trailing-zeroes/) -######No:`172` -######Difficulty:`Easy` -##JavaScript +# [Factorial Trailing Zeroes](https://leetcode.com/problems/factorial-trailing-zeroes/) +###### No:`172` +###### Difficulty:`Easy` +## JavaScript ```javascript /** @@ -19,7 +19,7 @@ var trailingZeroes = function(n) { }; ``` -###How to solve? +### How to solve? Only 2 * 5 can make 0. 4, 6, 8 can represent as 2 * 2, 2 * 3, 2 * 2 * 2. So 2 is more then 5.You need only count the number 5. 5 catains one 5; diff --git a/Algorithms/Find the Difference/README.md b/Algorithms/Find the Difference/README.md index 8dc35b9..263ec05 100644 --- a/Algorithms/Find the Difference/README.md +++ b/Algorithms/Find the Difference/README.md @@ -1,7 +1,7 @@ -#[Find the Difference](https://leetcode.com/problems/find-the-difference/) -######No:`389` -######Difficulty:`Easy` -##JavaScript +# [Find the Difference](https://leetcode.com/problems/find-the-difference/) +###### No:`389` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/First Unique Character in a String/README.md b/Algorithms/First Unique Character in a String/README.md index 42787c9..95e2fbf 100644 --- a/Algorithms/First Unique Character in a String/README.md +++ b/Algorithms/First Unique Character in a String/README.md @@ -1,7 +1,7 @@ -#[First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) -######No:`387` -######Difficulty:`Easy` -##JavaScript +# [First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) +###### No:`387` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Fizz Buzz/README.md b/Algorithms/Fizz Buzz/README.md index d155559..2e28688 100644 --- a/Algorithms/Fizz Buzz/README.md +++ b/Algorithms/Fizz Buzz/README.md @@ -1,7 +1,7 @@ -#[Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) -######No:`412` -######Difficulty:`Easy` -##JavaScript +# [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) +###### No:`412` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Generate Parentheses/README.md b/Algorithms/Generate Parentheses/README.md index c1c0b0d..c752baa 100644 --- a/Algorithms/Generate Parentheses/README.md +++ b/Algorithms/Generate Parentheses/README.md @@ -1,7 +1,7 @@ -#[Generate Parentheses](https://leetcode.com/problems/generate-parentheses/) -######No:`22` -######Difficulty:`Medium` -##JavaScript +# [Generate Parentheses](https://leetcode.com/problems/generate-parentheses/) +###### No:`22` +###### Difficulty:`Medium` +## JavaScript ```javascript /** diff --git a/Algorithms/Happy Number/README.md b/Algorithms/Happy Number/README.md index fa3d5ab..dee5aad 100644 --- a/Algorithms/Happy Number/README.md +++ b/Algorithms/Happy Number/README.md @@ -1,7 +1,7 @@ -#[Happy Number](https://leetcode.com/problems/happy-number/) -######No:`202` -######Difficulty:`Easy` -##JavaScript +# [Happy Number](https://leetcode.com/problems/happy-number/) +###### No:`202` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/House Robber/README.md b/Algorithms/House Robber/README.md index 3224308..8e3142f 100644 --- a/Algorithms/House Robber/README.md +++ b/Algorithms/House Robber/README.md @@ -1,7 +1,7 @@ -#[House Robber](https://leetcode.com/problems/house-robber/) -######No:`198` -######Difficulty:`Easy` -##JavaScript +# [House Robber](https://leetcode.com/problems/house-robber/) +###### No:`198` +###### Difficulty:`Easy` +## JavaScript ```javascript /** @@ -27,9 +27,9 @@ var rob = function(nums) { ``` ``` -###How to solve? +### How to solve? ``` -#####Using DP. +##### Using DP. max[0] = nums[0]; max[1] = Math.max(nums[0], nums[1]); max[n] = Math.max(max[n] + max[n - 2], max[n -1]); diff --git a/Algorithms/Implement Queue using Stacks/README.md b/Algorithms/Implement Queue using Stacks/README.md index c7dacaa..86bbf8f 100644 --- a/Algorithms/Implement Queue using Stacks/README.md +++ b/Algorithms/Implement Queue using Stacks/README.md @@ -1,7 +1,7 @@ -#[Implement Queue using Stacks](https://leetcode.com/problems/implement-queue-using-stacks/) -######No:`232` -######Difficulty:`Easy` -##JavaScript +# [Implement Queue using Stacks](https://leetcode.com/problems/implement-queue-using-stacks/) +###### No:`232` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Implement strStr()/README.md b/Algorithms/Implement strStr()/README.md index 3ecab39..a47979a 100644 --- a/Algorithms/Implement strStr()/README.md +++ b/Algorithms/Implement strStr()/README.md @@ -1,7 +1,7 @@ -#[Implement strStr()](https://leetcode.com/problems/implement-strstr) -######No:`28` -######Difficulty:`Easy` -##JavaScript +# [Implement strStr()](https://leetcode.com/problems/implement-strstr) +###### No:`28` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Integer Break/README.md b/Algorithms/Integer Break/README.md index 53c75cd..53a31ee 100644 --- a/Algorithms/Integer Break/README.md +++ b/Algorithms/Integer Break/README.md @@ -1,7 +1,7 @@ -#[Integer Break](https://leetcode.com/problems/integer-break/) -######No:`343` -######Difficulty:`Medium` -##JavaScript +# [Integer Break](https://leetcode.com/problems/integer-break/) +###### No:`343` +###### Difficulty:`Medium` +## JavaScript ```javascript /** diff --git a/Algorithms/Integer to Roman/README.md b/Algorithms/Integer to Roman/README.md index 5b04555..7d12bcd 100644 --- a/Algorithms/Integer to Roman/README.md +++ b/Algorithms/Integer to Roman/README.md @@ -1,7 +1,7 @@ -#[Integer to Roman](https://leetcode.com/problems/integer-to-roman/) -######No:`12` -######Difficulty:`Medium` -##JavaScript +# [Integer to Roman](https://leetcode.com/problems/integer-to-roman/) +###### No:`12` +###### Difficulty:`Medium` +## JavaScript ```javascript /** diff --git a/Algorithms/Intersection of Two Arrays II/README.md b/Algorithms/Intersection of Two Arrays II/README.md index ee184ce..3d2f792 100644 --- a/Algorithms/Intersection of Two Arrays II/README.md +++ b/Algorithms/Intersection of Two Arrays II/README.md @@ -1,7 +1,7 @@ -#[Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) -######No:`350` -######Difficulty:`Easy` -##JavaScript +# [Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) +###### No:`350` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Intersection of Two Arrays/README.md b/Algorithms/Intersection of Two Arrays/README.md index 64ecf4f..0187794 100644 --- a/Algorithms/Intersection of Two Arrays/README.md +++ b/Algorithms/Intersection of Two Arrays/README.md @@ -1,7 +1,7 @@ -#[Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/) -######No:`349` -######Difficulty:`Easy` -##JavaScript +# [Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/) +###### No:`349` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Intersection of Two Linked Lists/README.md b/Algorithms/Intersection of Two Linked Lists/README.md index f8ffa3b..53d4502 100644 --- a/Algorithms/Intersection of Two Linked Lists/README.md +++ b/Algorithms/Intersection of Two Linked Lists/README.md @@ -1,7 +1,7 @@ -#[Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/) -######No:`160` -######Difficulty:`Easy` -##JavaScript +# [Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/) +###### No:`160` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Invert Binary Tree/README.md b/Algorithms/Invert Binary Tree/README.md index 827d7cd..32bcd83 100644 --- a/Algorithms/Invert Binary Tree/README.md +++ b/Algorithms/Invert Binary Tree/README.md @@ -1,7 +1,7 @@ -#[Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/) -######No:`226` -######Difficulty:`Easy` -##JavaScript +# [Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/) +###### No:`226` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Island Perimeter/README.md b/Algorithms/Island Perimeter/README.md index 9934e0a..40dca30 100644 --- a/Algorithms/Island Perimeter/README.md +++ b/Algorithms/Island Perimeter/README.md @@ -1,7 +1,7 @@ -#[Add Digits](https://leetcode.com/problems/island-perimeter/) -######No:`463` -######Difficulty:`Easy` -##JavaScript +# [Add Digits](https://leetcode.com/problems/island-perimeter/) +###### No:`463` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Largest Number/README.md b/Algorithms/Largest Number/README.md index 66c1d36..09a9442 100644 --- a/Algorithms/Largest Number/README.md +++ b/Algorithms/Largest Number/README.md @@ -1,7 +1,7 @@ -#[Largest Number](https://leetcode.com/problems/largest-number/) -######No:`179` -######Difficulty:`Medium` -##JavaScript +# [Largest Number](https://leetcode.com/problems/largest-number/) +###### No:`179` +###### Difficulty:`Medium` +## JavaScript ```javascript /** diff --git a/Algorithms/Letter Combinations of a Phone Number/README.md b/Algorithms/Letter Combinations of a Phone Number/README.md index a82d71a..1eacd1a 100644 --- a/Algorithms/Letter Combinations of a Phone Number/README.md +++ b/Algorithms/Letter Combinations of a Phone Number/README.md @@ -1,7 +1,7 @@ -#[Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/) -######No:`17` -######Difficulty:`Medium` -##JavaScript +# [Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/) +###### No:`17` +###### Difficulty:`Medium` +## JavaScript ```javascript /** diff --git a/Algorithms/Longest Palindrome/README.md b/Algorithms/Longest Palindrome/README.md index 2f6b9a0..041b19d 100644 --- a/Algorithms/Longest Palindrome/README.md +++ b/Algorithms/Longest Palindrome/README.md @@ -1,7 +1,7 @@ -#[Longest Palindrome](https://leetcode.com/problems/longest-palindrome/) -######No:`409` -######Difficulty:`Easy` -##JavaScript +# [Longest Palindrome](https://leetcode.com/problems/longest-palindrome/) +###### No:`409` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Lowest Common Ancestor of a Binary Search Tree/README.md b/Algorithms/Lowest Common Ancestor of a Binary Search Tree/README.md index 17373ef..c7b0ad3 100644 --- a/Algorithms/Lowest Common Ancestor of a Binary Search Tree/README.md +++ b/Algorithms/Lowest Common Ancestor of a Binary Search Tree/README.md @@ -1,7 +1,7 @@ -#[Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree) -######No:`235` -######Difficulty:`Easy` -##JavaScript +# [Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree) +###### No:`235` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Majority Element/README.md b/Algorithms/Majority Element/README.md index c676e7b..390ec31 100644 --- a/Algorithms/Majority Element/README.md +++ b/Algorithms/Majority Element/README.md @@ -1,7 +1,7 @@ -#[Majority Element](https://leetcode.com/problems/majority-element/) -######No:`169` -######Difficulty:`Easy` -##JavaScript +# [Majority Element](https://leetcode.com/problems/majority-element/) +###### No:`169` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Max Points on a Line/README.md b/Algorithms/Max Points on a Line/README.md index 6437b5a..1ec3aa4 100644 --- a/Algorithms/Max Points on a Line/README.md +++ b/Algorithms/Max Points on a Line/README.md @@ -1,7 +1,7 @@ -#[Max Points on a Line](https://leetcode.com/problems/max-points-on-a-line/) -######No:`149` -######Difficulty:`Hard` -##CPP +# [Max Points on a Line](https://leetcode.com/problems/max-points-on-a-line/) +###### No:`149` +###### Difficulty:`Hard` +## CPP ```cpp /** diff --git a/Algorithms/Maximum Depth of Binary Tree/README.md b/Algorithms/Maximum Depth of Binary Tree/README.md index 492e02a..2ebc70c 100644 --- a/Algorithms/Maximum Depth of Binary Tree/README.md +++ b/Algorithms/Maximum Depth of Binary Tree/README.md @@ -1,7 +1,7 @@ -#[Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/) -######No:`104` -######Difficulty:`Easy` -##JavaScript +# [Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/) +###### No:`104` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Merge Sorted Array/README.md b/Algorithms/Merge Sorted Array/README.md index bc6e6e4..5d47be0 100644 --- a/Algorithms/Merge Sorted Array/README.md +++ b/Algorithms/Merge Sorted Array/README.md @@ -1,7 +1,7 @@ -#[Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/) -######No:`88` -######Difficulty:`Easy` -##JavaScript +# [Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/) +###### No:`88` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Merge Two Sorted Lists/README.md b/Algorithms/Merge Two Sorted Lists/README.md index 0a9fd0e..5849bcb 100644 --- a/Algorithms/Merge Two Sorted Lists/README.md +++ b/Algorithms/Merge Two Sorted Lists/README.md @@ -1,7 +1,7 @@ -#[Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) -######No:`21` -######Difficulty:`Easy` -##JavaScript +# [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) +###### No:`21` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Min Stack/README.md b/Algorithms/Min Stack/README.md index adfd0f6..9a775d0 100644 --- a/Algorithms/Min Stack/README.md +++ b/Algorithms/Min Stack/README.md @@ -1,7 +1,7 @@ -#[Min Stack](https://leetcode.com/problems/min-stack/) -######No:`155` -######Difficulty:`Easy` -##JavaScript +# [Min Stack](https://leetcode.com/problems/min-stack/) +###### No:`155` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Minimum Depth of Binary Tree/README.md b/Algorithms/Minimum Depth of Binary Tree/README.md index 1fa1291..1491978 100644 --- a/Algorithms/Minimum Depth of Binary Tree/README.md +++ b/Algorithms/Minimum Depth of Binary Tree/README.md @@ -1,7 +1,7 @@ -#[Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/) -######No:`111` -######Difficulty:`Easy` -##JavaScript +# [Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/) +###### No:`111` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Minimum Moves to Equal Array Elements/README.md b/Algorithms/Minimum Moves to Equal Array Elements/README.md index fe7aa4e..2e83e94 100644 --- a/Algorithms/Minimum Moves to Equal Array Elements/README.md +++ b/Algorithms/Minimum Moves to Equal Array Elements/README.md @@ -1,7 +1,7 @@ -#[Minimum Moves to Equal Array Elements](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/) -######No:`453` -######Difficulty:`Easy` -##JavaScript +# [Minimum Moves to Equal Array Elements](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/) +###### No:`453` +###### Difficulty:`Easy` +## JavaScript ### Solution: Increasing n-i elements by 1 is same as decrease 1 elements by 1. diff --git a/Algorithms/Missing Number/README.md b/Algorithms/Missing Number/README.md index 2ac46eb..8b2e385 100644 --- a/Algorithms/Missing Number/README.md +++ b/Algorithms/Missing Number/README.md @@ -1,7 +1,7 @@ -#[Missing Number](https://leetcode.com/problems/missing-number/) -######No:`268` -######Difficulty:`Medium` -##JavaScript +# [Missing Number](https://leetcode.com/problems/missing-number/) +###### No:`268` +###### Difficulty:`Medium` +## JavaScript ```javascript /** diff --git a/Algorithms/Move Zeroes/README.md b/Algorithms/Move Zeroes/README.md index c56236e..5a284a9 100644 --- a/Algorithms/Move Zeroes/README.md +++ b/Algorithms/Move Zeroes/README.md @@ -1,7 +1,7 @@ -#[Move Zeroes](https://leetcode.com/problems/move-zeroes/) -######No:`283` -######Difficulty:`Easy` -##JavaScript +# [Move Zeroes](https://leetcode.com/problems/move-zeroes/) +###### No:`283` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Nim Game/README.md b/Algorithms/Nim Game/README.md index 8b3aa46..9764cb4 100644 --- a/Algorithms/Nim Game/README.md +++ b/Algorithms/Nim Game/README.md @@ -1,7 +1,7 @@ -#[Nim Game](https://leetcode.com/problems/nim-game/) -######No:`292` -######Difficulty:`Easy` -##JavaScript +# [Nim Game](https://leetcode.com/problems/nim-game/) +###### No:`292` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Number of 1 Bits/README.md b/Algorithms/Number of 1 Bits/README.md index eaf6d79..c5583b7 100644 --- a/Algorithms/Number of 1 Bits/README.md +++ b/Algorithms/Number of 1 Bits/README.md @@ -1,7 +1,7 @@ -#[Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/) -######No:`191` -######Difficulty:`Easy` -##JavaScript +# [Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/) +###### No:`191` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Number of Boomerangs/README.md b/Algorithms/Number of Boomerangs/README.md index 795ad25..72810a5 100644 --- a/Algorithms/Number of Boomerangs/README.md +++ b/Algorithms/Number of Boomerangs/README.md @@ -1,7 +1,7 @@ -#[Number of Boomerangs](https://leetcode.com/problems/number-of-boomerangs/) -######No:`447` -######Difficulty:`Easy` -##JavaScript +# [Number of Boomerangs](https://leetcode.com/problems/number-of-boomerangs/) +###### No:`447` +###### Difficulty:`Easy` +## JavaScript ### Solution: Get each 2 points' distance. For one point, save the distance as the key and count as the value into a map. The Boomerangs which contains this point will be count * (count - 1). diff --git a/Algorithms/Odd Even Linked List/README.md b/Algorithms/Odd Even Linked List/README.md index d074807..2289e5f 100644 --- a/Algorithms/Odd Even Linked List/README.md +++ b/Algorithms/Odd Even Linked List/README.md @@ -1,7 +1,7 @@ -#[Odd Even Linked List](https://leetcode.com/problems/odd-even-linked-list/) -######No:`328` -######Difficulty:`Medium` -##JavaScript +# [Odd Even Linked List](https://leetcode.com/problems/odd-even-linked-list/) +###### No:`328` +###### Difficulty:`Medium` +## JavaScript ```javascript /** diff --git a/Algorithms/Palindrome Linked List/README.md b/Algorithms/Palindrome Linked List/README.md index ec81729..77eaee8 100644 --- a/Algorithms/Palindrome Linked List/README.md +++ b/Algorithms/Palindrome Linked List/README.md @@ -1,7 +1,7 @@ -#[Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list/) -######No:`234` -######Difficulty:`Easy` -##JavaScript +# [Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list/) +###### No:`234` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Palindrome Number/README.md b/Algorithms/Palindrome Number/README.md index 0d6d70c..ec97da2 100644 --- a/Algorithms/Palindrome Number/README.md +++ b/Algorithms/Palindrome Number/README.md @@ -1,7 +1,7 @@ -#[Palindrome Number](https://leetcode.com/problems/palindrome-number/) -######No:`9` -######Difficulty:`Easy` -##JavaScript +# [Palindrome Number](https://leetcode.com/problems/palindrome-number/) +###### No:`9` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Pascal's Triangle II/README.md b/Algorithms/Pascal's Triangle II/README.md index 35b5f56..923dfc6 100644 --- a/Algorithms/Pascal's Triangle II/README.md +++ b/Algorithms/Pascal's Triangle II/README.md @@ -1,7 +1,7 @@ -#[Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii/) -######No:`119` -######Difficulty:`Easy` -##JavaScript +# [Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii/) +###### No:`119` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Pascal's Triangle/README.md b/Algorithms/Pascal's Triangle/README.md index dbd07b6..db2e1f2 100644 --- a/Algorithms/Pascal's Triangle/README.md +++ b/Algorithms/Pascal's Triangle/README.md @@ -1,7 +1,7 @@ -#[Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/) -######No:`118` -######Difficulty:`Easy` -##JavaScript +# [Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/) +###### No:`118` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Path Sum/README.md b/Algorithms/Path Sum/README.md index 1517572..d4bd6c6 100644 --- a/Algorithms/Path Sum/README.md +++ b/Algorithms/Path Sum/README.md @@ -1,7 +1,7 @@ -#[Path Sum](https://leetcode.com/problems/path-sum/) -######No:`112` -######Difficulty:`Easy` -##JavaScript +# [Path Sum](https://leetcode.com/problems/path-sum/) +###### No:`112` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Permutations/README.md b/Algorithms/Permutations/README.md index 8a1cbec..34cd7e8 100644 --- a/Algorithms/Permutations/README.md +++ b/Algorithms/Permutations/README.md @@ -1,7 +1,7 @@ -#[Permutations](https://leetcode.com/problems/permutations/) -######No:`46` -######Difficulty:`Medium` -##JavaScript +# [Permutations](https://leetcode.com/problems/permutations/) +###### No:`46` +###### Difficulty:`Medium` +## JavaScript ```javascript /** diff --git a/Algorithms/Plus One/README.md b/Algorithms/Plus One/README.md index a140e70..365db73 100644 --- a/Algorithms/Plus One/README.md +++ b/Algorithms/Plus One/README.md @@ -1,7 +1,7 @@ -#[Plus One](https://leetcode.com/problems/plus-one/) -######No:`66` -######Difficulty:`Easy` -##JavaScript +# [Plus One](https://leetcode.com/problems/plus-one/) +###### No:`66` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Power of Three/README.md b/Algorithms/Power of Three/README.md index b235d80..d60d49f 100644 --- a/Algorithms/Power of Three/README.md +++ b/Algorithms/Power of Three/README.md @@ -1,7 +1,7 @@ -#[Power of Three](https://leetcode.com/problems/power-of-three/) -######No:`326` -######Difficulty:`Easy` -##JavaScript +# [Power of Three](https://leetcode.com/problems/power-of-three/) +###### No:`326` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Power of Two/README.md b/Algorithms/Power of Two/README.md index 18ffe0b..fd5860d 100644 --- a/Algorithms/Power of Two/README.md +++ b/Algorithms/Power of Two/README.md @@ -1,7 +1,7 @@ -#[Power of Two](https://leetcode.com/problems/power-of-two/) -######No:`231` -######Difficulty:`Easy` -##JavaScript +# [Power of Two](https://leetcode.com/problems/power-of-two/) +###### No:`231` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Product of Array Except Self/README.md b/Algorithms/Product of Array Except Self/README.md index c5e25d8..dab9786 100644 --- a/Algorithms/Product of Array Except Self/README.md +++ b/Algorithms/Product of Array Except Self/README.md @@ -1,7 +1,7 @@ -#[Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/) -######No:`238` -######Difficulty:`Medium` -##JavaScript +# [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/) +###### No:`238` +###### Difficulty:`Medium` +## JavaScript ```javascript /** diff --git a/Algorithms/Ransom Note/README.md b/Algorithms/Ransom Note/README.md index 975341d..747dea1 100644 --- a/Algorithms/Ransom Note/README.md +++ b/Algorithms/Ransom Note/README.md @@ -1,7 +1,7 @@ -#[Ransom Note](https://leetcode.com/problems/ransom-note/) -######No:`383` -######Difficulty:`Easy` -##JavaScript +# [Ransom Note](https://leetcode.com/problems/ransom-note/) +###### No:`383` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Rectangle Area/README.md b/Algorithms/Rectangle Area/README.md index 00a2999..98e2878 100644 --- a/Algorithms/Rectangle Area/README.md +++ b/Algorithms/Rectangle Area/README.md @@ -1,7 +1,7 @@ -#[Rectangle Area](https://leetcode.com/problems/rectangle-area/) -######No:`223` -######Difficulty:`Easy` -##JavaScript +# [Rectangle Area](https://leetcode.com/problems/rectangle-area/) +###### No:`223` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Remove Duplicates from Sorted Array/README.md b/Algorithms/Remove Duplicates from Sorted Array/README.md index 1904b8b..330ff90 100644 --- a/Algorithms/Remove Duplicates from Sorted Array/README.md +++ b/Algorithms/Remove Duplicates from Sorted Array/README.md @@ -1,7 +1,7 @@ -#[Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) -######No:`26` -######Difficulty:`Easy` -##JavaScript +# [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) +###### No:`26` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Remove Duplicates from Sorted List/README.md b/Algorithms/Remove Duplicates from Sorted List/README.md index adf47bd..8fa7855 100644 --- a/Algorithms/Remove Duplicates from Sorted List/README.md +++ b/Algorithms/Remove Duplicates from Sorted List/README.md @@ -1,7 +1,7 @@ -#[Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list/) -######No:`83` -######Difficulty:`Easy` -##JavaScript +# [Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list/) +###### No:`83` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Remove Element/README.md b/Algorithms/Remove Element/README.md index 85faa71..f9b81ec 100644 --- a/Algorithms/Remove Element/README.md +++ b/Algorithms/Remove Element/README.md @@ -1,7 +1,7 @@ -#[Remove Element](https://leetcode.com/problems/remove-element/) -######No:`27` -######Difficulty:`Easy` -##JavaScript +# [Remove Element](https://leetcode.com/problems/remove-element/) +###### No:`27` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Remove Linked List Elements/README.md b/Algorithms/Remove Linked List Elements/README.md index 93ee4c2..ebcbe4a 100644 --- a/Algorithms/Remove Linked List Elements/README.md +++ b/Algorithms/Remove Linked List Elements/README.md @@ -1,7 +1,7 @@ -#[Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements/) -######No:`203` -######Difficulty:`Easy` -##JavaScript +# [Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements/) +###### No:`203` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Remove Nth Node From End of List/README.md b/Algorithms/Remove Nth Node From End of List/README.md index d687f94..39dd652 100644 --- a/Algorithms/Remove Nth Node From End of List/README.md +++ b/Algorithms/Remove Nth Node From End of List/README.md @@ -1,7 +1,7 @@ -#[Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/) -######No:`19` -######Difficulty:`Easy` -##JavaScript +# [Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/) +###### No:`19` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Reverse Integer/README.md b/Algorithms/Reverse Integer/README.md index f3ff987..9893006 100644 --- a/Algorithms/Reverse Integer/README.md +++ b/Algorithms/Reverse Integer/README.md @@ -1,7 +1,7 @@ -#[Reverse Integer](https://leetcode.com/problems/reverse-integer/) -######No:`7` -######Difficulty:`Easy` -##JavaScript +# [Reverse Integer](https://leetcode.com/problems/reverse-integer/) +###### No:`7` +###### Difficulty:`Easy` +## JavaScript ```javascript /** @@ -22,6 +22,6 @@ var reverse = function(x) { } }; ``` -######PS: +###### PS: Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases? For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. diff --git a/Algorithms/Reverse Linked List II/README.md b/Algorithms/Reverse Linked List II/README.md index 50e59c3..86cc9be 100644 --- a/Algorithms/Reverse Linked List II/README.md +++ b/Algorithms/Reverse Linked List II/README.md @@ -1,7 +1,7 @@ -#[Reverse Linked List II](https://leetcode.com/problems/reverse-linked-list-ii/) -######No:`92` -######Difficulty:`Medium` -##JavaScript +# [Reverse Linked List II](https://leetcode.com/problems/reverse-linked-list-ii/) +###### No:`92` +###### Difficulty:`Medium` +## JavaScript ```javascript /** diff --git a/Algorithms/Reverse Linked List/README.md b/Algorithms/Reverse Linked List/README.md index 368e363..3a9f3ce 100644 --- a/Algorithms/Reverse Linked List/README.md +++ b/Algorithms/Reverse Linked List/README.md @@ -1,7 +1,7 @@ -#[Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) -######No:`206` -######Difficulty:`Easy` -##JavaScript +# [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) +###### No:`206` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Reverse String/README.md b/Algorithms/Reverse String/README.md index d57cb4b..17f5487 100644 --- a/Algorithms/Reverse String/README.md +++ b/Algorithms/Reverse String/README.md @@ -1,7 +1,7 @@ -#[Reverse String](https://leetcode.com/problems/reverse-string/) -######No:`344` -######Difficulty:`Easy` -##JavaScript +# [Reverse String](https://leetcode.com/problems/reverse-string/) +###### No:`344` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Reverse Words in a String/README.md b/Algorithms/Reverse Words in a String/README.md index 4ec142f..c134414 100644 --- a/Algorithms/Reverse Words in a String/README.md +++ b/Algorithms/Reverse Words in a String/README.md @@ -1,7 +1,7 @@ -#[Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) -######No:`151` -######Difficulty:`Easy` -##JavaScript +# [Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) +###### No:`151` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Roman to Integer/README.md b/Algorithms/Roman to Integer/README.md index 522efb4..8814420 100644 --- a/Algorithms/Roman to Integer/README.md +++ b/Algorithms/Roman to Integer/README.md @@ -1,7 +1,7 @@ -#[Roman to Integer](https://leetcode.com/problems/roman-to-integer/) -######No:`13` -######Difficulty:`Easy` -##JavaScript +# [Roman to Integer](https://leetcode.com/problems/roman-to-integer/) +###### No:`13` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Rotate Array/README.md b/Algorithms/Rotate Array/README.md index e97a5d9..92d6400 100644 --- a/Algorithms/Rotate Array/README.md +++ b/Algorithms/Rotate Array/README.md @@ -1,7 +1,7 @@ -#[Rotate Array](https://leetcode.com/problems/rotate-array/) -######No:`189` -######Difficulty:`Easy` -##JavaScript +# [Rotate Array](https://leetcode.com/problems/rotate-array/) +###### No:`189` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Same Tree/README.md b/Algorithms/Same Tree/README.md index c4be81e..44659f8 100644 --- a/Algorithms/Same Tree/README.md +++ b/Algorithms/Same Tree/README.md @@ -1,7 +1,7 @@ -#[Same Tree](https://leetcode.com/problems/same-tree/) -######No:`100` -######Difficulty:`Easy` -##JavaScript +# [Same Tree](https://leetcode.com/problems/same-tree/) +###### No:`100` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Set Matrix Zeroes/README.md b/Algorithms/Set Matrix Zeroes/README.md index 91f19d6..1ed5956 100644 --- a/Algorithms/Set Matrix Zeroes/README.md +++ b/Algorithms/Set Matrix Zeroes/README.md @@ -1,7 +1,7 @@ -#[Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/) -######No:`73` -######Difficulty:`Medium` -##JavaScript +# [Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/) +###### No:`73` +###### Difficulty:`Medium` +## JavaScript ```javascript /** diff --git a/Algorithms/Single Number/README.md b/Algorithms/Single Number/README.md index b1bfe1a..714a3bc 100644 --- a/Algorithms/Single Number/README.md +++ b/Algorithms/Single Number/README.md @@ -1,7 +1,7 @@ -#[Single Number](https://leetcode.com/problems/single-number/) -######No:`136` -######Difficulty:`Medium` -##JavaScript +# [Single Number](https://leetcode.com/problems/single-number/) +###### No:`136` +###### Difficulty:`Medium` +## JavaScript ```javascript /** @@ -15,6 +15,6 @@ var singleNumber = function(nums) { }; ``` -####Description: +#### Description: Any number that XOR itselt will be 0; diff --git a/Algorithms/Sort List/README.md b/Algorithms/Sort List/README.md index 78ba3fb..5e74c5d 100644 --- a/Algorithms/Sort List/README.md +++ b/Algorithms/Sort List/README.md @@ -1,7 +1,7 @@ -#[Sort List](https://leetcode.com/problems/sort-list/) -######No:`148` -######Difficulty:`Medium` -##JavaScript +# [Sort List](https://leetcode.com/problems/sort-list/) +###### No:`148` +###### Difficulty:`Medium` +## JavaScript ```javascript /** diff --git a/Algorithms/String to Integer (atoi)/README.md b/Algorithms/String to Integer (atoi)/README.md index fe59c95..a58758c 100644 --- a/Algorithms/String to Integer (atoi)/README.md +++ b/Algorithms/String to Integer (atoi)/README.md @@ -1,7 +1,7 @@ -#[String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/) -######No:`8` -######Difficulty:`Easy` -##JavaScript +# [String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/) +###### No:`8` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Sum of Left Leaves/README.md b/Algorithms/Sum of Left Leaves/README.md index 734d18f..dc6cbb5 100644 --- a/Algorithms/Sum of Left Leaves/README.md +++ b/Algorithms/Sum of Left Leaves/README.md @@ -1,7 +1,7 @@ -#[Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) -######No:`404` -######Difficulty:`Easy` -##JavaScript +# [Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) +###### No:`404` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Summary Ranges/README.md b/Algorithms/Summary Ranges/README.md index 9134dec..dee84bf 100644 --- a/Algorithms/Summary Ranges/README.md +++ b/Algorithms/Summary Ranges/README.md @@ -1,7 +1,7 @@ -#[Summary Ranges](https://leetcode.com/problems/summary-ranges/) -######No:`228` -######Difficulty:`Easy` -##JavaScript +# [Summary Ranges](https://leetcode.com/problems/summary-ranges/) +###### No:`228` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Swap Nodes in Pairs/README.md b/Algorithms/Swap Nodes in Pairs/README.md index 42ee251..6dec208 100644 --- a/Algorithms/Swap Nodes in Pairs/README.md +++ b/Algorithms/Swap Nodes in Pairs/README.md @@ -1,7 +1,7 @@ -#[Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) -######No:`24` -######Difficulty:`Easy` -##JavaScript +# [Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) +###### No:`24` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Symmetric Tree/README.md b/Algorithms/Symmetric Tree/README.md index 9479daa..467f4ee 100644 --- a/Algorithms/Symmetric Tree/README.md +++ b/Algorithms/Symmetric Tree/README.md @@ -1,7 +1,7 @@ -#[Symmetric Tree](https://leetcode.com/problems/symmetric-tree/) -######No:`101` -######Difficulty:`Easy` -##JavaScript +# [Symmetric Tree](https://leetcode.com/problems/symmetric-tree/) +###### No:`101` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Top K Frequent Elements/README.md b/Algorithms/Top K Frequent Elements/README.md index 47e517b..68c18c1 100644 --- a/Algorithms/Top K Frequent Elements/README.md +++ b/Algorithms/Top K Frequent Elements/README.md @@ -1,7 +1,7 @@ -#[Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) -######No:`347` -######Difficulty:`Medium` -##JavaScript +# [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) +###### No:`347` +###### Difficulty:`Medium` +## JavaScript ```javascript /** diff --git a/Algorithms/Two Sum/README.md b/Algorithms/Two Sum/README.md index 148aaa8..3f1c708 100644 --- a/Algorithms/Two Sum/README.md +++ b/Algorithms/Two Sum/README.md @@ -1,7 +1,7 @@ -#[Two Sum](https://leetcode.com/problems/two-sum/) -######No:`1` -######Difficulty:`Easy` -##JavaScript +# [Two Sum](https://leetcode.com/problems/two-sum/) +###### No:`1` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Ugly Number/README.md b/Algorithms/Ugly Number/README.md index 10726cd..6154158 100644 --- a/Algorithms/Ugly Number/README.md +++ b/Algorithms/Ugly Number/README.md @@ -1,7 +1,7 @@ -#[Ugly Number](https://leetcode.com/problems/ugly-number/) -######No:`263` -######Difficulty:`Easy` -##JavaScript +# [Ugly Number](https://leetcode.com/problems/ugly-number/) +###### No:`263` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Valid Anagram/README.md b/Algorithms/Valid Anagram/README.md index 6675140..d1ea03e 100644 --- a/Algorithms/Valid Anagram/README.md +++ b/Algorithms/Valid Anagram/README.md @@ -1,7 +1,7 @@ -#[Valid Anagram](https://leetcode.com/problems/valid-anagram/) -######No:`242` -######Difficulty:`Easy` -##JavaScript +# [Valid Anagram](https://leetcode.com/problems/valid-anagram/) +###### No:`242` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Valid Palindrome/README.md b/Algorithms/Valid Palindrome/README.md index 2a72f80..1583e59 100644 --- a/Algorithms/Valid Palindrome/README.md +++ b/Algorithms/Valid Palindrome/README.md @@ -1,7 +1,7 @@ -#[Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) -######No:`125` -######Difficulty:`Easy` -##JavaScript +# [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) +###### No:`125` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Valid Parentheses/README.md b/Algorithms/Valid Parentheses/README.md index 54a82b4..6a24feb 100644 --- a/Algorithms/Valid Parentheses/README.md +++ b/Algorithms/Valid Parentheses/README.md @@ -1,7 +1,7 @@ -#[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) -######No:`20` -######Difficulty:`Easy` -##JavaScript +# [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) +###### No:`20` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/Valid Sudoku/README.md b/Algorithms/Valid Sudoku/README.md index 2ea5736..323b9cd 100644 --- a/Algorithms/Valid Sudoku/README.md +++ b/Algorithms/Valid Sudoku/README.md @@ -1,7 +1,7 @@ -#[Valid Sudoku](https://leetcode.com/problems/valid-sudoku/) -######No:`36` -######Difficulty:`Easy` -##JavaScript +# [Valid Sudoku](https://leetcode.com/problems/valid-sudoku/) +###### No:`36` +###### Difficulty:`Easy` +## JavaScript ```javascript /** diff --git a/Algorithms/ZigZag Conversion/README.md b/Algorithms/ZigZag Conversion/README.md index 8496ac0..1906b42 100644 --- a/Algorithms/ZigZag Conversion/README.md +++ b/Algorithms/ZigZag Conversion/README.md @@ -1,7 +1,7 @@ -#[ZigZag Conversion](https://leetcode.com/problems/zigzag-conversion/) -######No:`6` -######Difficulty:`Easy` -##JavaScript +# [ZigZag Conversion](https://leetcode.com/problems/zigzag-conversion/) +###### No:`6` +###### Difficulty:`Easy` +## JavaScript ```javascript /** @@ -33,7 +33,7 @@ var convert = function(s, numRows) { return ret; }; ``` -###How to solve? +### How to solve? ``` 1 9 17 2 8 10 16 18 @@ -42,9 +42,9 @@ var convert = function(s, numRows) { 5 13 ``` -######1.Create `arrs = [[],[],[],[],[]]` +###### 1.Create `arrs = [[],[],[],[],[]]` -######2.traverse `chars` than save the every char like the following: +###### 2.traverse `chars` than save the every char like the following: arrs[0] = [1, 9, 17]; arrs[1] = [2, 8, 10, 16, 18]; arrs[2] = [3, 7, 11, 15]; From e44c33bbe1002ae6fb9c04f62fe8d0f04bf87d2f Mon Sep 17 00:00:00 2001 From: Teng DU Date: Tue, 21 Mar 2017 01:06:22 +0800 Subject: [PATCH 053/126] update --- README.md | 6 ++++++ generate.js | 6 +++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7964dad..b041793 100644 --- a/README.md +++ b/README.md @@ -10,13 +10,19 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |7|Reverse Integer|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Integer)|Easy| |8|String to Integer (atoi)|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/String%20to%20Integer%20(atoi))|Easy| |9|Palindrome Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Palindrome%20Number)|Easy| +|12|Integer to Roman|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Integer%20to%20Roman)|Medium| |13|Roman to Integer|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Roman%20to%20Integer)|Easy| +|15|3SumAdd Digits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/3SumAdd%20Digits)|Medium| +|16|3Sum Closest|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/3Sum%20Closest)|Medium| +|17|Letter Combinations of a Phone Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Letter%20Combinations%20of%20a%20Phone%20Number)|Medium| |19|Remove Nth Node From End of List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Nth%20Node%20From%20End%20of%20List)|Easy| |20|Valid Parentheses|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Parentheses)|Easy| |21|Merge Two Sorted Lists|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Merge%20Two%20Sorted%20Lists)|Easy| +|22|Generate Parentheses|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Generate%20Parentheses)|Medium| |24|Swap Nodes in Pairs|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Swap%20Nodes%20in%20Pairs)|Easy| |26|Remove Duplicates from Sorted Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Duplicates%20from%20Sorted%20Array)|Easy| |27|Remove Element|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Element)|Easy| +|28|Implement strStr()|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Implement%20strStr())|Easy| |36|Valid Sudoku|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Sudoku)|Easy| |46|Permutations|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Permutations)|Medium| |66|Plus One|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Plus%20One)|Easy| diff --git a/generate.js b/generate.js index 50c36cc..2a363de 100644 --- a/generate.js +++ b/generate.js @@ -10,9 +10,9 @@ targetDirs = fs.readdir(dir, function(err, files) { var count = files.length; files.forEach(function(value, index, arr) { fs.readFile(dir + '/' + value + '/README.md', {encoding: 'utf8'}, function(err, data) { - var no = parseInt(data.match(/######No:`(\d+)`/)[1], 10); - var difficulty = data.match(/######Difficulty:`(\w+)`/)[1]; - var title = data.match(/#\[(.*)\]/)[1]; + var no = parseInt(data.match(/###### No:`(\d+)`/)[1], 10); + var difficulty = data.match(/###### Difficulty:`(\w+)`/)[1]; + var title = data.match(/# \[(.*)\]/)[1]; var link = '[JavaScript](' + repo_url + title.replace(/\s/g, '%20') + ')'; ret.push({ no: no, From 4adbfba85e55690749d56981b5ec801214f1a710 Mon Sep 17 00:00:00 2001 From: Teng DU Date: Tue, 21 Mar 2017 01:10:36 +0800 Subject: [PATCH 054/126] update --- Algorithms/3Sum/README.md | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Algorithms/3Sum/README.md b/Algorithms/3Sum/README.md index 307dde7..e5fa761 100644 --- a/Algorithms/3Sum/README.md +++ b/Algorithms/3Sum/README.md @@ -1,4 +1,4 @@ -# [3SumAdd Digits](https://leetcode.com/problems/3sum/) +# [3Sum](https://leetcode.com/problems/3sum/) ###### No:`15` ###### Difficulty:`Medium` ## JavaScript diff --git a/README.md b/README.md index b041793..a97154c 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |9|Palindrome Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Palindrome%20Number)|Easy| |12|Integer to Roman|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Integer%20to%20Roman)|Medium| |13|Roman to Integer|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Roman%20to%20Integer)|Easy| -|15|3SumAdd Digits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/3SumAdd%20Digits)|Medium| +|15|3Sum|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/3Sum)|Medium| |16|3Sum Closest|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/3Sum%20Closest)|Medium| |17|Letter Combinations of a Phone Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Letter%20Combinations%20of%20a%20Phone%20Number)|Medium| |19|Remove Nth Node From End of List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Nth%20Node%20From%20End%20of%20List)|Easy| From a71aa04c685d8b2065b3c5f187b2c5f396002710 Mon Sep 17 00:00:00 2001 From: Teng DU Date: Tue, 21 Mar 2017 02:41:34 +0800 Subject: [PATCH 055/126] add basics --- generate-md.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 23 +++++++++++++++++++++ request-problem.js | 37 ++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 generate-md.js create mode 100644 package.json create mode 100644 request-problem.js diff --git a/generate-md.js b/generate-md.js new file mode 100644 index 0000000..2a363de --- /dev/null +++ b/generate-md.js @@ -0,0 +1,50 @@ +var fs = require('fs'); +var events = require('events'); +var proxy = new events.EventEmitter(); + +var dir = './Algorithms'; +var repo_url = 'https://github.com/duteng/leetcode/tree/master/Algorithms/'; +var ret = []; + +targetDirs = fs.readdir(dir, function(err, files) { + var count = files.length; + files.forEach(function(value, index, arr) { + fs.readFile(dir + '/' + value + '/README.md', {encoding: 'utf8'}, function(err, data) { + var no = parseInt(data.match(/###### No:`(\d+)`/)[1], 10); + var difficulty = data.match(/###### Difficulty:`(\w+)`/)[1]; + var title = data.match(/# \[(.*)\]/)[1]; + var link = '[JavaScript](' + repo_url + title.replace(/\s/g, '%20') + ')'; + ret.push({ + no: no, + title:title, + difficulty: difficulty, + link: link + }); + if(ret.length == count) { + proxy.emit('readDone'); + } + + }); + }); +}); + +proxy.once('readDone', function() { + ret.sort(function(a, b) { + return a.no - b.no; + }); + + var content = [ + 'leetcode', + '========', + 'My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/)', + '', + '', + '| No | Title | Source Code | Difficulty |', + '|----| ----- | -------- | ---------- |' + ].join('\r\n') + '\r\n'; + + for(var i = 0; i < ret.length; i++) { + content += '|' + ret[i].no + '|' + ret[i].title + '|' + ret[i].link + '|' + ret[i].difficulty + '|' + '\r\n'; + } + fs.writeFile('README.md', content); +}); diff --git a/package.json b/package.json new file mode 100644 index 0000000..12852d1 --- /dev/null +++ b/package.json @@ -0,0 +1,23 @@ +{ + "name": "request-leetcode", + "version": "1.0.0", + "description": "leetcode\r ========\r My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/)", + "main": "request.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/duteng/leedcode.git" + }, + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/duteng/leedcode/issues" + }, + "homepage": "https://github.com/duteng/leedcode#readme", + "dependencies": { + "cheerio": "^0.22.0", + "request": "^2.81.0" + } +} diff --git a/request-problem.js b/request-problem.js new file mode 100644 index 0000000..da56424 --- /dev/null +++ b/request-problem.js @@ -0,0 +1,37 @@ +var fs = require('fs'); +var request = require('request'); +var cheerio = require('cheerio'); + +var urls = [ + 'https://leetcode.com/problems/copy-list-with-random-pointer/#/description', +]; + +urls.forEach((url) => { + request(url, (err, response, body) => { + var $ = cheerio.load(body); + var textArr = $('.question-title h3').text().trim().split('. '); + var no = textArr[0]; + var title = textArr[1]; + var difficulty = $('.question-info li').eq(2).text().split(': ').pop(); + + var folderPath = `Algorithms/${title}`; + var filePath = `${folderPath}/README.md`; + + fs.mkdir(folderPath, (err) => { + fs.open(filePath, 'wx', (err) => { + var content = [ + `# [${title}](${url})`, + `###### No:\`${no}\``, + `###### Difficulty:\`${difficulty}\``, + '## JavaScript', + '', + '', + '```js', + '```', + ].join('\r\n') + '\r\n'; + + fs.writeFile(filePath, content); + }); + }); + }); +}); From e853ed596721acc30d9870c06c102fde8030f45d Mon Sep 17 00:00:00 2001 From: Teng DU Date: Tue, 21 Mar 2017 02:44:13 +0800 Subject: [PATCH 056/126] update ignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index dfe8e2e..3c3629e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -Algorithms/README.md +node_modules From a41743a1b874f6020fce223f6266cbdc84729d2b Mon Sep 17 00:00:00 2001 From: Teng DU Date: Tue, 21 Mar 2017 03:00:29 +0800 Subject: [PATCH 057/126] add --- Algorithms/Arithmetic Slices/README.md | 8 +++ Algorithms/Arranging Coins/README.md | 8 +++ .../Best Time to Buy and Sell Stock/README.md | 8 +++ .../Copy List with Random Pointer/README.md | 8 +++ Algorithms/Count Primes/README.md | 8 +++ Algorithms/Delete Node in a BST/README.md | 8 +++ .../Find All Duplicates in an Array/README.md | 8 +++ .../README.md | 8 +++ Algorithms/First Bad Version/README.md | 8 +++ .../Guess Number Higher or Lower/README.md | 8 +++ Algorithms/Hamming Distance/README.md | 8 +++ Algorithms/Isomorphic Strings/README.md | 8 +++ Algorithms/Linked List Cycle/README.md | 8 +++ .../README.md | 28 +++-------- Algorithms/Nth Digit/README.md | 8 +++ .../Number of Segments in a String/README.md | 8 +++ Algorithms/Path Sum III/README.md | 8 +++ Algorithms/Power of Four/README.md | 8 +++ .../Queue Reconstruction by Height/README.md | 8 +++ .../Repeated Substring Pattern/README.md | 8 +++ .../Reverse Vowels of a String/README.md | 8 +++ Algorithms/Third Maximum Number/README.md | 8 +++ Algorithms/Valid Perfect Square/README.md | 8 +++ README.md | 22 ++++++++ generate.js | 50 ------------------- request-problem.js | 21 +++++++- 26 files changed, 226 insertions(+), 71 deletions(-) create mode 100644 Algorithms/Arithmetic Slices/README.md create mode 100644 Algorithms/Arranging Coins/README.md create mode 100644 Algorithms/Best Time to Buy and Sell Stock/README.md create mode 100644 Algorithms/Copy List with Random Pointer/README.md create mode 100644 Algorithms/Count Primes/README.md create mode 100644 Algorithms/Delete Node in a BST/README.md create mode 100644 Algorithms/Find All Duplicates in an Array/README.md create mode 100644 Algorithms/Find All Numbers Disappeared in an Array/README.md create mode 100644 Algorithms/First Bad Version/README.md create mode 100644 Algorithms/Guess Number Higher or Lower/README.md create mode 100644 Algorithms/Hamming Distance/README.md create mode 100644 Algorithms/Isomorphic Strings/README.md create mode 100644 Algorithms/Linked List Cycle/README.md create mode 100644 Algorithms/Nth Digit/README.md create mode 100644 Algorithms/Number of Segments in a String/README.md create mode 100644 Algorithms/Path Sum III/README.md create mode 100644 Algorithms/Power of Four/README.md create mode 100644 Algorithms/Queue Reconstruction by Height/README.md create mode 100644 Algorithms/Repeated Substring Pattern/README.md create mode 100644 Algorithms/Reverse Vowels of a String/README.md create mode 100644 Algorithms/Third Maximum Number/README.md create mode 100644 Algorithms/Valid Perfect Square/README.md delete mode 100644 generate.js diff --git a/Algorithms/Arithmetic Slices/README.md b/Algorithms/Arithmetic Slices/README.md new file mode 100644 index 0000000..310fb67 --- /dev/null +++ b/Algorithms/Arithmetic Slices/README.md @@ -0,0 +1,8 @@ +# [Arithmetic Slices](https://leetcode.com/problems/arithmetic-slices/#/description) +###### No:`413` +###### Difficulty:`Medium` +## JavaScript + + +```js +``` diff --git a/Algorithms/Arranging Coins/README.md b/Algorithms/Arranging Coins/README.md new file mode 100644 index 0000000..6fdc5ee --- /dev/null +++ b/Algorithms/Arranging Coins/README.md @@ -0,0 +1,8 @@ +# [Arranging Coins](https://leetcode.com/problems/arranging-coins/#/description) +###### No:`441` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/Algorithms/Best Time to Buy and Sell Stock/README.md b/Algorithms/Best Time to Buy and Sell Stock/README.md new file mode 100644 index 0000000..119d06f --- /dev/null +++ b/Algorithms/Best Time to Buy and Sell Stock/README.md @@ -0,0 +1,8 @@ +# [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/#/description) +###### No:`121` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/Algorithms/Copy List with Random Pointer/README.md b/Algorithms/Copy List with Random Pointer/README.md new file mode 100644 index 0000000..f5b0e1c --- /dev/null +++ b/Algorithms/Copy List with Random Pointer/README.md @@ -0,0 +1,8 @@ +# [Copy List with Random Pointer](https://leetcode.com/problems/copy-list-with-random-pointer/#/description) +###### No:`138` +###### Difficulty:`Medium` +## JavaScript + + +```js +``` diff --git a/Algorithms/Count Primes/README.md b/Algorithms/Count Primes/README.md new file mode 100644 index 0000000..72167f1 --- /dev/null +++ b/Algorithms/Count Primes/README.md @@ -0,0 +1,8 @@ +# [Count Primes](https://leetcode.com/problems/count-primes/#/description) +###### No:`204` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/Algorithms/Delete Node in a BST/README.md b/Algorithms/Delete Node in a BST/README.md new file mode 100644 index 0000000..1689034 --- /dev/null +++ b/Algorithms/Delete Node in a BST/README.md @@ -0,0 +1,8 @@ +# [Delete Node in a BST](https://leetcode.com/problems/delete-node-in-a-bst/#/description) +###### No:`450` +###### Difficulty:`Medium` +## JavaScript + + +```js +``` diff --git a/Algorithms/Find All Duplicates in an Array/README.md b/Algorithms/Find All Duplicates in an Array/README.md new file mode 100644 index 0000000..d329ba7 --- /dev/null +++ b/Algorithms/Find All Duplicates in an Array/README.md @@ -0,0 +1,8 @@ +# [Find All Duplicates in an Array](https://leetcode.com/problems/find-all-duplicates-in-an-array/#/description) +###### No:`442` +###### Difficulty:`Medium` +## JavaScript + + +```js +``` diff --git a/Algorithms/Find All Numbers Disappeared in an Array/README.md b/Algorithms/Find All Numbers Disappeared in an Array/README.md new file mode 100644 index 0000000..38e2da6 --- /dev/null +++ b/Algorithms/Find All Numbers Disappeared in an Array/README.md @@ -0,0 +1,8 @@ +# [Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/#/description) +###### No:`448` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/Algorithms/First Bad Version/README.md b/Algorithms/First Bad Version/README.md new file mode 100644 index 0000000..74844a2 --- /dev/null +++ b/Algorithms/First Bad Version/README.md @@ -0,0 +1,8 @@ +# [First Bad Version](https://leetcode.com/problems/first-bad-version/#/description) +###### No:`278` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/Algorithms/Guess Number Higher or Lower/README.md b/Algorithms/Guess Number Higher or Lower/README.md new file mode 100644 index 0000000..498e51b --- /dev/null +++ b/Algorithms/Guess Number Higher or Lower/README.md @@ -0,0 +1,8 @@ +# [Guess Number Higher or Lower](https://leetcode.com/problems/guess-number-higher-or-lower/#/description) +###### No:`374` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/Algorithms/Hamming Distance/README.md b/Algorithms/Hamming Distance/README.md new file mode 100644 index 0000000..99281f6 --- /dev/null +++ b/Algorithms/Hamming Distance/README.md @@ -0,0 +1,8 @@ +# [Hamming Distance](https://leetcode.com/problems/hamming-distance/#/description) +###### No:`461` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/Algorithms/Isomorphic Strings/README.md b/Algorithms/Isomorphic Strings/README.md new file mode 100644 index 0000000..1f7a01f --- /dev/null +++ b/Algorithms/Isomorphic Strings/README.md @@ -0,0 +1,8 @@ +# [Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings/#/description) +###### No:`205` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/Algorithms/Linked List Cycle/README.md b/Algorithms/Linked List Cycle/README.md new file mode 100644 index 0000000..2a43fc4 --- /dev/null +++ b/Algorithms/Linked List Cycle/README.md @@ -0,0 +1,8 @@ +# [Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/#/description) +###### No:`141` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/Algorithms/Minimum Moves to Equal Array Elements/README.md b/Algorithms/Minimum Moves to Equal Array Elements/README.md index 2e83e94..fc60795 100644 --- a/Algorithms/Minimum Moves to Equal Array Elements/README.md +++ b/Algorithms/Minimum Moves to Equal Array Elements/README.md @@ -1,20 +1,8 @@ -# [Minimum Moves to Equal Array Elements](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/) -###### No:`453` -###### Difficulty:`Easy` -## JavaScript - -### Solution: -Increasing n-i elements by 1 is same as decrease 1 elements by 1. -Get the minimum number and calculate the sum of other elements minus minimum. - -```javascript -/** - * @param {number[]} nums - * @return {number} - */ -var minMoves = function(nums) { - var min = Math.min.apply(null, nums) - - return nums.reduce((p, c) => p + c - min, 0); -}; -``` +# [Minimum Moves to Equal Array Elements](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/#/description) +###### No:`453` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/Algorithms/Nth Digit/README.md b/Algorithms/Nth Digit/README.md new file mode 100644 index 0000000..ad2eba1 --- /dev/null +++ b/Algorithms/Nth Digit/README.md @@ -0,0 +1,8 @@ +# [Nth Digit](https://leetcode.com/problems/nth-digit/#/description) +###### No:`400` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/Algorithms/Number of Segments in a String/README.md b/Algorithms/Number of Segments in a String/README.md new file mode 100644 index 0000000..6bf16fd --- /dev/null +++ b/Algorithms/Number of Segments in a String/README.md @@ -0,0 +1,8 @@ +# [Number of Segments in a String](https://leetcode.com/problems/number-of-segments-in-a-string/#/description) +###### No:`434` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/Algorithms/Path Sum III/README.md b/Algorithms/Path Sum III/README.md new file mode 100644 index 0000000..c5cc1f2 --- /dev/null +++ b/Algorithms/Path Sum III/README.md @@ -0,0 +1,8 @@ +# [Path Sum III](https://leetcode.com/problems/path-sum-iii/#/description) +###### No:`437` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/Algorithms/Power of Four/README.md b/Algorithms/Power of Four/README.md new file mode 100644 index 0000000..9906093 --- /dev/null +++ b/Algorithms/Power of Four/README.md @@ -0,0 +1,8 @@ +# [Power of Four](https://leetcode.com/problems/power-of-four/#/description) +###### No:`342` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/Algorithms/Queue Reconstruction by Height/README.md b/Algorithms/Queue Reconstruction by Height/README.md new file mode 100644 index 0000000..1e671e2 --- /dev/null +++ b/Algorithms/Queue Reconstruction by Height/README.md @@ -0,0 +1,8 @@ +# [Queue Reconstruction by Height](https://leetcode.com/problems/queue-reconstruction-by-height/#/description) +###### No:`406` +###### Difficulty:`Medium` +## JavaScript + + +```js +``` diff --git a/Algorithms/Repeated Substring Pattern/README.md b/Algorithms/Repeated Substring Pattern/README.md new file mode 100644 index 0000000..b808c21 --- /dev/null +++ b/Algorithms/Repeated Substring Pattern/README.md @@ -0,0 +1,8 @@ +# [Repeated Substring Pattern](https://leetcode.com/problems/repeated-substring-pattern/#/description) +###### No:`459` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/Algorithms/Reverse Vowels of a String/README.md b/Algorithms/Reverse Vowels of a String/README.md new file mode 100644 index 0000000..f389254 --- /dev/null +++ b/Algorithms/Reverse Vowels of a String/README.md @@ -0,0 +1,8 @@ +# [Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/#/description) +###### No:`345` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/Algorithms/Third Maximum Number/README.md b/Algorithms/Third Maximum Number/README.md new file mode 100644 index 0000000..4f7072f --- /dev/null +++ b/Algorithms/Third Maximum Number/README.md @@ -0,0 +1,8 @@ +# [Third Maximum Number](https://leetcode.com/problems/third-maximum-number/#/description) +###### No:`414` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/Algorithms/Valid Perfect Square/README.md b/Algorithms/Valid Perfect Square/README.md new file mode 100644 index 0000000..1edf8fd --- /dev/null +++ b/Algorithms/Valid Perfect Square/README.md @@ -0,0 +1,8 @@ +# [Valid Perfect Square](https://leetcode.com/problems/valid-perfect-square/#/description) +###### No:`367` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/README.md b/README.md index a97154c..b959100 100644 --- a/README.md +++ b/README.md @@ -41,9 +41,12 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |112|Path Sum|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Path%20Sum)|Easy| |118|Pascal's Triangle|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Pascal's%20Triangle)|Easy| |119|Pascal's Triangle II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Pascal's%20Triangle%20II)|Easy| +|121|Best Time to Buy and Sell Stock|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Best%20Time%20to%20Buy%20and%20Sell%20Stock)|Easy| |122|Best Time to Buy and Sell Stock II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Best%20Time%20to%20Buy%20and%20Sell%20Stock%20II)|Medium| |125|Valid Palindrome|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Palindrome)|Easy| |136|Single Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Single%20Number)|Medium| +|138|Copy List with Random Pointer|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Copy%20List%20with%20Random%20Pointer)|Medium| +|141|Linked List Cycle|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Linked%20List%20Cycle)|Easy| |144|Binary Tree Preorder Traversal|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Binary%20Tree%20Preorder%20Traversal)|Medium| |148|Sort List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Sort%20List)|Medium| |149|Max Points on a Line|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Max%20Points%20on%20a%20Line)|Hard| @@ -62,6 +65,8 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |198|House Robber|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/House%20Robber)|Easy| |202|Happy Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Happy%20Number)|Easy| |203|Remove Linked List Elements|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Linked%20List%20Elements)|Easy| +|204|Count Primes|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Count%20Primes)|Easy| +|205|Isomorphic Strings|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Isomorphic%20Strings)|Easy| |206|Reverse Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Linked%20List)|Easy| |217|Contains Duplicate|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Contains%20Duplicate)|Easy| |219|Contains Duplicate II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Contains%20Duplicate%20II)|Easy| @@ -79,26 +84,43 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |258|Add Digits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Add%20Digits)|Easy| |263|Ugly Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Ugly%20Number)|Easy| |268|Missing Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Missing%20Number)|Medium| +|278|First Bad Version|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/First%20Bad%20Version)|Easy| |283|Move Zeroes|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Move%20Zeroes)|Easy| |292|Nim Game|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Nim%20Game)|Easy| |319|Add Digits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Add%20Digits)|Medium| |326|Power of Three|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Power%20of%20Three)|Easy| |328|Odd Even Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Odd%20Even%20Linked%20List)|Medium| |338|Counting Bits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Counting%20Bits)|Medium| +|342|Power of Four|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Power%20of%20Four)|Easy| |343|Integer Break|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Integer%20Break)|Medium| |344|Reverse String|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20String)|Easy| +|345|Reverse Vowels of a String|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Vowels%20of%20a%20String)|Easy| |347|Top K Frequent Elements|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Top%20K%20Frequent%20Elements)|Medium| |349|Intersection of Two Arrays|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Intersection%20of%20Two%20Arrays)|Easy| |350|Intersection of Two Arrays II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Intersection%20of%20Two%20Arrays%20II)|Easy| +|367|Valid Perfect Square|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Perfect%20Square)|Easy| +|374|Guess Number Higher or Lower|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Guess%20Number%20Higher%20or%20Lower)|Easy| |383|Ransom Note|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Ransom%20Note)|Easy| |387|First Unique Character in a String|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/First%20Unique%20Character%20in%20a%20String)|Easy| |389|Find the Difference|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Find%20the%20Difference)|Easy| +|400|Nth Digit|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Nth%20Digit)|Easy| |401|Binary Watch|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Binary%20Watch)|Easy| |404|Sum of Left Leaves|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Sum%20of%20Left%20Leaves)|Easy| +|406|Queue Reconstruction by Height|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Queue%20Reconstruction%20by%20Height)|Medium| |409|Longest Palindrome|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Longest%20Palindrome)|Easy| |412|Fizz Buzz|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Fizz%20Buzz)|Easy| +|413|Arithmetic Slices|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Arithmetic%20Slices)|Medium| +|414|Third Maximum Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Third%20Maximum%20Number)|Easy| |415|Add Strings|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Add%20Strings)|Easy| +|434|Number of Segments in a String|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Number%20of%20Segments%20in%20a%20String)|Easy| +|437|Path Sum III|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Path%20Sum%20III)|Easy| +|441|Arranging Coins|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Arranging%20Coins)|Easy| +|442|Find All Duplicates in an Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Find%20All%20Duplicates%20in%20an%20Array)|Medium| |447|Number of Boomerangs|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Number%20of%20Boomerangs)|Easy| +|448|Find All Numbers Disappeared in an Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Find%20All%20Numbers%20Disappeared%20in%20an%20Array)|Easy| +|450|Delete Node in a BST|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Delete%20Node%20in%20a%20BST)|Medium| |453|Minimum Moves to Equal Array Elements|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Minimum%20Moves%20to%20Equal%20Array%20Elements)|Easy| |455|Assign Cookies|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Assign%20Cookies)|Easy| +|459|Repeated Substring Pattern|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Repeated%20Substring%20Pattern)|Easy| +|461|Hamming Distance|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Hamming%20Distance)|Easy| |463|Add Digits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Add%20Digits)|Easy| diff --git a/generate.js b/generate.js deleted file mode 100644 index 2a363de..0000000 --- a/generate.js +++ /dev/null @@ -1,50 +0,0 @@ -var fs = require('fs'); -var events = require('events'); -var proxy = new events.EventEmitter(); - -var dir = './Algorithms'; -var repo_url = 'https://github.com/duteng/leetcode/tree/master/Algorithms/'; -var ret = []; - -targetDirs = fs.readdir(dir, function(err, files) { - var count = files.length; - files.forEach(function(value, index, arr) { - fs.readFile(dir + '/' + value + '/README.md', {encoding: 'utf8'}, function(err, data) { - var no = parseInt(data.match(/###### No:`(\d+)`/)[1], 10); - var difficulty = data.match(/###### Difficulty:`(\w+)`/)[1]; - var title = data.match(/# \[(.*)\]/)[1]; - var link = '[JavaScript](' + repo_url + title.replace(/\s/g, '%20') + ')'; - ret.push({ - no: no, - title:title, - difficulty: difficulty, - link: link - }); - if(ret.length == count) { - proxy.emit('readDone'); - } - - }); - }); -}); - -proxy.once('readDone', function() { - ret.sort(function(a, b) { - return a.no - b.no; - }); - - var content = [ - 'leetcode', - '========', - 'My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/)', - '', - '', - '| No | Title | Source Code | Difficulty |', - '|----| ----- | -------- | ---------- |' - ].join('\r\n') + '\r\n'; - - for(var i = 0; i < ret.length; i++) { - content += '|' + ret[i].no + '|' + ret[i].title + '|' + ret[i].link + '|' + ret[i].difficulty + '|' + '\r\n'; - } - fs.writeFile('README.md', content); -}); diff --git a/request-problem.js b/request-problem.js index da56424..9f8c340 100644 --- a/request-problem.js +++ b/request-problem.js @@ -3,7 +3,24 @@ var request = require('request'); var cheerio = require('cheerio'); var urls = [ - 'https://leetcode.com/problems/copy-list-with-random-pointer/#/description', + 'https://leetcode.com/problems/nth-digit/#/description', + 'https://leetcode.com/problems/queue-reconstruction-by-height/#/description', + 'https://leetcode.com/problems/arithmetic-slices/#/description', + 'https://leetcode.com/problems/third-maximum-number/#/description', + 'https://leetcode.com/problems/number-of-segments-in-a-string/#/description', + 'https://leetcode.com/problems/path-sum-iii/#/description', + 'https://leetcode.com/problems/arranging-coins/#/description', + 'https://leetcode.com/problems/find-all-duplicates-in-an-array/#/description', + 'https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/#/description', + 'https://leetcode.com/problems/delete-node-in-a-bst/#/description', + 'https://leetcode.com/problems/minimum-moves-to-equal-array-elements/#/description', + 'https://leetcode.com/problems/repeated-substring-pattern/#/description', + 'https://leetcode.com/problems/hamming-distance/#/description', + 'https://leetcode.com/problems/first-bad-version/#/description', + 'https://leetcode.com/problems/power-of-four/#/description', + 'https://leetcode.com/problems/reverse-vowels-of-a-string/#/description', + 'https://leetcode.com/problems/valid-perfect-square/#/description', + 'https://leetcode.com/problems/guess-number-higher-or-lower/#/description' ]; urls.forEach((url) => { @@ -17,6 +34,8 @@ urls.forEach((url) => { var folderPath = `Algorithms/${title}`; var filePath = `${folderPath}/README.md`; + console.log(no, title, difficulty); + fs.mkdir(folderPath, (err) => { fs.open(filePath, 'wx', (err) => { var content = [ From 0c03bb66ece7553ebcd203ddb3cfef099fe44113 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:02:07 +0800 Subject: [PATCH 058/126] Update README.md --- Algorithms/Arithmetic Slices/README.md | 27 ++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Algorithms/Arithmetic Slices/README.md b/Algorithms/Arithmetic Slices/README.md index 310fb67..5d7102a 100644 --- a/Algorithms/Arithmetic Slices/README.md +++ b/Algorithms/Arithmetic Slices/README.md @@ -5,4 +5,31 @@ ```js +/** + * @param {number[]} A + * @return {number} + */ +var count = function(len) { + return len < 3 ? 0 : (1+ len - 2) * (len - 2) / 2; +}; + +var numberOfArithmeticSlices = function(A) { + if (A.length < 3) return 0; + var len = 2; + var diff = A[1] - A[0]; + var ret = 0; + + for (var i = 1; i < A.length - 1; i++) { + if (diff === A[i + 1] - A[i]) { + len++ + } else { + ret += count(len); + diff = A[i + 1] - A[i]; + len = 2; + } + } + ret += count(len); + + return ret; +}; ``` From 5fb9ebd0cbf282501ecdfc36ced771eefd43a314 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:02:36 +0800 Subject: [PATCH 059/126] Update README.md --- Algorithms/Arranging Coins/README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Algorithms/Arranging Coins/README.md b/Algorithms/Arranging Coins/README.md index 6fdc5ee..c3d93e6 100644 --- a/Algorithms/Arranging Coins/README.md +++ b/Algorithms/Arranging Coins/README.md @@ -5,4 +5,26 @@ ```js +/** + * @param {number} n + * @return {number} + */ +var arrangeCoins = function(n) { + if (n === 0) {return 0;} + var start = 0; + var end = n; + var middle = parseInt((start + end + 1) / 2); + + while (start <= end) { + if ((1 + middle) * middle / 2 <= n && n < (2 + middle) * (middle + 1) / 2) { + return middle; + } else if ((2 + middle) * (middle + 1) / 2 <= n) { + start = middle + 1; + } else if ((1 + middle) * middle / 2 > n) { + end = middle - 1; + } + middle = parseInt((start + end) / 2); + } +}; + ``` From 8ed8dd22ef0dea8ca17a32c7894c5a84e03863b4 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:03:06 +0800 Subject: [PATCH 060/126] Update README.md --- .../Best Time to Buy and Sell Stock/README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Algorithms/Best Time to Buy and Sell Stock/README.md b/Algorithms/Best Time to Buy and Sell Stock/README.md index 119d06f..3696190 100644 --- a/Algorithms/Best Time to Buy and Sell Stock/README.md +++ b/Algorithms/Best Time to Buy and Sell Stock/README.md @@ -5,4 +5,19 @@ ```js +/** + * @param {number[]} prices + * @return {number} + */ +var maxProfit = function(prices) { + var min = Number.MAX_VALUE; + var profit = 0; + + for(var i = 0; i < prices.length; i++) { + min = Math.min(min, prices[i]); + profit = Math.max(profit, prices[i] - min); + } + + return profit; +}; ``` From 04dd23814ffb13e374df9e45e474be6e6f6628e4 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:03:46 +0800 Subject: [PATCH 061/126] Update README.md --- .../Copy List with Random Pointer/README.md | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/Algorithms/Copy List with Random Pointer/README.md b/Algorithms/Copy List with Random Pointer/README.md index f5b0e1c..9ea3cfe 100644 --- a/Algorithms/Copy List with Random Pointer/README.md +++ b/Algorithms/Copy List with Random Pointer/README.md @@ -5,4 +5,78 @@ ```js +/** + * Definition for singly-linked list with a random pointer. + * function RandomListNode(label) { + * this.label = label; + * this.next = this.random = null; + * } + */ + +/** + * @param {RandomListNode} head + * @return {RandomListNode} + */ + +var copyLinkList = function(head) { + if (head === null) { + return null; + } + + var new_head = new RandomListNode(head.label); + var new_current = new_head; + var node; + + head = head.next; + while(head !== null) { + node = new RandomListNode(head.label); + new_current.next = node; + new_current = node; + head = head.next; + } + + return new_head; +}; + +var copyRandomList = function(head) { + if (head === null) { + return null; + } + + var new_head = copyLinkList(head); + var new_list = []; + var old_list = []; + + var new_curr = new_head; + var old_curr = head; + while (new_curr !== null) { + new_list.push(new_curr); + old_list.push(old_curr); + new_curr = new_curr.next; + old_curr = old_curr.next; + } + + for (var i = 0; i < new_list.length; i++) { + new_list[i].random = old_list[i]; + old_list[i].next = new_list[i]; + } + + for (i = 0; i < new_list.length; i++) { + if (old_list[i].random === null) { + new_list[i].random = null; + } else { + new_list[i].random = old_list[i].random.next; + } + } + + for (i = 0; i < old_list.length - 1; i++) { + old_list[i].next = old_list[i + 1]; + } + old_list[old_list.length - 1].next = null; + + + return new_head; +}; + + ``` From fe6e1161bbb26f05fe32786f019e72a79ddf0ba2 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:04:12 +0800 Subject: [PATCH 062/126] Update README.md --- Algorithms/Count Primes/README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/Algorithms/Count Primes/README.md b/Algorithms/Count Primes/README.md index 72167f1..bc47fe8 100644 --- a/Algorithms/Count Primes/README.md +++ b/Algorithms/Count Primes/README.md @@ -5,4 +5,32 @@ ```js +/** + * @param {number} n + * @return {number} + */ +var countPrimes = function(n) { + var isPrime = []; + for (var i = 2; i < n; i++) { + isPrime[i] = true; + } + + for (var i = 2; i * i < n; i++) { + if (isPrime[i]) { + for (var j = i * i; j < n; j += i) { + isPrime[j] = false; + } + } + } + + var count = 0; + for (var i = 2; i < n; i++) { + if (isPrime[i]) { + count++; + } + } + + return count; +}; + ``` From b5219dfa44bd2e2b233852fdab501ac585aa2cb6 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:04:50 +0800 Subject: [PATCH 063/126] Update README.md --- Algorithms/Delete Node in a BST/README.md | 76 +++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/Algorithms/Delete Node in a BST/README.md b/Algorithms/Delete Node in a BST/README.md index 1689034..860c1c6 100644 --- a/Algorithms/Delete Node in a BST/README.md +++ b/Algorithms/Delete Node in a BST/README.md @@ -5,4 +5,80 @@ ```js +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @param {number} key + * @return {TreeNode} + */ +var update = function(parent, direction) { + if (parent === null) { + return; + } + var deleteNode = parent[direction]; + if (deleteNode.right) { + parent[direction] = deleteNode.right; + + var leftIsNullNode = deleteNode.right; + while(leftIsNullNode.left !== null) leftIsNullNode = leftIsNullNode.left; + leftIsNullNode.left = deleteNode.left; + return parent; + } + if(deleteNode.left) { + parent[direction] = deleteNode.left; + return parent; + } + parent[direction] = null; + return parent; +}; + +var search = function(root, direction, key, ret) { + var node = root[direction]; + if (node === null) { + return; + } + + if (node.val === key) { + ret.parent = root; + ret.direction = direction; + } else if (node.val > key) { + search(node, 'left', key, ret); + } else { + search(node, 'right', key, ret); + } +}; + +var deleteNode = function(root, key) { + if (root === null) { + return null; + } + + if (root.val === key) { + if (root.right) { + var leftIsNullNode = root.right; + while(leftIsNullNode.left !== null) leftIsNullNode = leftIsNullNode.left; + + leftIsNullNode.left = root.left; + return root.right; + } + return root.left; + } + + var ret = { + parent: null, + direction: null, + }; + search(root, 'left', key, ret); + search(root, 'right', key, ret); + update(ret.parent, ret.direction); + + return root; +}; + ``` From 63b3d1df4d953bb94f90a87ce0275f3b794fe848 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:05:30 +0800 Subject: [PATCH 064/126] Update README.md --- .../Find All Duplicates in an Array/README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Algorithms/Find All Duplicates in an Array/README.md b/Algorithms/Find All Duplicates in an Array/README.md index d329ba7..5fa4849 100644 --- a/Algorithms/Find All Duplicates in an Array/README.md +++ b/Algorithms/Find All Duplicates in an Array/README.md @@ -5,4 +5,20 @@ ```js +/** + * @param {number[]} nums + * @return {number[]} + */ +var findDuplicates = function(nums) { + var ret = []; + for (var i = 0; i < nums.length; i++) { + var index = Math.abs(nums[i]) - 1; + if (nums[index] < 0) { + ret.push(Math.abs(nums[i])); + } + nums[index] = -Math.abs(nums[index]); + } + + return ret; +}; ``` From 45e06d53157c41dbcd81dcbcecdf2345ec125ceb Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:05:54 +0800 Subject: [PATCH 065/126] Update README.md --- .../README.md | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Algorithms/Find All Numbers Disappeared in an Array/README.md b/Algorithms/Find All Numbers Disappeared in an Array/README.md index 38e2da6..3752028 100644 --- a/Algorithms/Find All Numbers Disappeared in an Array/README.md +++ b/Algorithms/Find All Numbers Disappeared in an Array/README.md @@ -5,4 +5,24 @@ ```js +/** + * @param {number[]} nums + * @return {number[]} + */ +var findDisappearedNumbers = function(nums) { + var map = {}; + var ret = []; + + nums.forEach(function(num) { + map[num] = true; + }); + + for (var i = 1; i <= nums.length; i++) { + if (map[i] === undefined) { + ret.push(i); + } + } + + return ret; +}; ``` From c4a1dc502e85655d7465d2e3341b9d4669852052 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:06:28 +0800 Subject: [PATCH 066/126] Update README.md --- Algorithms/First Bad Version/README.md | 43 ++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/Algorithms/First Bad Version/README.md b/Algorithms/First Bad Version/README.md index 74844a2..fa74904 100644 --- a/Algorithms/First Bad Version/README.md +++ b/Algorithms/First Bad Version/README.md @@ -5,4 +5,47 @@ ```js +/** + * Definition for isBadVersion() + * + * @param {integer} version number + * @return {boolean} whether the version is bad + * isBadVersion = function(version) { + * ... + * }; + */ + +/** + * @param {function} isBadVersion() + * @return {function} + */ +var solution = function(isBadVersion) { + /** + * @param {integer} n Total versions + * @return {integer} The first bad version + */ + return function(n) { + var start = 1; + var end = n; + var middle; + while (start <= end) { + middle = start + Math.floor((end - start) / 2); + if (middle + 1 <= n && !isBadVersion(middle) && isBadVersion(middle + 1)) { + return middle + 1; + } + if (middle - 1 > 0 && !isBadVersion(middle - 1) && isBadVersion(middle)) { + return middle + } + if (middle === 1 && isBadVersion(middle)) { + return middle; + } + if (isBadVersion(middle)) { + end = middle - 1; + } else { + start = middle + 1; + } + } + return 'null'; + }; +}; ``` From 6b1215117b7401b4241761bcb4531806be4d1f65 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:07:02 +0800 Subject: [PATCH 067/126] Update README.md --- .../Guess Number Higher or Lower/README.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Algorithms/Guess Number Higher or Lower/README.md b/Algorithms/Guess Number Higher or Lower/README.md index 498e51b..156c9b3 100644 --- a/Algorithms/Guess Number Higher or Lower/README.md +++ b/Algorithms/Guess Number Higher or Lower/README.md @@ -5,4 +5,28 @@ ```js +/* The guess API is defined in the parent class GuessGame. + @param num, your guess + @return -1 if my number is lower, 1 if my number is higher, otherwise return 0 + int guess(int num); */ + +public class Solution extends GuessGame { + public int guessNumber(int n) { + int start = 1; + int end = n; + int middle; + + while (start <= end) { + middle = (end - start) / 2 + start; + if (guess(middle) == 0) { + return middle; + } else if (guess(middle) > 0) { + start = middle + 1; + } else { + end = middle - 1; + } + } + return 0; + } +} ``` From 25bad41b0acae3e33639120f8512cc1945e6a557 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:07:24 +0800 Subject: [PATCH 068/126] Update README.md --- Algorithms/Hamming Distance/README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Algorithms/Hamming Distance/README.md b/Algorithms/Hamming Distance/README.md index 99281f6..a382dfa 100644 --- a/Algorithms/Hamming Distance/README.md +++ b/Algorithms/Hamming Distance/README.md @@ -5,4 +5,26 @@ ```js +/** + * @param {number} x + * @param {number} y + * @return {number} + */ + +// Number of 1 Bits +var hammingWeight = function(n) { + var ret = 0; + for(var power = 32; power >= 0; power--) { + var exponent = Math.pow(2, power); + if (n >= exponent) { + ret++; + n -= exponent; + } + } + return ret; +}; + +var hammingDistance = function(x, y) { + return hammingWeight(x ^ y); +}; ``` From 1ccb886cbf72639ed188f44712232a393097ce21 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:07:52 +0800 Subject: [PATCH 069/126] Update README.md --- Algorithms/Isomorphic Strings/README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Algorithms/Isomorphic Strings/README.md b/Algorithms/Isomorphic Strings/README.md index 1f7a01f..49a55d0 100644 --- a/Algorithms/Isomorphic Strings/README.md +++ b/Algorithms/Isomorphic Strings/README.md @@ -5,4 +5,28 @@ ```js +/** + * @param {string} s + * @param {string} t + * @return {boolean} + */ +var isIsomorphic = function(s, t) { + if (s.length !== t.length) return false; + + var smap = {}; + var tmap = {}; + + for (var i = 0; i < s.length; i++) { + if (smap[s[i]] === undefined && tmap[t[i]] === undefined) { + smap[s[i]] = t[i]; + tmap[t[i]] = s[i]; + } + + if (smap[s[i]] !== t[i] || tmap[t[i]] !== s[i] ){ + return false; + } + } + + return true; +}; ``` From 9e9b8ffeb250912f616c8088af3b6bace06d7acb Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:08:22 +0800 Subject: [PATCH 070/126] Update README.md --- .../Minimum Moves to Equal Array Elements/README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Algorithms/Minimum Moves to Equal Array Elements/README.md b/Algorithms/Minimum Moves to Equal Array Elements/README.md index fc60795..0f358a9 100644 --- a/Algorithms/Minimum Moves to Equal Array Elements/README.md +++ b/Algorithms/Minimum Moves to Equal Array Elements/README.md @@ -5,4 +5,13 @@ ```js +/** + * @param {number[]} nums + * @return {number} + */ +var minMoves = function(nums) { + var min = Math.min.apply(null, nums) + + return nums.reduce((p, c) => p + c - min, 0); +}; ``` From 34a817953a719a309118774c05dd905cc2c53ff1 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:08:42 +0800 Subject: [PATCH 071/126] Update README.md --- Algorithms/Nth Digit/README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Algorithms/Nth Digit/README.md b/Algorithms/Nth Digit/README.md index ad2eba1..ad8fa5f 100644 --- a/Algorithms/Nth Digit/README.md +++ b/Algorithms/Nth Digit/README.md @@ -5,4 +5,30 @@ ```js +/** + * @param {number} n + * @return {number} + */ +var findNthDigit = function(n) { + var length = 1; + var count = 9; + var digits = 9; + + while (n > digits) { + length++; + count *= 10; + digits += length * count; + } + n = n - (digits - length * count); + + var position = Math.ceil(n / length); + var number = Math.pow(10, (length - 1)) + position - 1; + + if (n % length === 0) { + return number % 10; + } else { + return parseInt(String(number)[n % length - 1]); + } +}; + ``` From 5092da10191bc82579bbf3e65f029ac8e5d96f57 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:09:02 +0800 Subject: [PATCH 072/126] Update README.md --- .../Number of Segments in a String/README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Algorithms/Number of Segments in a String/README.md b/Algorithms/Number of Segments in a String/README.md index 6bf16fd..e132f6e 100644 --- a/Algorithms/Number of Segments in a String/README.md +++ b/Algorithms/Number of Segments in a String/README.md @@ -5,4 +5,22 @@ ```js +/** + * @param {string} s + * @return {number} + */ +/* +var countSegments = function(s) { + return s.split(' ').filter((item) => item !== '').length; +}; +*/ + +var countSegments = function(s) { + var count = 0; + for (var i = 0; i < s.length; i++) { + if (s[i] !== ' ' && (i === 0 || s[i - 1] === ' ')) count++; + } + + return count; +}; ``` From 9d8330a356344a3984efba22287ff9f7680a51b9 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:09:23 +0800 Subject: [PATCH 073/126] Update README.md --- Algorithms/Path Sum III/README.md | 34 +++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/Algorithms/Path Sum III/README.md b/Algorithms/Path Sum III/README.md index c5cc1f2..ca1460f 100644 --- a/Algorithms/Path Sum III/README.md +++ b/Algorithms/Path Sum III/README.md @@ -5,4 +5,38 @@ ```js +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @param {number} sum + * @return {number} + */ +var traversal = function(root, list, sum, cb) { + if (root === null) return; + cb(list, root.val); + var newList = list.map((item) => item - root.val).concat(sum); + + traversal(root.left, newList, sum, cb); + traversal(root.right, newList, sum, cb); +}; +var pathSum = function(root, sum) { + var count = 0; + var cb = function (list, val) { + list.forEach((item) => { + if (item === val) { + count++; + } + }); + } + + traversal(root, [sum], sum, cb); + return count; +}; + ``` From 5ba3b6f2493b8cd9565fd118ecb7ec5b5adc756f Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:09:44 +0800 Subject: [PATCH 074/126] Update README.md --- Algorithms/Power of Four/README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Algorithms/Power of Four/README.md b/Algorithms/Power of Four/README.md index 9906093..816eea3 100644 --- a/Algorithms/Power of Four/README.md +++ b/Algorithms/Power of Four/README.md @@ -5,4 +5,17 @@ ```js +/** + * @param {number} num + * @return {boolean} + */ +/* +if (num & (num - 1)) === 0) { + // num only contains only one '1' bit +} +*/ + +var isPowerOfFour = function(num) { + return num > 0 && (num & (num - 1)) === 0 && (num & 0x55555555) !== 0; +}; ``` From f58d6fafc9d13af254e2488d9493426e9f278e44 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:10:05 +0800 Subject: [PATCH 075/126] Update README.md --- .../Queue Reconstruction by Height/README.md | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/Algorithms/Queue Reconstruction by Height/README.md b/Algorithms/Queue Reconstruction by Height/README.md index 1e671e2..e2cf4d5 100644 --- a/Algorithms/Queue Reconstruction by Height/README.md +++ b/Algorithms/Queue Reconstruction by Height/README.md @@ -5,4 +5,37 @@ ```js +/** + * @param {number[][]} people + * @return {number[][]} + */ +var reconstructQueue = function(people) { + if (people.length <= 1) return people; + people.sort((a, b) => { + if (a[0] !== b[0]) { + return b[0] - a[0]; + } else { + return a[1] - b[1]; + } + }); + + var ret = []; + ret.push(people[0]); + + for (var i = 1; i < people.length; i++) { + if (people[i][0] === people[0][0]) { + ret.push(people[i]); + continue; + } + + var count = 0, index = 0; + while (count < people[i][1]) { + if (ret[index][0] >= people[i][0]) count++; + index++; + } + ret.splice(index, 0, people[i]); + } + + return ret; +}; ``` From 262d42cd4054545a48727ac3f81a6ac568204895 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:10:34 +0800 Subject: [PATCH 076/126] Update README.md --- .../Repeated Substring Pattern/README.md | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Algorithms/Repeated Substring Pattern/README.md b/Algorithms/Repeated Substring Pattern/README.md index b808c21..74f3a15 100644 --- a/Algorithms/Repeated Substring Pattern/README.md +++ b/Algorithms/Repeated Substring Pattern/README.md @@ -5,4 +5,30 @@ ```js +/** + * @param {string} s + * @return {boolean} + */ +/* +var repeatedSubstringPattern = function(s) { + if (s.length === 1) return false; + var ret = false; + + for (var i = 1; i <= parseInt((s.length + 1) / 2); i++) { + for (var j = i; j < s.length; j++) { + if (s[j % i] !== s[j]) break; + } + if (j === s.length && (j - 1) % i === i - 1) { + ret = true; + break; + } + } + + return ret; +}; +*/ + +var repeatedSubstringPattern = function(s) { + return /^(.+)\1+$/.test(s); +} ``` From c799a3183724a1bfc98407b7fa0dc25dec383b45 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:11:06 +0800 Subject: [PATCH 077/126] Update README.md --- .../Reverse Vowels of a String/README.md | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Algorithms/Reverse Vowels of a String/README.md b/Algorithms/Reverse Vowels of a String/README.md index f389254..4fd1aa8 100644 --- a/Algorithms/Reverse Vowels of a String/README.md +++ b/Algorithms/Reverse Vowels of a String/README.md @@ -5,4 +5,25 @@ ```js +/** + * @param {string} s + * @return {string} + */ +var reverseVowels = function(s) { + s = s.split(''); + var vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']; + var start = -1; + var end = s.length; + var temp; + + while (start < end) { + while (start < end && vowels.indexOf(s[++start]) === -1); + while (start < end && vowels.indexOf(s[--end]) === -1); + temp = s[start]; + s[start] = s[end]; + s[end] = temp; + } + + return s.join(''); +}; ``` From 6c9c3f22e360e7804cddcc0f3ea73df2e925a81c Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:11:26 +0800 Subject: [PATCH 078/126] Update README.md --- Algorithms/Third Maximum Number/README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Algorithms/Third Maximum Number/README.md b/Algorithms/Third Maximum Number/README.md index 4f7072f..23c813f 100644 --- a/Algorithms/Third Maximum Number/README.md +++ b/Algorithms/Third Maximum Number/README.md @@ -5,4 +5,27 @@ ```js +/** + * @param {number[]} nums + * @return {number} + */ +var getMax = function(nums) { + return Math.max.apply(null, nums); +}; + +var removeItem = function(nums, item) { + return nums.filter((value) => value !== item); +}; + +var thirdMax = function(nums) { + var first = getMax(nums); + nums = removeItem(nums, first); + var second = getMax(nums); + nums = removeItem(nums, second); + + if (nums.length === 0) return first; + return getMax(nums); + +}; + ``` From 0eebf11e1f8587e428a4b485b888ca78c0c06ae1 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:11:49 +0800 Subject: [PATCH 079/126] Update README.md --- Algorithms/Valid Perfect Square/README.md | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Algorithms/Valid Perfect Square/README.md b/Algorithms/Valid Perfect Square/README.md index 1edf8fd..9ed3931 100644 --- a/Algorithms/Valid Perfect Square/README.md +++ b/Algorithms/Valid Perfect Square/README.md @@ -5,4 +5,29 @@ ```js +/** + * @param {number} num + * @return {boolean} + */ +var isPerfectSquare = function(num) { + if (num < 1) return false; + if (num === 1) return true; + + var start = 0; + var end = num; + var middle = num / 2; + + while (start <= end) { + if (parseInt(num / middle) === middle && num % middle === 0) { + return true; + } else if (num / middle > middle) { + start = middle + 1; + } else if (num / middle < middle) { + end = middle - 1; + } + middle = parseInt((start + end) / 2); + } + + return false; +}; ``` From 38db553da0e02b5c8959862dd8b67af5dbf6c262 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:12:25 +0800 Subject: [PATCH 080/126] Update README.md --- Algorithms/Linked List Cycle/README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Algorithms/Linked List Cycle/README.md b/Algorithms/Linked List Cycle/README.md index 2a43fc4..8c5777a 100644 --- a/Algorithms/Linked List Cycle/README.md +++ b/Algorithms/Linked List Cycle/README.md @@ -5,4 +5,29 @@ ```js +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ + +/** + * @param {ListNode} head + * @return {boolean} + */ +var hasCycle = function(head) { + if (head === null) {return false;} + var fast = head; + var slow = head; + + while(slow.next !== null && slow.next.next !== null) { + fast = fast.next; + slow = slow.next.next; + if (slow === fast) return true; + } + + return false; +}; ``` From 6149947d141d4d745fa298944daa13577e2ce492 Mon Sep 17 00:00:00 2001 From: Teng DU Date: Tue, 21 Mar 2017 03:22:17 +0800 Subject: [PATCH 081/126] add --- Algorithms/01 Matrix/README.md | 8 ++++++++ Algorithms/Base 7/README.md | 8 ++++++++ Algorithms/Beautiful Arrangement/README.md | 8 ++++++++ Algorithms/Construct the Rectangle/README.md | 8 ++++++++ Algorithms/Detect Capital/README.md | 8 ++++++++ Algorithms/Diameter of Binary Tree/README.md | 8 ++++++++ Algorithms/Find Bottom Left Tree Value/README.md | 8 ++++++++ Algorithms/Find Largest Value in Each Tree Row/README.md | 8 ++++++++ Algorithms/Find Mode in Binary Search Tree/README.md | 8 ++++++++ Algorithms/K-diff Pairs in an Array/README.md | 8 ++++++++ Algorithms/Keyboard Row/README.md | 8 ++++++++ Algorithms/License Key Formatting/README.md | 8 ++++++++ Algorithms/Max Consecutive Ones/README.md | 8 ++++++++ Algorithms/Minesweeper/README.md | 8 ++++++++ Algorithms/Minimum Absolute Difference in BST/README.md | 8 ++++++++ Algorithms/Most Frequent Subtree Sum/README.md | 8 ++++++++ Algorithms/Next Greater Element I/README.md | 8 ++++++++ Algorithms/Next Greater Element II/README.md | 8 ++++++++ Algorithms/Number Complement/README.md | 8 ++++++++ Algorithms/Output Contest Matches/README.md | 0 Algorithms/Relative Ranks/README.md | 8 ++++++++ 21 files changed, 160 insertions(+) create mode 100644 Algorithms/01 Matrix/README.md create mode 100644 Algorithms/Base 7/README.md create mode 100644 Algorithms/Beautiful Arrangement/README.md create mode 100644 Algorithms/Construct the Rectangle/README.md create mode 100644 Algorithms/Detect Capital/README.md create mode 100644 Algorithms/Diameter of Binary Tree/README.md create mode 100644 Algorithms/Find Bottom Left Tree Value/README.md create mode 100644 Algorithms/Find Largest Value in Each Tree Row/README.md create mode 100644 Algorithms/Find Mode in Binary Search Tree/README.md create mode 100644 Algorithms/K-diff Pairs in an Array/README.md create mode 100644 Algorithms/Keyboard Row/README.md create mode 100644 Algorithms/License Key Formatting/README.md create mode 100644 Algorithms/Max Consecutive Ones/README.md create mode 100644 Algorithms/Minesweeper/README.md create mode 100644 Algorithms/Minimum Absolute Difference in BST/README.md create mode 100644 Algorithms/Most Frequent Subtree Sum/README.md create mode 100644 Algorithms/Next Greater Element I/README.md create mode 100644 Algorithms/Next Greater Element II/README.md create mode 100644 Algorithms/Number Complement/README.md create mode 100644 Algorithms/Output Contest Matches/README.md create mode 100644 Algorithms/Relative Ranks/README.md diff --git a/Algorithms/01 Matrix/README.md b/Algorithms/01 Matrix/README.md new file mode 100644 index 0000000..5f1de18 --- /dev/null +++ b/Algorithms/01 Matrix/README.md @@ -0,0 +1,8 @@ +# [01 Matrix](https://leetcode.com/problems/01-matrix/#/description) +###### No:`542` +###### Difficulty:`Medium` +## JavaScript + + +```js +``` diff --git a/Algorithms/Base 7/README.md b/Algorithms/Base 7/README.md new file mode 100644 index 0000000..92fa07b --- /dev/null +++ b/Algorithms/Base 7/README.md @@ -0,0 +1,8 @@ +# [Base 7](https://leetcode.com/problems/base-7/#/description) +###### No:`504` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/Algorithms/Beautiful Arrangement/README.md b/Algorithms/Beautiful Arrangement/README.md new file mode 100644 index 0000000..fc67c0d --- /dev/null +++ b/Algorithms/Beautiful Arrangement/README.md @@ -0,0 +1,8 @@ +# [Beautiful Arrangement](https://leetcode.com/problems/beautiful-arrangement/#/description) +###### No:`526` +###### Difficulty:`Medium` +## JavaScript + + +```js +``` diff --git a/Algorithms/Construct the Rectangle/README.md b/Algorithms/Construct the Rectangle/README.md new file mode 100644 index 0000000..ecdbcaa --- /dev/null +++ b/Algorithms/Construct the Rectangle/README.md @@ -0,0 +1,8 @@ +# [Construct the Rectangle](https://leetcode.com/problems/construct-the-rectangle/#/description) +###### No:`492` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/Algorithms/Detect Capital/README.md b/Algorithms/Detect Capital/README.md new file mode 100644 index 0000000..0fe7a22 --- /dev/null +++ b/Algorithms/Detect Capital/README.md @@ -0,0 +1,8 @@ +# [Detect Capital](https://leetcode.com/problems/detect-capital/#/description) +###### No:`520` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/Algorithms/Diameter of Binary Tree/README.md b/Algorithms/Diameter of Binary Tree/README.md new file mode 100644 index 0000000..ddc5588 --- /dev/null +++ b/Algorithms/Diameter of Binary Tree/README.md @@ -0,0 +1,8 @@ +# [Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/#/description) +###### No:`543` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/Algorithms/Find Bottom Left Tree Value/README.md b/Algorithms/Find Bottom Left Tree Value/README.md new file mode 100644 index 0000000..da06f4d --- /dev/null +++ b/Algorithms/Find Bottom Left Tree Value/README.md @@ -0,0 +1,8 @@ +# [Find Bottom Left Tree Value](https://leetcode.com/problems/find-bottom-left-tree-value/#/description) +###### No:`513` +###### Difficulty:`Medium` +## JavaScript + + +```js +``` diff --git a/Algorithms/Find Largest Value in Each Tree Row/README.md b/Algorithms/Find Largest Value in Each Tree Row/README.md new file mode 100644 index 0000000..8b64540 --- /dev/null +++ b/Algorithms/Find Largest Value in Each Tree Row/README.md @@ -0,0 +1,8 @@ +# [Find Largest Value in Each Tree Row](https://leetcode.com/problems/find-largest-value-in-each-tree-row/#/description) +###### No:`515` +###### Difficulty:`Medium` +## JavaScript + + +```js +``` diff --git a/Algorithms/Find Mode in Binary Search Tree/README.md b/Algorithms/Find Mode in Binary Search Tree/README.md new file mode 100644 index 0000000..46c691b --- /dev/null +++ b/Algorithms/Find Mode in Binary Search Tree/README.md @@ -0,0 +1,8 @@ +# [Find Mode in Binary Search Tree](https://leetcode.com/problems/find-mode-in-binary-search-tree/#/description) +###### No:`501` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/Algorithms/K-diff Pairs in an Array/README.md b/Algorithms/K-diff Pairs in an Array/README.md new file mode 100644 index 0000000..4206511 --- /dev/null +++ b/Algorithms/K-diff Pairs in an Array/README.md @@ -0,0 +1,8 @@ +# [K-diff Pairs in an Array](https://leetcode.com/problems/k-diff-pairs-in-an-array/#/description) +###### No:`532` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/Algorithms/Keyboard Row/README.md b/Algorithms/Keyboard Row/README.md new file mode 100644 index 0000000..5c6d248 --- /dev/null +++ b/Algorithms/Keyboard Row/README.md @@ -0,0 +1,8 @@ +# [Keyboard Row](https://leetcode.com/problems/keyboard-row/#/description) +###### No:`500` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/Algorithms/License Key Formatting/README.md b/Algorithms/License Key Formatting/README.md new file mode 100644 index 0000000..5e433d3 --- /dev/null +++ b/Algorithms/License Key Formatting/README.md @@ -0,0 +1,8 @@ +# [License Key Formatting](https://leetcode.com/problems/license-key-formatting/#/description) +###### No:`482` +###### Difficulty:`Medium` +## JavaScript + + +```js +``` diff --git a/Algorithms/Max Consecutive Ones/README.md b/Algorithms/Max Consecutive Ones/README.md new file mode 100644 index 0000000..0517c1e --- /dev/null +++ b/Algorithms/Max Consecutive Ones/README.md @@ -0,0 +1,8 @@ +# [Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/#/description) +###### No:`485` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/Algorithms/Minesweeper/README.md b/Algorithms/Minesweeper/README.md new file mode 100644 index 0000000..74e7140 --- /dev/null +++ b/Algorithms/Minesweeper/README.md @@ -0,0 +1,8 @@ +# [Minesweeper](https://leetcode.com/problems/minesweeper/#/description) +###### No:`529` +###### Difficulty:`Medium` +## JavaScript + + +```js +``` diff --git a/Algorithms/Minimum Absolute Difference in BST/README.md b/Algorithms/Minimum Absolute Difference in BST/README.md new file mode 100644 index 0000000..fd35b25 --- /dev/null +++ b/Algorithms/Minimum Absolute Difference in BST/README.md @@ -0,0 +1,8 @@ +# [Minimum Absolute Difference in BST](https://leetcode.com/problems/minimum-absolute-difference-in-bst/#/description) +###### No:`530` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/Algorithms/Most Frequent Subtree Sum/README.md b/Algorithms/Most Frequent Subtree Sum/README.md new file mode 100644 index 0000000..b665183 --- /dev/null +++ b/Algorithms/Most Frequent Subtree Sum/README.md @@ -0,0 +1,8 @@ +# [Most Frequent Subtree Sum](https://leetcode.com/problems/most-frequent-subtree-sum/#/description) +###### No:`508` +###### Difficulty:`Medium` +## JavaScript + + +```js +``` diff --git a/Algorithms/Next Greater Element I/README.md b/Algorithms/Next Greater Element I/README.md new file mode 100644 index 0000000..f51988c --- /dev/null +++ b/Algorithms/Next Greater Element I/README.md @@ -0,0 +1,8 @@ +# [Next Greater Element I](https://leetcode.com/problems/next-greater-element-i/#/description) +###### No:`496` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/Algorithms/Next Greater Element II/README.md b/Algorithms/Next Greater Element II/README.md new file mode 100644 index 0000000..c684418 --- /dev/null +++ b/Algorithms/Next Greater Element II/README.md @@ -0,0 +1,8 @@ +# [Next Greater Element II](https://leetcode.com/problems/next-greater-element-ii/#/description) +###### No:`503` +###### Difficulty:`Medium` +## JavaScript + + +```js +``` diff --git a/Algorithms/Number Complement/README.md b/Algorithms/Number Complement/README.md new file mode 100644 index 0000000..417b65a --- /dev/null +++ b/Algorithms/Number Complement/README.md @@ -0,0 +1,8 @@ +# [Number Complement](https://leetcode.com/problems/number-complement/#/description) +###### No:`476` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/Algorithms/Output Contest Matches/README.md b/Algorithms/Output Contest Matches/README.md new file mode 100644 index 0000000..e69de29 diff --git a/Algorithms/Relative Ranks/README.md b/Algorithms/Relative Ranks/README.md new file mode 100644 index 0000000..73898f9 --- /dev/null +++ b/Algorithms/Relative Ranks/README.md @@ -0,0 +1,8 @@ +# [Relative Ranks](https://leetcode.com/problems/relative-ranks/#/description) +###### No:`506` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` From 8b114eee00c127a5b6e2af4af3db766f3c1e680f Mon Sep 17 00:00:00 2001 From: Teng DU Date: Tue, 21 Mar 2017 03:26:37 +0800 Subject: [PATCH 082/126] update md --- Algorithms/Output Contest Matches/README.md | 50 +++++++++++++++++++++ README.md | 21 +++++++++ 2 files changed, 71 insertions(+) diff --git a/Algorithms/Output Contest Matches/README.md b/Algorithms/Output Contest Matches/README.md index e69de29..fbeb7ab 100644 --- a/Algorithms/Output Contest Matches/README.md +++ b/Algorithms/Output Contest Matches/README.md @@ -0,0 +1,50 @@ +# [Output Contest Matches](https://leetcode.com/problems/output-contest-matches/) +###### No:`544` +###### Difficulty:`Medium` +## JavaScript + +### LeetCode Weekly Contest 24 +### 544. Output Contest Matches +During the NBA playoffs, we always arrange the rather strong team to play with the rather weak team, like make the rank 1 team play with the rank nth team, which is a good strategy to make the contest more interesting. Now, you're given n teams, you need to output their final contest matches in the form of a string. + +The n teams are given in the form of positive integers from 1 to n, which represents their initial rank. (Rank 1 is the strongest team and Rank n is the weakest team.) We'll use parentheses('(', ')') and commas(',') to represent the contest team pairing - parentheses('(' , ')') for pairing and commas(',') for partition. During the pairing process in each round, you always need to follow the strategy of making the rather strong one pair with the rather weak one. + +#### Example +``` +Input: 2 +Output: (1,2) +Input: 4 +Output: ((1,4),(2,3)) +Input: 8 +Output: (((1,8),(4,5)),((2,7),(3,6))) +``` + +#### Solution +```js +/** + * @param {number} n + * @return {string} + */ +function getMatch (match) { + if (match.length === 1) return match; + var ret = []; + var start = 0; + var end = match.length - 1; + var value; + + while (start < end) { + value = `(${match[start]},${match[end]})`; + ret.push(value); + start++; + end--; + } + + return getMatch(ret); +} +var findContestMatch = function(n) { + var match = []; + for (var i = 1; i <= n; i++) match.push(i); + + return getMatch(match)[0]; +}; +``` diff --git a/README.md b/README.md index b959100..b8352ea 100644 --- a/README.md +++ b/README.md @@ -124,3 +124,24 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |459|Repeated Substring Pattern|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Repeated%20Substring%20Pattern)|Easy| |461|Hamming Distance|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Hamming%20Distance)|Easy| |463|Add Digits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Add%20Digits)|Easy| +|476|Number Complement|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Number%20Complement)|Easy| +|482|License Key Formatting|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/License%20Key%20Formatting)|Medium| +|485|Max Consecutive Ones|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Max%20Consecutive%20Ones)|Easy| +|492|Construct the Rectangle|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Construct%20the%20Rectangle)|Easy| +|496|Next Greater Element I|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Next%20Greater%20Element%20I)|Easy| +|500|Keyboard Row|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Keyboard%20Row)|Easy| +|501|Find Mode in Binary Search Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Find%20Mode%20in%20Binary%20Search%20Tree)|Easy| +|503|Next Greater Element II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Next%20Greater%20Element%20II)|Medium| +|504|Base 7|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Base%207)|Easy| +|506|Relative Ranks|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Relative%20Ranks)|Easy| +|508|Most Frequent Subtree Sum|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Most%20Frequent%20Subtree%20Sum)|Medium| +|513|Find Bottom Left Tree Value|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Find%20Bottom%20Left%20Tree%20Value)|Medium| +|515|Find Largest Value in Each Tree Row|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Find%20Largest%20Value%20in%20Each%20Tree%20Row)|Medium| +|520|Detect Capital|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Detect%20Capital)|Easy| +|526|Beautiful Arrangement|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Beautiful%20Arrangement)|Medium| +|529|Minesweeper|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Minesweeper)|Medium| +|530|Minimum Absolute Difference in BST|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Minimum%20Absolute%20Difference%20in%20BST)|Easy| +|532|K-diff Pairs in an Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/K-diff%20Pairs%20in%20an%20Array)|Easy| +|542|01 Matrix|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/01%20Matrix)|Medium| +|543|Diameter of Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Diameter%20of%20Binary%20Tree)|Easy| +|544|Output Contest Matches|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Output%20Contest%20Matches)|Medium| From 79d81cd93fd9c9f3bde75700db74b5491c6f5431 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:28:42 +0800 Subject: [PATCH 083/126] Update README.md --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b8352ea..2a21f6e 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,10 @@ leetcode ======== My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) - +### Total solved: 151 +##### Easy: 1 +##### Easy: 1 +##### Easy: 1 | No | Title | Source Code | Difficulty | |----| ----- | -------- | ---------- | From 6cded75221357809a3448185cfdd0b7a93d9461a Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:31:51 +0800 Subject: [PATCH 084/126] Update README.md --- README.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2a21f6e..398a1dc 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,8 @@ leetcode ======== My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) -### Total solved: 151 -##### Easy: 1 -##### Easy: 1 -##### Easy: 1 +#### Total solved: 151 (Easy: 1 Medium: 1 Hard: 1 ) + | No | Title | Source Code | Difficulty | |----| ----- | -------- | ---------- | From cb9bdfa8800cc6cf08df7e12f21e458f119cec15 Mon Sep 17 00:00:00 2001 From: Teng DU Date: Tue, 21 Mar 2017 03:35:54 +0800 Subject: [PATCH 085/126] update md --- README.md | 2 +- generate-md.js | 85 +++++++++++++++++++++++++++++--------------------- 2 files changed, 51 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 398a1dc..b8908d2 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ leetcode ======== +#### Total solved: 140 (Easy: 104 Medium: 35 Hard: 1 ) My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) -#### Total solved: 151 (Easy: 1 Medium: 1 Hard: 1 ) | No | Title | Source Code | Difficulty | diff --git a/generate-md.js b/generate-md.js index 2a363de..1085f1d 100644 --- a/generate-md.js +++ b/generate-md.js @@ -6,45 +6,60 @@ var dir = './Algorithms'; var repo_url = 'https://github.com/duteng/leetcode/tree/master/Algorithms/'; var ret = []; +var total = 0; +var easy = 0; +var medium = 0; +var hard = 0; + targetDirs = fs.readdir(dir, function(err, files) { - var count = files.length; - files.forEach(function(value, index, arr) { - fs.readFile(dir + '/' + value + '/README.md', {encoding: 'utf8'}, function(err, data) { - var no = parseInt(data.match(/###### No:`(\d+)`/)[1], 10); - var difficulty = data.match(/###### Difficulty:`(\w+)`/)[1]; - var title = data.match(/# \[(.*)\]/)[1]; - var link = '[JavaScript](' + repo_url + title.replace(/\s/g, '%20') + ')'; - ret.push({ - no: no, - title:title, - difficulty: difficulty, - link: link - }); - if(ret.length == count) { - proxy.emit('readDone'); - } - - }); + var count = files.length; + files.forEach(function(value, index, arr) { + fs.readFile(dir + '/' + value + '/README.md', {encoding: 'utf8'}, function(err, data) { + var no = parseInt(data.match(/###### No:`(\d+)`/)[1], 10); + var difficulty = data.match(/###### Difficulty:`(\w+)`/)[1]; + var title = data.match(/# \[(.*)\]/)[1]; + var link = '[JavaScript](' + repo_url + title.replace(/\s/g, '%20') + ')'; + + total++; + if (difficulty === 'Hard') { + hard++; + } else if (difficulty === 'Medium') { + medium++; + } else if (difficulty === 'Easy') { + easy++; + } + ret.push({ + no: no, + title:title, + difficulty: difficulty, + link: link + }); + if(ret.length == count) { + proxy.emit('readDone'); + } + }); + }); }); proxy.once('readDone', function() { - ret.sort(function(a, b) { - return a.no - b.no; - }); + ret.sort(function(a, b) { + return a.no - b.no; + }); + + var content = [ + 'leetcode', + '========', + `#### Total solved: ${total} (Easy: ${easy} Medium: ${medium} Hard: ${hard} )`, + 'My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/)', + '', + '', + '| No | Title | Source Code | Difficulty |', + '|----| ----- | -------- | ---------- |' + ].join('\r\n') + '\r\n'; - var content = [ - 'leetcode', - '========', - 'My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/)', - '', - '', - '| No | Title | Source Code | Difficulty |', - '|----| ----- | -------- | ---------- |' - ].join('\r\n') + '\r\n'; - - for(var i = 0; i < ret.length; i++) { - content += '|' + ret[i].no + '|' + ret[i].title + '|' + ret[i].link + '|' + ret[i].difficulty + '|' + '\r\n'; - } - fs.writeFile('README.md', content); + for(var i = 0; i < ret.length; i++) { + content += '|' + ret[i].no + '|' + ret[i].title + '|' + ret[i].link + '|' + ret[i].difficulty + '|' + '\r\n'; + } + fs.writeFile('README.md', content); }); From 8e173ca47a4e651ed25ee609450fe8902ffda594 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:37:22 +0800 Subject: [PATCH 086/126] Update README.md --- Algorithms/01 Matrix/README.md | 65 ++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/Algorithms/01 Matrix/README.md b/Algorithms/01 Matrix/README.md index 5f1de18..67dbc92 100644 --- a/Algorithms/01 Matrix/README.md +++ b/Algorithms/01 Matrix/README.md @@ -3,6 +3,71 @@ ###### Difficulty:`Medium` ## JavaScript +### LeetCode Weekly Contest 24 +### 542. 01 Matrix +Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell. +The distance between two adjacent cells is 1. +#### Example 1 +Input: +``` +0 0 0 +0 1 0 +0 0 0 +``` +Output: +``` +0 0 0 +0 1 0 +0 0 0 +``` +#### Example 2 +Input: +``` +0 0 0 +0 1 0 +1 1 1 +``` +Output: +``` +0 0 0 +0 1 0 +1 2 1 +``` +#### Solution ```js +/** + * @param {number[][]} matrix + * @return {number[][]} + */ +var updateMatrix = function(matrix) { + var m = matrix.length; + var n = matrix[0].length; + var queue = []; + + for (var i = 0; i < m; i++) { + for (var j = 0; j < n; j++) { + if (matrix[i][j] === 0) { + queue.push([i, j]); + } else { + matrix[i][j] = undefined; + } + } + } + + while (queue.length > 0) { + var [x, y] = queue.shift(); + var range = [[-1,0], [1, 0], [0, -1], [0, 1]]; + range.forEach(([p, q]) => { + p += x; + q += y; + if (p < 0 || p >= m || q < 0 || q >= n) return; + if (matrix[p][q] !== undefined && matrix[p][q] < matrix[x][y] + 1) return; + matrix[p][q] = matrix[x][y] + 1; + queue.push([p, q]); + }); + } + + return matrix; +}; ``` From 87a9b73dcdffd935a084f9afa11db56be03e0017 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:39:27 +0800 Subject: [PATCH 087/126] Update README.md --- Algorithms/Base 7/README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Algorithms/Base 7/README.md b/Algorithms/Base 7/README.md index 92fa07b..e11385f 100644 --- a/Algorithms/Base 7/README.md +++ b/Algorithms/Base 7/README.md @@ -5,4 +5,13 @@ ```js +/** + * @param {number} num + * @return {string} + */ +var convertToBase7 = function(num) { + if (num < 0) return '-' + convertToBase7(-num); + else if (num < 7) return String(num); + else return convertToBase7(parseInt(num / 7)) + num % 7; +}; ``` From beae3cad8beb2f05e321b7b35b44661d9cf28ed3 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:40:00 +0800 Subject: [PATCH 088/126] Update README.md --- Algorithms/Beautiful Arrangement/README.md | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Algorithms/Beautiful Arrangement/README.md b/Algorithms/Beautiful Arrangement/README.md index fc67c0d..a1a393e 100644 --- a/Algorithms/Beautiful Arrangement/README.md +++ b/Algorithms/Beautiful Arrangement/README.md @@ -5,4 +5,31 @@ ```js +/** + * @param {number} N + * @return {number} + */ +var countArrangement = function(N) { + var position = []; + for (var i = 1; i <= N; i++) position.push(i); + var value = position.slice(); + + var ret = 0; + var count = function(position, value) { + if (position.length === 1 && value.length === 1) { + if (position[0] % value[0] === 0 || value[0] % position[0] === 0) { + ret++; + } + return + } + for (var i = 0; i < position.length; i++) { + if (position[i] % value[0] === 0 || value[0] % position[i] === 0) { + count(position.slice(0, i).concat(position.slice(i + 1)), value.slice(1)); + } + } + }; + count(position, value); + return ret; +}; + ``` From fd48c83d64352c66586ac04efdd0a1bd1edab7eb Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:40:30 +0800 Subject: [PATCH 089/126] Update README.md --- Algorithms/Construct the Rectangle/README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Algorithms/Construct the Rectangle/README.md b/Algorithms/Construct the Rectangle/README.md index ecdbcaa..fde800f 100644 --- a/Algorithms/Construct the Rectangle/README.md +++ b/Algorithms/Construct the Rectangle/README.md @@ -5,4 +5,19 @@ ```js +/** + * @param {number} area + * @return {number[]} + */ +var constructRectangle = function(area) { + var root = parseInt(Math.sqrt(area)); + var ret = []; + for (var i = root; i <= area; i++) { + if (area % i === 0) { + i > area / i ? ret.push(i, area / i) : ret.push(area / i, i); + break; + } + } + return ret; +}; ``` From f4e17f41d0cbfa5a1d344132fda78f8f51ff827b Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:40:56 +0800 Subject: [PATCH 090/126] Update README.md --- Algorithms/Detect Capital/README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Algorithms/Detect Capital/README.md b/Algorithms/Detect Capital/README.md index 0fe7a22..6e9bc68 100644 --- a/Algorithms/Detect Capital/README.md +++ b/Algorithms/Detect Capital/README.md @@ -5,4 +5,11 @@ ```js +/** + * @param {string} word + * @return {boolean} + */ +var detectCapitalUse = function(word) { + return /^[A-Z]?([a-z]*|[A-Z]*)$/.test(word); +}; ``` From 628ba623d499a72bde391959b66b82df15c6ca2b Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:41:20 +0800 Subject: [PATCH 091/126] Update README.md --- Algorithms/Diameter of Binary Tree/README.md | 31 ++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/Algorithms/Diameter of Binary Tree/README.md b/Algorithms/Diameter of Binary Tree/README.md index ddc5588..5068eaf 100644 --- a/Algorithms/Diameter of Binary Tree/README.md +++ b/Algorithms/Diameter of Binary Tree/README.md @@ -5,4 +5,35 @@ ```js +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number} + */ + +var diameterOfBinaryTree = function(root) { + var maxDiameter = -1; + var getDepth = function(root) { + if (root === null) { + return 0; + } + var leftDepth = getDepth(root.left); + var rightDepth = getDepth(root.right); + + maxDiameter = Math.max(maxDiameter, leftDepth + rightDepth); + maxDepth = Math.max(leftDepth, rightDepth) + 1; + return maxDepth; + }; + + if (root === null) return 0; + getDepth(root); + + return maxDiameter; +}; ``` From e9dfe53e29193652608b77f37b8bf18b80c94a07 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:41:44 +0800 Subject: [PATCH 092/126] Update README.md --- .../Find Bottom Left Tree Value/README.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/Algorithms/Find Bottom Left Tree Value/README.md b/Algorithms/Find Bottom Left Tree Value/README.md index da06f4d..0ae65d7 100644 --- a/Algorithms/Find Bottom Left Tree Value/README.md +++ b/Algorithms/Find Bottom Left Tree Value/README.md @@ -5,4 +5,34 @@ ```js +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number} + */ + +var findBottomLeftValue = function(root) { + var maxDepth = Number.MIN_SAFE_INTEGER; + var value; + + var traverse = function(root, depth) { + if (root === null) return; + if (depth > maxDepth) { + value = root.val; + maxDepth = depth; + } + traverse(root.left, depth + 1); + traverse(root.right, depth + 1); + }; + + traverse(root, 1); + return value; +}; + ``` From f74ffa2cca075163e552491edcc12148243f800c Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:42:17 +0800 Subject: [PATCH 093/126] Update README.md --- .../README.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/Algorithms/Find Largest Value in Each Tree Row/README.md b/Algorithms/Find Largest Value in Each Tree Row/README.md index 8b64540..2debf05 100644 --- a/Algorithms/Find Largest Value in Each Tree Row/README.md +++ b/Algorithms/Find Largest Value in Each Tree Row/README.md @@ -5,4 +5,34 @@ ```js +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number[]} + */ +var largestValues = function(root) { + if (root === null) return []; + var left = largestValues(root.left); + var right = largestValues(root.right); + + var ret = []; + for(var i = 0; i < left.length || i < right.length; i++) { + if (left[i] === undefined) { + ret.push(right[i]); + } else if (right[i] === undefined) { + ret.push(left[i]); + } else { + ret.push(Math.max(right[i], left[i])); + } + } + ret.unshift(root.val); + + return ret; +}; ``` From 6bb22f24192e31e0302ad21596cf51f035dd9244 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:42:37 +0800 Subject: [PATCH 094/126] Update README.md --- .../Find Mode in Binary Search Tree/README.md | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/Algorithms/Find Mode in Binary Search Tree/README.md b/Algorithms/Find Mode in Binary Search Tree/README.md index 46c691b..bdd5a29 100644 --- a/Algorithms/Find Mode in Binary Search Tree/README.md +++ b/Algorithms/Find Mode in Binary Search Tree/README.md @@ -5,4 +5,57 @@ ```js +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number[]} + */ + +var traversal = function(root, func) { + if (root === null) { + return null; + } + traversal(root.left, func); + func(root.val); + traversal(root.right, func); +}; + +var findMode = function(root) { + if (root === null) return []; + + var ret; + var maxCount = Number.MIN_VALUE; + var prevValue; + var currentCount; + + traversal(root, function(val) { + if (prevValue === undefined) { + prevValue = val; + currentCount = 1; + } else { + if (prevValue === val) { + currentCount++; + } else { + currentCount = 1; + } + } + + if(currentCount > maxCount) { + ret = []; + ret.push(val); + maxCount = currentCount; + } else if (currentCount === maxCount) { + ret.push(val); + } + prevValue = val; + }); + + return ret; +}; ``` From 632623efb9e12d6b55d925daea508da41dbc8589 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:43:01 +0800 Subject: [PATCH 095/126] Update README.md --- Algorithms/K-diff Pairs in an Array/README.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/Algorithms/K-diff Pairs in an Array/README.md b/Algorithms/K-diff Pairs in an Array/README.md index 4206511..1227a9f 100644 --- a/Algorithms/K-diff Pairs in an Array/README.md +++ b/Algorithms/K-diff Pairs in an Array/README.md @@ -5,4 +5,35 @@ ```js +/** + * @param {number[]} nums + * @param {number} k + * @return {number} + */ +var findPairs = function(nums, k) { + if (k < 0 || nums.length === 0) return 0; + var itemCountMap = {}; + var count = 0; + + nums.forEach((item, index) => { + itemCountMap[item] === undefined ? itemCountMap[item] = 1 : itemCountMap[item]++; + }); + + if (k === 0) { + for (var key in itemCountMap) { + if (itemCountMap[key] >= 2) { + count++; + } + } + } else { + for (var key in itemCountMap) { + if (itemCountMap[parseInt(key)+ k] !== undefined) { + count++; + } + } + } + + return count; +}; + ``` From b115bc1c74fce9072a63d5d9c9b820753bec5994 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:43:35 +0800 Subject: [PATCH 096/126] Update README.md --- Algorithms/Keyboard Row/README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Algorithms/Keyboard Row/README.md b/Algorithms/Keyboard Row/README.md index 5c6d248..1f13760 100644 --- a/Algorithms/Keyboard Row/README.md +++ b/Algorithms/Keyboard Row/README.md @@ -5,4 +5,13 @@ ```js +/** + * @param {string[]} words + * @return {string[]} + */ +var findWords = function(words) { + return words.filter(function(str) { + return /^([qwertyuiop]*|[asdfghjkl]*|[zxcvbnm]*)$/.test(str.toLowerCase()); + }); +}; ``` From 5d1b87945d91118243d7f7cf5b13041de017bd66 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:43:54 +0800 Subject: [PATCH 097/126] Update README.md --- Algorithms/License Key Formatting/README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Algorithms/License Key Formatting/README.md b/Algorithms/License Key Formatting/README.md index 5e433d3..d3d18fc 100644 --- a/Algorithms/License Key Formatting/README.md +++ b/Algorithms/License Key Formatting/README.md @@ -5,4 +5,23 @@ ```js +/** + * @param {string} S + * @param {number} K + * @return {string} + */ +var format = function(chars, length, separator) { + if (chars.length <= length) { + return chars.join(''); + } + return format(chars.slice(0, chars.length - length), length, separator) + separator + chars.slice(-length).join(''); +}; +var licenseKeyFormatting = function(S, K) { + var chars = S.split('').filter((char) => { + return char !== '-'; + }); + + return format(chars, K, '-').toUpperCase(); + +}; ``` From 310e1689aa99f64c3d346dfc3a70a48502e2ae2f Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:44:18 +0800 Subject: [PATCH 098/126] Update README.md --- Algorithms/Max Consecutive Ones/README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Algorithms/Max Consecutive Ones/README.md b/Algorithms/Max Consecutive Ones/README.md index 0517c1e..4142107 100644 --- a/Algorithms/Max Consecutive Ones/README.md +++ b/Algorithms/Max Consecutive Ones/README.md @@ -5,4 +5,21 @@ ```js +/** + * @param {number[]} nums + * @return {number} + */ +var findMaxConsecutiveOnes = function(nums) { + var max = 0; + var length = 0; + nums.forEach(function(num) { + if (num === 1) length++; + if (num === 0) { + if (length > max) max = length; + length = 0; + } + }); + if (length > max) max = length; + return max; +}; ``` From acdd8916955658d42b02d0a9d100315f21e61b14 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:44:46 +0800 Subject: [PATCH 099/126] Update README.md --- Algorithms/Minesweeper/README.md | 68 ++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/Algorithms/Minesweeper/README.md b/Algorithms/Minesweeper/README.md index 74e7140..6cf4625 100644 --- a/Algorithms/Minesweeper/README.md +++ b/Algorithms/Minesweeper/README.md @@ -5,4 +5,72 @@ ```js +/** + * @param {character[][]} board + * @param {number[]} click + * @return {character[][]} + */ +var getMineCount = function(board, point) { + var m = board.length; + var n = board[0].length; + var x = point[0]; + var y = point[1]; + var count = 0; + for (var i = -1; i < 2; i++) { + for (var j = -1; j < 2; j++) { + var p = x + i; + var q = y + j; + if (p < 0 || p >= m || q < 0 || q >= n) continue; + if (board[p][q] === 'M' || board[p][q] === 'X') count++; + } + } + return count; +}; + +var updateBoard = function(board, click) { + var m = board.length; + var n = board[0].length; + var visited = []; + for (var k = 0; k < m; k++) visited.push(new Array(n).fill(false)); + + var queue = []; + queue.push(click); + + while(queue.length > 0) { + var point = queue.shift(); + var x = point[0]; + var y = point[1]; + + if (visited[x][y]) { + continue; + } else { + visited[x][y] = true; + } + + if (board[x][y] === 'M') { + board[x][y] = 'X'; + } else { + var count = getMineCount(board, point); + + if (count === 0) { + board[x][y] = 'B'; + for (var i = -1; i < 2; i++) { + for (var j = -1; j < 2; j++) { + var p = x + i; + var q = y + j; + if (p < 0 || p >= m || q < 0 || q >= n || (p === x && q === y)) continue; + if (board[p][q] === 'E'){ + queue.push([p, q]); + } + } + } + } else { + board[x][y] = count + ''; + } + } + } + + return board; +}; + ``` From d95da489178ce5f244c6bc375b6457ca07590651 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:45:09 +0800 Subject: [PATCH 100/126] Update README.md --- .../README.md | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/Algorithms/Minimum Absolute Difference in BST/README.md b/Algorithms/Minimum Absolute Difference in BST/README.md index fd35b25..f50893a 100644 --- a/Algorithms/Minimum Absolute Difference in BST/README.md +++ b/Algorithms/Minimum Absolute Difference in BST/README.md @@ -5,4 +5,32 @@ ```js +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number} + */ + +var getMinimumDifference = function(root) { + var prev = null; + var min = Number.MAX_VALUE; + var traversal = function(root) { + if (root === null) return; + traversal(root.left); + if (prev !== null && root.val - prev < min) { + min = root.val - prev; + } + prev = root.val; + traversal(root.right); + }; + traversal(root); + + return min; +}; ``` From b7575d9512034dc603e79f3a088b9e22fa3f51ff Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:45:30 +0800 Subject: [PATCH 101/126] Update README.md --- .../Most Frequent Subtree Sum/README.md | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/Algorithms/Most Frequent Subtree Sum/README.md b/Algorithms/Most Frequent Subtree Sum/README.md index b665183..1455c49 100644 --- a/Algorithms/Most Frequent Subtree Sum/README.md +++ b/Algorithms/Most Frequent Subtree Sum/README.md @@ -5,4 +5,46 @@ ```js +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number[]} + */ +function getSum (root, map) { + if (root === null) return 0; + var left = getSum(root.left, map); + var right = getSum(root.right, map); + var sum = root.val + left + right; + + map[sum] === undefined ? map[sum] = 1 : map[sum]++; + return sum; +} +var findFrequentTreeSum = function(root) { + if (root === null) return []; + var valueCountMap = {}; + var max = -1; + var ret = []; + var key; + + getSum(root, valueCountMap); + for (key in valueCountMap) { + if (valueCountMap[key] > max) { + max = valueCountMap[key]; + } + } + + for (key in valueCountMap) { + if (valueCountMap[key] === max) { + ret.push(parseInt(key)); + } + } + + return ret; +}; ``` From c1bb99232487febcc12b457bd6482dec8dc95f3a Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:45:50 +0800 Subject: [PATCH 102/126] Update README.md --- Algorithms/Next Greater Element I/README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Algorithms/Next Greater Element I/README.md b/Algorithms/Next Greater Element I/README.md index f51988c..0cb5dc4 100644 --- a/Algorithms/Next Greater Element I/README.md +++ b/Algorithms/Next Greater Element I/README.md @@ -5,4 +5,23 @@ ```js +/** + * @param {number[]} findNums + * @param {number[]} nums + * @return {number[]} + */ +var nextGreaterElement = function(findNums, nums) { + var map = {}; + var stack = []; + nums.forEach(function(num) { + while(stack.length > 0 && stack[stack.length - 1] < num) { + map[stack.pop()] = num; + } + stack.push(num); + }); + + return findNums.map(function(num) { + return map[num] || -1; + }); +}; ``` From 4ba1e9393c729072c1522c2b0cb7d4071d2b117c Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:46:10 +0800 Subject: [PATCH 103/126] Update README.md --- Algorithms/Next Greater Element II/README.md | 27 ++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Algorithms/Next Greater Element II/README.md b/Algorithms/Next Greater Element II/README.md index c684418..fae4471 100644 --- a/Algorithms/Next Greater Element II/README.md +++ b/Algorithms/Next Greater Element II/README.md @@ -5,4 +5,31 @@ ```js +/** + * @param {number[]} nums + * @return {number[]} + */ +var nextGreaterElements = function(nums) { + var nextGreaterMap = {}; + var stack = []; + nums.concat(nums).forEach((item) => { + while (stack.length > 0 && stack[stack.length - 1] < item) { + var key = stack.pop(); + if (nextGreaterMap[key] === undefined) { + nextGreaterMap[key] = []; + } + nextGreaterMap[key].push(item); + } + stack.push(item); + }); + + return nums.map((item) => { + if (nextGreaterMap[item] && nextGreaterMap[item].length > 0) { + return nextGreaterMap[item].shift(); + } else { + return -1; + } + }); +}; + ``` From 886f7238ef738c9ec039d24242278a44ef73a261 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:46:40 +0800 Subject: [PATCH 104/126] Update README.md --- Algorithms/Number Complement/README.md | 63 ++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/Algorithms/Number Complement/README.md b/Algorithms/Number Complement/README.md index 417b65a..8a2a97f 100644 --- a/Algorithms/Number Complement/README.md +++ b/Algorithms/Number Complement/README.md @@ -5,4 +5,67 @@ ```js +/** + * @param {number} num + * @return {number} + +这道题给了我们一个数,让我们求补数。通过分析题目汇总的例子,我们知道需要做的就是每个位翻转一下就行了,但是翻转的起始位置上从最高位的1开始的,前面的0是不能被翻转的,所以我们从高往低遍历,如果遇到第一个1了后,我们的flag就赋值为true,然后就可以进行翻转了,翻转的方法就是对应位异或一个1即可,参见代码如下: + + + +解法一: + +复制代码 +class Solution { +public: + int findComplement(int num) { + bool start = false; + for (int i = 31; i >= 0; --i) { + if (num & (1 << i)) start = true; + if (start) num ^= (1 << i); + } + return num; + } +}; +复制代码 + + +由于位操作里面的取反符号~本身就可以翻转位,但是如果直接对num取反的话就是每一位都翻转了,而最高位1之前的0是不能翻转的,所以我们只要用一个mask来标记最高位1前面的所有0的位置,然后对mask取反后,与上对num取反的结果即可,参见代码如下: + + + +解法二: + +复制代码 +class Solution { +public: + int findComplement(int num) { + int mask = INT_MAX; + while (mask & num) mask <<= 1; + return ~mask & ~num; + } +}; +复制代码 + + +再来看一种迭代的写法,一行搞定碉堡了,思路就是每次都右移一位,并根据最低位的值先进行翻转,如果当前值小于等于1了,就不用再调用递归函数了,参见代码如下: + + + +解法三: + +class Solution { +public: + int findComplement(int num) { + return (1 - num % 2) + 2 * (num <= 1 ? 0 : findComplement(num / 2)); + } +}; + + */ + +var findComplement = function(num) { + var mask = ~0; + while (mask & num) mask <<= 1; + return ~mask & ~num; +}; ``` From 010f923ed437514fb1e4fa62acf953bddd3cd107 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 03:47:10 +0800 Subject: [PATCH 105/126] Update README.md --- Algorithms/Relative Ranks/README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Algorithms/Relative Ranks/README.md b/Algorithms/Relative Ranks/README.md index 73898f9..5ee5f0c 100644 --- a/Algorithms/Relative Ranks/README.md +++ b/Algorithms/Relative Ranks/README.md @@ -5,4 +5,29 @@ ```js +/** + * @param {number[]} nums + * @return {string[]} + */ +var findRelativeRanks = function(nums) { + var rank = 4; + var map = {}; + var sortNums = [].slice.call(nums).sort((a, b) => b - a); + for (var i = 0; i < sortNums.length; i++) { + if (i === 0) { + map[sortNums[i]] = 'Gold Medal'; + } else if (i === 1) { + map[sortNums[i]] = 'Silver Medal'; + } else if (i === 2) { + map[sortNums[i]] = 'Bronze Medal'; + } else { + map[sortNums[i]] = String(rank); + rank++; + } + } + + return nums.map((score) => { + return map[score]; + }); +}; ``` From 310e90b834aea80a13e8f21ef0328680358f7494 Mon Sep 17 00:00:00 2001 From: Teng DU Date: Tue, 21 Mar 2017 03:50:46 +0800 Subject: [PATCH 106/126] update md --- Algorithms/01 Matrix/README.md | 2 +- Algorithms/3Sum Closest/README.md | 2 +- Algorithms/3Sum/README.md | 2 +- Algorithms/Add Digits/README.md | 2 +- Algorithms/Add Strings/README.md | 2 +- Algorithms/Arithmetic Slices/README.md | 2 +- Algorithms/Arranging Coins/README.md | 2 +- Algorithms/Assign Cookies/README.md | 2 +- Algorithms/Balanced Binary Tree/README.md | 2 +- Algorithms/Base 7/README.md | 2 +- Algorithms/Beautiful Arrangement/README.md | 2 +- Algorithms/Best Time to Buy and Sell Stock II/README.md | 2 +- Algorithms/Best Time to Buy and Sell Stock/README.md | 2 +- Algorithms/Binary Tree Level Order Traversal II/README.md | 2 +- Algorithms/Binary Tree Level Order Traversal/README.md | 2 +- Algorithms/Binary Tree Paths/README.md | 2 +- Algorithms/Binary Tree Preorder Traversal/README.md | 2 +- Algorithms/Binary Watch/README.md | 2 +- Algorithms/Bulb Switcher/README.md | 2 +- Algorithms/Climbing Stairs/README.md | 2 +- Algorithms/Compare Version Numbers/README.md | 2 +- Algorithms/Construct the Rectangle/README.md | 2 +- Algorithms/Contains Duplicate II/README.md | 2 +- Algorithms/Contains Duplicate/README.md | 2 +- Algorithms/Copy List with Random Pointer/README.md | 2 +- Algorithms/Count Primes/README.md | 2 +- Algorithms/Counting Bits/README.md | 2 +- Algorithms/Delete Node in a BST/README.md | 2 +- Algorithms/Delete Node in a Linked List/README.md | 2 +- Algorithms/Detect Capital/README.md | 2 +- Algorithms/Diameter of Binary Tree/README.md | 2 +- Algorithms/Evaluate Reverse Polish Notation/README.md | 2 +- Algorithms/Excel Sheet Column Number/README.md | 2 +- Algorithms/Excel Sheet Column Title/README.md | 2 +- Algorithms/Factorial Trailing Zeroes/README.md | 2 +- Algorithms/Find All Duplicates in an Array/README.md | 2 +- Algorithms/Find All Numbers Disappeared in an Array/README.md | 2 +- Algorithms/Find Bottom Left Tree Value/README.md | 2 +- Algorithms/Find Largest Value in Each Tree Row/README.md | 2 +- Algorithms/Find Mode in Binary Search Tree/README.md | 2 +- Algorithms/Find the Difference/README.md | 2 +- Algorithms/First Bad Version/README.md | 2 +- Algorithms/First Unique Character in a String/README.md | 2 +- Algorithms/Fizz Buzz/README.md | 2 +- Algorithms/Generate Parentheses/README.md | 2 +- Algorithms/Guess Number Higher or Lower/README.md | 2 +- Algorithms/Hamming Distance/README.md | 2 +- Algorithms/Happy Number/README.md | 2 +- Algorithms/House Robber/README.md | 2 +- Algorithms/Implement Queue using Stacks/README.md | 2 +- Algorithms/Implement strStr()/README.md | 2 +- Algorithms/Integer Break/README.md | 2 +- Algorithms/Integer to Roman/README.md | 2 +- Algorithms/Intersection of Two Arrays II/README.md | 2 +- Algorithms/Intersection of Two Arrays/README.md | 2 +- Algorithms/Intersection of Two Linked Lists/README.md | 2 +- Algorithms/Invert Binary Tree/README.md | 2 +- Algorithms/Island Perimeter/README.md | 2 +- Algorithms/Isomorphic Strings/README.md | 2 +- Algorithms/K-diff Pairs in an Array/README.md | 2 +- Algorithms/Keyboard Row/README.md | 2 +- Algorithms/Largest Number/README.md | 2 +- Algorithms/Letter Combinations of a Phone Number/README.md | 2 +- Algorithms/License Key Formatting/README.md | 2 +- Algorithms/Linked List Cycle/README.md | 2 +- Algorithms/Longest Palindrome/README.md | 2 +- .../Lowest Common Ancestor of a Binary Search Tree/README.md | 2 +- Algorithms/Majority Element/README.md | 2 +- Algorithms/Max Consecutive Ones/README.md | 2 +- Algorithms/Maximum Depth of Binary Tree/README.md | 2 +- Algorithms/Merge Sorted Array/README.md | 2 +- Algorithms/Merge Two Sorted Lists/README.md | 2 +- Algorithms/Min Stack/README.md | 2 +- Algorithms/Minesweeper/README.md | 2 +- Algorithms/Minimum Absolute Difference in BST/README.md | 2 +- Algorithms/Minimum Depth of Binary Tree/README.md | 2 +- Algorithms/Minimum Moves to Equal Array Elements/README.md | 2 +- Algorithms/Missing Number/README.md | 2 +- Algorithms/Most Frequent Subtree Sum/README.md | 2 +- Algorithms/Move Zeroes/README.md | 2 +- Algorithms/Next Greater Element I/README.md | 2 +- Algorithms/Next Greater Element II/README.md | 2 +- Algorithms/Nim Game/README.md | 2 +- Algorithms/Nth Digit/README.md | 2 +- Algorithms/Number Complement/README.md | 2 +- Algorithms/Number of 1 Bits/README.md | 2 +- Algorithms/Number of Boomerangs/README.md | 2 +- Algorithms/Number of Segments in a String/README.md | 2 +- Algorithms/Odd Even Linked List/README.md | 2 +- Algorithms/Output Contest Matches/README.md | 2 +- Algorithms/Palindrome Linked List/README.md | 2 +- Algorithms/Palindrome Number/README.md | 2 +- Algorithms/Pascal's Triangle II/README.md | 2 +- Algorithms/Pascal's Triangle/README.md | 2 +- Algorithms/Path Sum III/README.md | 2 +- Algorithms/Path Sum/README.md | 2 +- Algorithms/Permutations/README.md | 2 +- Algorithms/Plus One/README.md | 2 +- Algorithms/Power of Four/README.md | 2 +- Algorithms/Power of Three/README.md | 2 +- Algorithms/Power of Two/README.md | 2 +- Algorithms/Product of Array Except Self/README.md | 2 +- Algorithms/Queue Reconstruction by Height/README.md | 2 +- Algorithms/Ransom Note/README.md | 2 +- Algorithms/Rectangle Area/README.md | 2 +- Algorithms/Relative Ranks/README.md | 2 +- Algorithms/Remove Duplicates from Sorted Array/README.md | 2 +- Algorithms/Remove Duplicates from Sorted List/README.md | 2 +- Algorithms/Remove Element/README.md | 2 +- Algorithms/Remove Linked List Elements/README.md | 2 +- Algorithms/Remove Nth Node From End of List/README.md | 2 +- Algorithms/Repeated Substring Pattern/README.md | 2 +- Algorithms/Reverse Integer/README.md | 2 +- Algorithms/Reverse Linked List II/README.md | 2 +- Algorithms/Reverse Linked List/README.md | 2 +- Algorithms/Reverse String/README.md | 2 +- Algorithms/Reverse Vowels of a String/README.md | 2 +- Algorithms/Reverse Words in a String/README.md | 2 +- Algorithms/Roman to Integer/README.md | 2 +- Algorithms/Rotate Array/README.md | 2 +- Algorithms/Same Tree/README.md | 2 +- Algorithms/Set Matrix Zeroes/README.md | 2 +- Algorithms/Single Number/README.md | 2 +- Algorithms/Sort List/README.md | 2 +- Algorithms/String to Integer (atoi)/README.md | 2 +- Algorithms/Sum of Left Leaves/README.md | 2 +- Algorithms/Summary Ranges/README.md | 2 +- Algorithms/Swap Nodes in Pairs/README.md | 2 +- Algorithms/Symmetric Tree/README.md | 2 +- Algorithms/Third Maximum Number/README.md | 2 +- Algorithms/Top K Frequent Elements/README.md | 2 +- Algorithms/Two Sum/README.md | 2 +- Algorithms/Ugly Number/README.md | 2 +- Algorithms/Valid Anagram/README.md | 2 +- Algorithms/Valid Palindrome/README.md | 2 +- Algorithms/Valid Parentheses/README.md | 2 +- Algorithms/Valid Perfect Square/README.md | 2 +- Algorithms/Valid Sudoku/README.md | 2 +- Algorithms/ZigZag Conversion/README.md | 2 +- 139 files changed, 139 insertions(+), 139 deletions(-) diff --git a/Algorithms/01 Matrix/README.md b/Algorithms/01 Matrix/README.md index 67dbc92..86b12f7 100644 --- a/Algorithms/01 Matrix/README.md +++ b/Algorithms/01 Matrix/README.md @@ -1,7 +1,7 @@ # [01 Matrix](https://leetcode.com/problems/01-matrix/#/description) ###### No:`542` ###### Difficulty:`Medium` -## JavaScript + ### LeetCode Weekly Contest 24 ### 542. 01 Matrix diff --git a/Algorithms/3Sum Closest/README.md b/Algorithms/3Sum Closest/README.md index 4de9cb6..9a11268 100644 --- a/Algorithms/3Sum Closest/README.md +++ b/Algorithms/3Sum Closest/README.md @@ -1,7 +1,7 @@ # [3Sum Closest](https://leetcode.com/problems/3sum-closest/) ###### No:`16` ###### Difficulty:`Medium` -## JavaScript + ```javascript /** diff --git a/Algorithms/3Sum/README.md b/Algorithms/3Sum/README.md index e5fa761..3523d60 100644 --- a/Algorithms/3Sum/README.md +++ b/Algorithms/3Sum/README.md @@ -1,7 +1,7 @@ # [3Sum](https://leetcode.com/problems/3sum/) ###### No:`15` ###### Difficulty:`Medium` -## JavaScript + ### Solution 1 Result: `timeout` diff --git a/Algorithms/Add Digits/README.md b/Algorithms/Add Digits/README.md index 9a05afb..1425167 100644 --- a/Algorithms/Add Digits/README.md +++ b/Algorithms/Add Digits/README.md @@ -1,7 +1,7 @@ # [Add Digits](https://leetcode.com/problems/add-digits/) ###### No:`258` ###### Difficulty:`Easy` -## JavaScript + #### Solution 1: ```javascript diff --git a/Algorithms/Add Strings/README.md b/Algorithms/Add Strings/README.md index 5ac91c9..4658de4 100644 --- a/Algorithms/Add Strings/README.md +++ b/Algorithms/Add Strings/README.md @@ -1,7 +1,7 @@ # [Add Strings](https://leetcode.com/problems/add-strings/) ###### No:`415` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Arithmetic Slices/README.md b/Algorithms/Arithmetic Slices/README.md index 5d7102a..7dc61a4 100644 --- a/Algorithms/Arithmetic Slices/README.md +++ b/Algorithms/Arithmetic Slices/README.md @@ -1,7 +1,7 @@ # [Arithmetic Slices](https://leetcode.com/problems/arithmetic-slices/#/description) ###### No:`413` ###### Difficulty:`Medium` -## JavaScript + ```js diff --git a/Algorithms/Arranging Coins/README.md b/Algorithms/Arranging Coins/README.md index c3d93e6..712bcea 100644 --- a/Algorithms/Arranging Coins/README.md +++ b/Algorithms/Arranging Coins/README.md @@ -1,7 +1,7 @@ # [Arranging Coins](https://leetcode.com/problems/arranging-coins/#/description) ###### No:`441` ###### Difficulty:`Easy` -## JavaScript + ```js diff --git a/Algorithms/Assign Cookies/README.md b/Algorithms/Assign Cookies/README.md index 1638849..cfee914 100644 --- a/Algorithms/Assign Cookies/README.md +++ b/Algorithms/Assign Cookies/README.md @@ -1,7 +1,7 @@ # [Assign Cookies](https://leetcode.com/problems/assign-cookies/) ###### No:`455` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Balanced Binary Tree/README.md b/Algorithms/Balanced Binary Tree/README.md index 727dead..10475e3 100644 --- a/Algorithms/Balanced Binary Tree/README.md +++ b/Algorithms/Balanced Binary Tree/README.md @@ -1,7 +1,7 @@ # [Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/) ###### No:`110` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Base 7/README.md b/Algorithms/Base 7/README.md index e11385f..b470ec3 100644 --- a/Algorithms/Base 7/README.md +++ b/Algorithms/Base 7/README.md @@ -1,7 +1,7 @@ # [Base 7](https://leetcode.com/problems/base-7/#/description) ###### No:`504` ###### Difficulty:`Easy` -## JavaScript + ```js diff --git a/Algorithms/Beautiful Arrangement/README.md b/Algorithms/Beautiful Arrangement/README.md index a1a393e..4f0722b 100644 --- a/Algorithms/Beautiful Arrangement/README.md +++ b/Algorithms/Beautiful Arrangement/README.md @@ -1,7 +1,7 @@ # [Beautiful Arrangement](https://leetcode.com/problems/beautiful-arrangement/#/description) ###### No:`526` ###### Difficulty:`Medium` -## JavaScript + ```js diff --git a/Algorithms/Best Time to Buy and Sell Stock II/README.md b/Algorithms/Best Time to Buy and Sell Stock II/README.md index 2099072..47071af 100644 --- a/Algorithms/Best Time to Buy and Sell Stock II/README.md +++ b/Algorithms/Best Time to Buy and Sell Stock II/README.md @@ -1,7 +1,7 @@ # [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) ###### No:`122` ###### Difficulty:`Medium` -## JavaScript + ```javascript /** diff --git a/Algorithms/Best Time to Buy and Sell Stock/README.md b/Algorithms/Best Time to Buy and Sell Stock/README.md index 3696190..3d83f5c 100644 --- a/Algorithms/Best Time to Buy and Sell Stock/README.md +++ b/Algorithms/Best Time to Buy and Sell Stock/README.md @@ -1,7 +1,7 @@ # [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/#/description) ###### No:`121` ###### Difficulty:`Easy` -## JavaScript + ```js diff --git a/Algorithms/Binary Tree Level Order Traversal II/README.md b/Algorithms/Binary Tree Level Order Traversal II/README.md index d57f3d0..22b3407 100644 --- a/Algorithms/Binary Tree Level Order Traversal II/README.md +++ b/Algorithms/Binary Tree Level Order Traversal II/README.md @@ -1,7 +1,7 @@ # [Binary Tree Level Order Traversal II](https://leetcode.com/problems/binary-tree-level-order-traversal-ii/) ###### No:`107` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Binary Tree Level Order Traversal/README.md b/Algorithms/Binary Tree Level Order Traversal/README.md index a79282c..691ec4f 100644 --- a/Algorithms/Binary Tree Level Order Traversal/README.md +++ b/Algorithms/Binary Tree Level Order Traversal/README.md @@ -1,7 +1,7 @@ # [Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/) ###### No:`102` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Binary Tree Paths/README.md b/Algorithms/Binary Tree Paths/README.md index b911ef6..c93ac26 100644 --- a/Algorithms/Binary Tree Paths/README.md +++ b/Algorithms/Binary Tree Paths/README.md @@ -1,7 +1,7 @@ # [Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/) ###### No:`257` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Binary Tree Preorder Traversal/README.md b/Algorithms/Binary Tree Preorder Traversal/README.md index 97ca4aa..ce02dd9 100644 --- a/Algorithms/Binary Tree Preorder Traversal/README.md +++ b/Algorithms/Binary Tree Preorder Traversal/README.md @@ -1,7 +1,7 @@ # [Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/) ###### No:`144` ###### Difficulty:`Medium` -## JavaScript + ```javascript /** diff --git a/Algorithms/Binary Watch/README.md b/Algorithms/Binary Watch/README.md index 428d7f0..78699b3 100644 --- a/Algorithms/Binary Watch/README.md +++ b/Algorithms/Binary Watch/README.md @@ -1,7 +1,7 @@ # [Binary Watch](https://leetcode.com/problems/binary-watch/) ###### No:`401` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Bulb Switcher/README.md b/Algorithms/Bulb Switcher/README.md index 35e20e2..276441f 100644 --- a/Algorithms/Bulb Switcher/README.md +++ b/Algorithms/Bulb Switcher/README.md @@ -1,7 +1,7 @@ # [Add Digits](https://leetcode.com/problems/add-digits/) ###### No:`319` ###### Difficulty:`Medium` -## JavaScript + #### Solution 1: refer: https://leetcode.com/discuss/91371/share-my-o-1-solution-with-explanation diff --git a/Algorithms/Climbing Stairs/README.md b/Algorithms/Climbing Stairs/README.md index c30c2f4..73d87cb 100644 --- a/Algorithms/Climbing Stairs/README.md +++ b/Algorithms/Climbing Stairs/README.md @@ -1,7 +1,7 @@ # [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/) ###### No:`70` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Compare Version Numbers/README.md b/Algorithms/Compare Version Numbers/README.md index dbbc3e8..7164fa4 100644 --- a/Algorithms/Compare Version Numbers/README.md +++ b/Algorithms/Compare Version Numbers/README.md @@ -1,7 +1,7 @@ # [Compare Version Numbers](https://leetcode.com/problems/compare-version-numbers/) ###### No:`165` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Construct the Rectangle/README.md b/Algorithms/Construct the Rectangle/README.md index fde800f..4049f65 100644 --- a/Algorithms/Construct the Rectangle/README.md +++ b/Algorithms/Construct the Rectangle/README.md @@ -1,7 +1,7 @@ # [Construct the Rectangle](https://leetcode.com/problems/construct-the-rectangle/#/description) ###### No:`492` ###### Difficulty:`Easy` -## JavaScript + ```js diff --git a/Algorithms/Contains Duplicate II/README.md b/Algorithms/Contains Duplicate II/README.md index a2c7bc9..5038eb4 100644 --- a/Algorithms/Contains Duplicate II/README.md +++ b/Algorithms/Contains Duplicate II/README.md @@ -1,7 +1,7 @@ # [Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/) ###### No:`219` ###### Difficulty:`Easy` -## JavaScript + #### Solution 1: (slower) ```javascript diff --git a/Algorithms/Contains Duplicate/README.md b/Algorithms/Contains Duplicate/README.md index a57eec3..ebc9fa4 100644 --- a/Algorithms/Contains Duplicate/README.md +++ b/Algorithms/Contains Duplicate/README.md @@ -1,7 +1,7 @@ # [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) ###### No:`217` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Copy List with Random Pointer/README.md b/Algorithms/Copy List with Random Pointer/README.md index 9ea3cfe..18df542 100644 --- a/Algorithms/Copy List with Random Pointer/README.md +++ b/Algorithms/Copy List with Random Pointer/README.md @@ -1,7 +1,7 @@ # [Copy List with Random Pointer](https://leetcode.com/problems/copy-list-with-random-pointer/#/description) ###### No:`138` ###### Difficulty:`Medium` -## JavaScript + ```js diff --git a/Algorithms/Count Primes/README.md b/Algorithms/Count Primes/README.md index bc47fe8..310ad9c 100644 --- a/Algorithms/Count Primes/README.md +++ b/Algorithms/Count Primes/README.md @@ -1,7 +1,7 @@ # [Count Primes](https://leetcode.com/problems/count-primes/#/description) ###### No:`204` ###### Difficulty:`Easy` -## JavaScript + ```js diff --git a/Algorithms/Counting Bits/README.md b/Algorithms/Counting Bits/README.md index dc718cd..7da8c2a 100644 --- a/Algorithms/Counting Bits/README.md +++ b/Algorithms/Counting Bits/README.md @@ -1,7 +1,7 @@ # [Counting Bits](https://leetcode.com/problems/counting-bits/) ###### No:`338` ###### Difficulty:`Medium` -## JavaScript + ```javascript var countBits = function(num) { diff --git a/Algorithms/Delete Node in a BST/README.md b/Algorithms/Delete Node in a BST/README.md index 860c1c6..cc5c58e 100644 --- a/Algorithms/Delete Node in a BST/README.md +++ b/Algorithms/Delete Node in a BST/README.md @@ -1,7 +1,7 @@ # [Delete Node in a BST](https://leetcode.com/problems/delete-node-in-a-bst/#/description) ###### No:`450` ###### Difficulty:`Medium` -## JavaScript + ```js diff --git a/Algorithms/Delete Node in a Linked List/README.md b/Algorithms/Delete Node in a Linked List/README.md index 255f49e..ade4793 100644 --- a/Algorithms/Delete Node in a Linked List/README.md +++ b/Algorithms/Delete Node in a Linked List/README.md @@ -1,7 +1,7 @@ # [Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/) ###### No:`237` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Detect Capital/README.md b/Algorithms/Detect Capital/README.md index 6e9bc68..e6b21bc 100644 --- a/Algorithms/Detect Capital/README.md +++ b/Algorithms/Detect Capital/README.md @@ -1,7 +1,7 @@ # [Detect Capital](https://leetcode.com/problems/detect-capital/#/description) ###### No:`520` ###### Difficulty:`Easy` -## JavaScript + ```js diff --git a/Algorithms/Diameter of Binary Tree/README.md b/Algorithms/Diameter of Binary Tree/README.md index 5068eaf..e6da936 100644 --- a/Algorithms/Diameter of Binary Tree/README.md +++ b/Algorithms/Diameter of Binary Tree/README.md @@ -1,7 +1,7 @@ # [Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/#/description) ###### No:`543` ###### Difficulty:`Easy` -## JavaScript + ```js diff --git a/Algorithms/Evaluate Reverse Polish Notation/README.md b/Algorithms/Evaluate Reverse Polish Notation/README.md index ef6b689..56ec469 100644 --- a/Algorithms/Evaluate Reverse Polish Notation/README.md +++ b/Algorithms/Evaluate Reverse Polish Notation/README.md @@ -1,7 +1,7 @@ # [Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/) ###### No:`150` ###### Difficulty:`Medium` -## JavaScript + ```javascript /** diff --git a/Algorithms/Excel Sheet Column Number/README.md b/Algorithms/Excel Sheet Column Number/README.md index 79e7c17..9532661 100644 --- a/Algorithms/Excel Sheet Column Number/README.md +++ b/Algorithms/Excel Sheet Column Number/README.md @@ -1,7 +1,7 @@ # [Excel Sheet Column Number](https://leetcode.com/problems/excel-sheet-column-number/) ###### No:`171` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Excel Sheet Column Title/README.md b/Algorithms/Excel Sheet Column Title/README.md index ec7c1a9..813aab6 100644 --- a/Algorithms/Excel Sheet Column Title/README.md +++ b/Algorithms/Excel Sheet Column Title/README.md @@ -1,7 +1,7 @@ # [Excel Sheet Column Title](https://leetcode.com/problems/excel-sheet-column-title/) ###### No:`168` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Factorial Trailing Zeroes/README.md b/Algorithms/Factorial Trailing Zeroes/README.md index 6dcbe10..846e866 100644 --- a/Algorithms/Factorial Trailing Zeroes/README.md +++ b/Algorithms/Factorial Trailing Zeroes/README.md @@ -1,7 +1,7 @@ # [Factorial Trailing Zeroes](https://leetcode.com/problems/factorial-trailing-zeroes/) ###### No:`172` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Find All Duplicates in an Array/README.md b/Algorithms/Find All Duplicates in an Array/README.md index 5fa4849..e22fd9c 100644 --- a/Algorithms/Find All Duplicates in an Array/README.md +++ b/Algorithms/Find All Duplicates in an Array/README.md @@ -1,7 +1,7 @@ # [Find All Duplicates in an Array](https://leetcode.com/problems/find-all-duplicates-in-an-array/#/description) ###### No:`442` ###### Difficulty:`Medium` -## JavaScript + ```js diff --git a/Algorithms/Find All Numbers Disappeared in an Array/README.md b/Algorithms/Find All Numbers Disappeared in an Array/README.md index 3752028..d517d7d 100644 --- a/Algorithms/Find All Numbers Disappeared in an Array/README.md +++ b/Algorithms/Find All Numbers Disappeared in an Array/README.md @@ -1,7 +1,7 @@ # [Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/#/description) ###### No:`448` ###### Difficulty:`Easy` -## JavaScript + ```js diff --git a/Algorithms/Find Bottom Left Tree Value/README.md b/Algorithms/Find Bottom Left Tree Value/README.md index 0ae65d7..49fe8c7 100644 --- a/Algorithms/Find Bottom Left Tree Value/README.md +++ b/Algorithms/Find Bottom Left Tree Value/README.md @@ -1,7 +1,7 @@ # [Find Bottom Left Tree Value](https://leetcode.com/problems/find-bottom-left-tree-value/#/description) ###### No:`513` ###### Difficulty:`Medium` -## JavaScript + ```js diff --git a/Algorithms/Find Largest Value in Each Tree Row/README.md b/Algorithms/Find Largest Value in Each Tree Row/README.md index 2debf05..c3b5104 100644 --- a/Algorithms/Find Largest Value in Each Tree Row/README.md +++ b/Algorithms/Find Largest Value in Each Tree Row/README.md @@ -1,7 +1,7 @@ # [Find Largest Value in Each Tree Row](https://leetcode.com/problems/find-largest-value-in-each-tree-row/#/description) ###### No:`515` ###### Difficulty:`Medium` -## JavaScript + ```js diff --git a/Algorithms/Find Mode in Binary Search Tree/README.md b/Algorithms/Find Mode in Binary Search Tree/README.md index bdd5a29..75d4e18 100644 --- a/Algorithms/Find Mode in Binary Search Tree/README.md +++ b/Algorithms/Find Mode in Binary Search Tree/README.md @@ -1,7 +1,7 @@ # [Find Mode in Binary Search Tree](https://leetcode.com/problems/find-mode-in-binary-search-tree/#/description) ###### No:`501` ###### Difficulty:`Easy` -## JavaScript + ```js diff --git a/Algorithms/Find the Difference/README.md b/Algorithms/Find the Difference/README.md index 263ec05..9ddd28e 100644 --- a/Algorithms/Find the Difference/README.md +++ b/Algorithms/Find the Difference/README.md @@ -1,7 +1,7 @@ # [Find the Difference](https://leetcode.com/problems/find-the-difference/) ###### No:`389` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/First Bad Version/README.md b/Algorithms/First Bad Version/README.md index fa74904..3f7d81d 100644 --- a/Algorithms/First Bad Version/README.md +++ b/Algorithms/First Bad Version/README.md @@ -1,7 +1,7 @@ # [First Bad Version](https://leetcode.com/problems/first-bad-version/#/description) ###### No:`278` ###### Difficulty:`Easy` -## JavaScript + ```js diff --git a/Algorithms/First Unique Character in a String/README.md b/Algorithms/First Unique Character in a String/README.md index 95e2fbf..1a1ca53 100644 --- a/Algorithms/First Unique Character in a String/README.md +++ b/Algorithms/First Unique Character in a String/README.md @@ -1,7 +1,7 @@ # [First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) ###### No:`387` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Fizz Buzz/README.md b/Algorithms/Fizz Buzz/README.md index 2e28688..eb00e59 100644 --- a/Algorithms/Fizz Buzz/README.md +++ b/Algorithms/Fizz Buzz/README.md @@ -1,7 +1,7 @@ # [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) ###### No:`412` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Generate Parentheses/README.md b/Algorithms/Generate Parentheses/README.md index c752baa..516fd6f 100644 --- a/Algorithms/Generate Parentheses/README.md +++ b/Algorithms/Generate Parentheses/README.md @@ -1,7 +1,7 @@ # [Generate Parentheses](https://leetcode.com/problems/generate-parentheses/) ###### No:`22` ###### Difficulty:`Medium` -## JavaScript + ```javascript /** diff --git a/Algorithms/Guess Number Higher or Lower/README.md b/Algorithms/Guess Number Higher or Lower/README.md index 156c9b3..0c1ca15 100644 --- a/Algorithms/Guess Number Higher or Lower/README.md +++ b/Algorithms/Guess Number Higher or Lower/README.md @@ -1,7 +1,7 @@ # [Guess Number Higher or Lower](https://leetcode.com/problems/guess-number-higher-or-lower/#/description) ###### No:`374` ###### Difficulty:`Easy` -## JavaScript + ```js diff --git a/Algorithms/Hamming Distance/README.md b/Algorithms/Hamming Distance/README.md index a382dfa..f0a55a9 100644 --- a/Algorithms/Hamming Distance/README.md +++ b/Algorithms/Hamming Distance/README.md @@ -1,7 +1,7 @@ # [Hamming Distance](https://leetcode.com/problems/hamming-distance/#/description) ###### No:`461` ###### Difficulty:`Easy` -## JavaScript + ```js diff --git a/Algorithms/Happy Number/README.md b/Algorithms/Happy Number/README.md index dee5aad..ad2cc7d 100644 --- a/Algorithms/Happy Number/README.md +++ b/Algorithms/Happy Number/README.md @@ -1,7 +1,7 @@ # [Happy Number](https://leetcode.com/problems/happy-number/) ###### No:`202` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/House Robber/README.md b/Algorithms/House Robber/README.md index 8e3142f..f8805d7 100644 --- a/Algorithms/House Robber/README.md +++ b/Algorithms/House Robber/README.md @@ -1,7 +1,7 @@ # [House Robber](https://leetcode.com/problems/house-robber/) ###### No:`198` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Implement Queue using Stacks/README.md b/Algorithms/Implement Queue using Stacks/README.md index 86bbf8f..f3a0938 100644 --- a/Algorithms/Implement Queue using Stacks/README.md +++ b/Algorithms/Implement Queue using Stacks/README.md @@ -1,7 +1,7 @@ # [Implement Queue using Stacks](https://leetcode.com/problems/implement-queue-using-stacks/) ###### No:`232` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Implement strStr()/README.md b/Algorithms/Implement strStr()/README.md index a47979a..9d31c36 100644 --- a/Algorithms/Implement strStr()/README.md +++ b/Algorithms/Implement strStr()/README.md @@ -1,7 +1,7 @@ # [Implement strStr()](https://leetcode.com/problems/implement-strstr) ###### No:`28` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Integer Break/README.md b/Algorithms/Integer Break/README.md index 53a31ee..2667c91 100644 --- a/Algorithms/Integer Break/README.md +++ b/Algorithms/Integer Break/README.md @@ -1,7 +1,7 @@ # [Integer Break](https://leetcode.com/problems/integer-break/) ###### No:`343` ###### Difficulty:`Medium` -## JavaScript + ```javascript /** diff --git a/Algorithms/Integer to Roman/README.md b/Algorithms/Integer to Roman/README.md index 7d12bcd..9314d53 100644 --- a/Algorithms/Integer to Roman/README.md +++ b/Algorithms/Integer to Roman/README.md @@ -1,7 +1,7 @@ # [Integer to Roman](https://leetcode.com/problems/integer-to-roman/) ###### No:`12` ###### Difficulty:`Medium` -## JavaScript + ```javascript /** diff --git a/Algorithms/Intersection of Two Arrays II/README.md b/Algorithms/Intersection of Two Arrays II/README.md index 3d2f792..c02ad6b 100644 --- a/Algorithms/Intersection of Two Arrays II/README.md +++ b/Algorithms/Intersection of Two Arrays II/README.md @@ -1,7 +1,7 @@ # [Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) ###### No:`350` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Intersection of Two Arrays/README.md b/Algorithms/Intersection of Two Arrays/README.md index 0187794..8d7fb90 100644 --- a/Algorithms/Intersection of Two Arrays/README.md +++ b/Algorithms/Intersection of Two Arrays/README.md @@ -1,7 +1,7 @@ # [Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/) ###### No:`349` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Intersection of Two Linked Lists/README.md b/Algorithms/Intersection of Two Linked Lists/README.md index 53d4502..c2340b3 100644 --- a/Algorithms/Intersection of Two Linked Lists/README.md +++ b/Algorithms/Intersection of Two Linked Lists/README.md @@ -1,7 +1,7 @@ # [Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/) ###### No:`160` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Invert Binary Tree/README.md b/Algorithms/Invert Binary Tree/README.md index 32bcd83..ba13916 100644 --- a/Algorithms/Invert Binary Tree/README.md +++ b/Algorithms/Invert Binary Tree/README.md @@ -1,7 +1,7 @@ # [Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/) ###### No:`226` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Island Perimeter/README.md b/Algorithms/Island Perimeter/README.md index 40dca30..f6317db 100644 --- a/Algorithms/Island Perimeter/README.md +++ b/Algorithms/Island Perimeter/README.md @@ -1,7 +1,7 @@ # [Add Digits](https://leetcode.com/problems/island-perimeter/) ###### No:`463` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Isomorphic Strings/README.md b/Algorithms/Isomorphic Strings/README.md index 49a55d0..ebfee03 100644 --- a/Algorithms/Isomorphic Strings/README.md +++ b/Algorithms/Isomorphic Strings/README.md @@ -1,7 +1,7 @@ # [Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings/#/description) ###### No:`205` ###### Difficulty:`Easy` -## JavaScript + ```js diff --git a/Algorithms/K-diff Pairs in an Array/README.md b/Algorithms/K-diff Pairs in an Array/README.md index 1227a9f..f8afdbb 100644 --- a/Algorithms/K-diff Pairs in an Array/README.md +++ b/Algorithms/K-diff Pairs in an Array/README.md @@ -1,7 +1,7 @@ # [K-diff Pairs in an Array](https://leetcode.com/problems/k-diff-pairs-in-an-array/#/description) ###### No:`532` ###### Difficulty:`Easy` -## JavaScript + ```js diff --git a/Algorithms/Keyboard Row/README.md b/Algorithms/Keyboard Row/README.md index 1f13760..d15b890 100644 --- a/Algorithms/Keyboard Row/README.md +++ b/Algorithms/Keyboard Row/README.md @@ -1,7 +1,7 @@ # [Keyboard Row](https://leetcode.com/problems/keyboard-row/#/description) ###### No:`500` ###### Difficulty:`Easy` -## JavaScript + ```js diff --git a/Algorithms/Largest Number/README.md b/Algorithms/Largest Number/README.md index 09a9442..3c732d5 100644 --- a/Algorithms/Largest Number/README.md +++ b/Algorithms/Largest Number/README.md @@ -1,7 +1,7 @@ # [Largest Number](https://leetcode.com/problems/largest-number/) ###### No:`179` ###### Difficulty:`Medium` -## JavaScript + ```javascript /** diff --git a/Algorithms/Letter Combinations of a Phone Number/README.md b/Algorithms/Letter Combinations of a Phone Number/README.md index 1eacd1a..e2f2d66 100644 --- a/Algorithms/Letter Combinations of a Phone Number/README.md +++ b/Algorithms/Letter Combinations of a Phone Number/README.md @@ -1,7 +1,7 @@ # [Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/) ###### No:`17` ###### Difficulty:`Medium` -## JavaScript + ```javascript /** diff --git a/Algorithms/License Key Formatting/README.md b/Algorithms/License Key Formatting/README.md index d3d18fc..c36281e 100644 --- a/Algorithms/License Key Formatting/README.md +++ b/Algorithms/License Key Formatting/README.md @@ -1,7 +1,7 @@ # [License Key Formatting](https://leetcode.com/problems/license-key-formatting/#/description) ###### No:`482` ###### Difficulty:`Medium` -## JavaScript + ```js diff --git a/Algorithms/Linked List Cycle/README.md b/Algorithms/Linked List Cycle/README.md index 8c5777a..b331cad 100644 --- a/Algorithms/Linked List Cycle/README.md +++ b/Algorithms/Linked List Cycle/README.md @@ -1,7 +1,7 @@ # [Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/#/description) ###### No:`141` ###### Difficulty:`Easy` -## JavaScript + ```js diff --git a/Algorithms/Longest Palindrome/README.md b/Algorithms/Longest Palindrome/README.md index 041b19d..944cc57 100644 --- a/Algorithms/Longest Palindrome/README.md +++ b/Algorithms/Longest Palindrome/README.md @@ -1,7 +1,7 @@ # [Longest Palindrome](https://leetcode.com/problems/longest-palindrome/) ###### No:`409` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Lowest Common Ancestor of a Binary Search Tree/README.md b/Algorithms/Lowest Common Ancestor of a Binary Search Tree/README.md index c7b0ad3..85140b2 100644 --- a/Algorithms/Lowest Common Ancestor of a Binary Search Tree/README.md +++ b/Algorithms/Lowest Common Ancestor of a Binary Search Tree/README.md @@ -1,7 +1,7 @@ # [Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree) ###### No:`235` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Majority Element/README.md b/Algorithms/Majority Element/README.md index 390ec31..435c558 100644 --- a/Algorithms/Majority Element/README.md +++ b/Algorithms/Majority Element/README.md @@ -1,7 +1,7 @@ # [Majority Element](https://leetcode.com/problems/majority-element/) ###### No:`169` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Max Consecutive Ones/README.md b/Algorithms/Max Consecutive Ones/README.md index 4142107..3223d8e 100644 --- a/Algorithms/Max Consecutive Ones/README.md +++ b/Algorithms/Max Consecutive Ones/README.md @@ -1,7 +1,7 @@ # [Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/#/description) ###### No:`485` ###### Difficulty:`Easy` -## JavaScript + ```js diff --git a/Algorithms/Maximum Depth of Binary Tree/README.md b/Algorithms/Maximum Depth of Binary Tree/README.md index 2ebc70c..0d240df 100644 --- a/Algorithms/Maximum Depth of Binary Tree/README.md +++ b/Algorithms/Maximum Depth of Binary Tree/README.md @@ -1,7 +1,7 @@ # [Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/) ###### No:`104` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Merge Sorted Array/README.md b/Algorithms/Merge Sorted Array/README.md index 5d47be0..7934313 100644 --- a/Algorithms/Merge Sorted Array/README.md +++ b/Algorithms/Merge Sorted Array/README.md @@ -1,7 +1,7 @@ # [Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/) ###### No:`88` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Merge Two Sorted Lists/README.md b/Algorithms/Merge Two Sorted Lists/README.md index 5849bcb..153d896 100644 --- a/Algorithms/Merge Two Sorted Lists/README.md +++ b/Algorithms/Merge Two Sorted Lists/README.md @@ -1,7 +1,7 @@ # [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) ###### No:`21` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Min Stack/README.md b/Algorithms/Min Stack/README.md index 9a775d0..4a28b34 100644 --- a/Algorithms/Min Stack/README.md +++ b/Algorithms/Min Stack/README.md @@ -1,7 +1,7 @@ # [Min Stack](https://leetcode.com/problems/min-stack/) ###### No:`155` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Minesweeper/README.md b/Algorithms/Minesweeper/README.md index 6cf4625..399c4a6 100644 --- a/Algorithms/Minesweeper/README.md +++ b/Algorithms/Minesweeper/README.md @@ -1,7 +1,7 @@ # [Minesweeper](https://leetcode.com/problems/minesweeper/#/description) ###### No:`529` ###### Difficulty:`Medium` -## JavaScript + ```js diff --git a/Algorithms/Minimum Absolute Difference in BST/README.md b/Algorithms/Minimum Absolute Difference in BST/README.md index f50893a..c0260f0 100644 --- a/Algorithms/Minimum Absolute Difference in BST/README.md +++ b/Algorithms/Minimum Absolute Difference in BST/README.md @@ -1,7 +1,7 @@ # [Minimum Absolute Difference in BST](https://leetcode.com/problems/minimum-absolute-difference-in-bst/#/description) ###### No:`530` ###### Difficulty:`Easy` -## JavaScript + ```js diff --git a/Algorithms/Minimum Depth of Binary Tree/README.md b/Algorithms/Minimum Depth of Binary Tree/README.md index 1491978..0a3e565 100644 --- a/Algorithms/Minimum Depth of Binary Tree/README.md +++ b/Algorithms/Minimum Depth of Binary Tree/README.md @@ -1,7 +1,7 @@ # [Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/) ###### No:`111` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Minimum Moves to Equal Array Elements/README.md b/Algorithms/Minimum Moves to Equal Array Elements/README.md index 0f358a9..c9e1c47 100644 --- a/Algorithms/Minimum Moves to Equal Array Elements/README.md +++ b/Algorithms/Minimum Moves to Equal Array Elements/README.md @@ -1,7 +1,7 @@ # [Minimum Moves to Equal Array Elements](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/#/description) ###### No:`453` ###### Difficulty:`Easy` -## JavaScript + ```js diff --git a/Algorithms/Missing Number/README.md b/Algorithms/Missing Number/README.md index 8b2e385..218ba80 100644 --- a/Algorithms/Missing Number/README.md +++ b/Algorithms/Missing Number/README.md @@ -1,7 +1,7 @@ # [Missing Number](https://leetcode.com/problems/missing-number/) ###### No:`268` ###### Difficulty:`Medium` -## JavaScript + ```javascript /** diff --git a/Algorithms/Most Frequent Subtree Sum/README.md b/Algorithms/Most Frequent Subtree Sum/README.md index 1455c49..cff7a2f 100644 --- a/Algorithms/Most Frequent Subtree Sum/README.md +++ b/Algorithms/Most Frequent Subtree Sum/README.md @@ -1,7 +1,7 @@ # [Most Frequent Subtree Sum](https://leetcode.com/problems/most-frequent-subtree-sum/#/description) ###### No:`508` ###### Difficulty:`Medium` -## JavaScript + ```js diff --git a/Algorithms/Move Zeroes/README.md b/Algorithms/Move Zeroes/README.md index 5a284a9..6471621 100644 --- a/Algorithms/Move Zeroes/README.md +++ b/Algorithms/Move Zeroes/README.md @@ -1,7 +1,7 @@ # [Move Zeroes](https://leetcode.com/problems/move-zeroes/) ###### No:`283` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Next Greater Element I/README.md b/Algorithms/Next Greater Element I/README.md index 0cb5dc4..adc1c10 100644 --- a/Algorithms/Next Greater Element I/README.md +++ b/Algorithms/Next Greater Element I/README.md @@ -1,7 +1,7 @@ # [Next Greater Element I](https://leetcode.com/problems/next-greater-element-i/#/description) ###### No:`496` ###### Difficulty:`Easy` -## JavaScript + ```js diff --git a/Algorithms/Next Greater Element II/README.md b/Algorithms/Next Greater Element II/README.md index fae4471..a2dbce0 100644 --- a/Algorithms/Next Greater Element II/README.md +++ b/Algorithms/Next Greater Element II/README.md @@ -1,7 +1,7 @@ # [Next Greater Element II](https://leetcode.com/problems/next-greater-element-ii/#/description) ###### No:`503` ###### Difficulty:`Medium` -## JavaScript + ```js diff --git a/Algorithms/Nim Game/README.md b/Algorithms/Nim Game/README.md index 9764cb4..5428ebb 100644 --- a/Algorithms/Nim Game/README.md +++ b/Algorithms/Nim Game/README.md @@ -1,7 +1,7 @@ # [Nim Game](https://leetcode.com/problems/nim-game/) ###### No:`292` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Nth Digit/README.md b/Algorithms/Nth Digit/README.md index ad8fa5f..9a62d82 100644 --- a/Algorithms/Nth Digit/README.md +++ b/Algorithms/Nth Digit/README.md @@ -1,7 +1,7 @@ # [Nth Digit](https://leetcode.com/problems/nth-digit/#/description) ###### No:`400` ###### Difficulty:`Easy` -## JavaScript + ```js diff --git a/Algorithms/Number Complement/README.md b/Algorithms/Number Complement/README.md index 8a2a97f..10fa230 100644 --- a/Algorithms/Number Complement/README.md +++ b/Algorithms/Number Complement/README.md @@ -1,7 +1,7 @@ # [Number Complement](https://leetcode.com/problems/number-complement/#/description) ###### No:`476` ###### Difficulty:`Easy` -## JavaScript + ```js diff --git a/Algorithms/Number of 1 Bits/README.md b/Algorithms/Number of 1 Bits/README.md index c5583b7..42312ae 100644 --- a/Algorithms/Number of 1 Bits/README.md +++ b/Algorithms/Number of 1 Bits/README.md @@ -1,7 +1,7 @@ # [Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/) ###### No:`191` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Number of Boomerangs/README.md b/Algorithms/Number of Boomerangs/README.md index 72810a5..9c21791 100644 --- a/Algorithms/Number of Boomerangs/README.md +++ b/Algorithms/Number of Boomerangs/README.md @@ -1,7 +1,7 @@ # [Number of Boomerangs](https://leetcode.com/problems/number-of-boomerangs/) ###### No:`447` ###### Difficulty:`Easy` -## JavaScript + ### Solution: Get each 2 points' distance. For one point, save the distance as the key and count as the value into a map. The Boomerangs which contains this point will be count * (count - 1). diff --git a/Algorithms/Number of Segments in a String/README.md b/Algorithms/Number of Segments in a String/README.md index e132f6e..036be85 100644 --- a/Algorithms/Number of Segments in a String/README.md +++ b/Algorithms/Number of Segments in a String/README.md @@ -1,7 +1,7 @@ # [Number of Segments in a String](https://leetcode.com/problems/number-of-segments-in-a-string/#/description) ###### No:`434` ###### Difficulty:`Easy` -## JavaScript + ```js diff --git a/Algorithms/Odd Even Linked List/README.md b/Algorithms/Odd Even Linked List/README.md index 2289e5f..3cc9445 100644 --- a/Algorithms/Odd Even Linked List/README.md +++ b/Algorithms/Odd Even Linked List/README.md @@ -1,7 +1,7 @@ # [Odd Even Linked List](https://leetcode.com/problems/odd-even-linked-list/) ###### No:`328` ###### Difficulty:`Medium` -## JavaScript + ```javascript /** diff --git a/Algorithms/Output Contest Matches/README.md b/Algorithms/Output Contest Matches/README.md index fbeb7ab..0168e02 100644 --- a/Algorithms/Output Contest Matches/README.md +++ b/Algorithms/Output Contest Matches/README.md @@ -1,7 +1,7 @@ # [Output Contest Matches](https://leetcode.com/problems/output-contest-matches/) ###### No:`544` ###### Difficulty:`Medium` -## JavaScript + ### LeetCode Weekly Contest 24 ### 544. Output Contest Matches diff --git a/Algorithms/Palindrome Linked List/README.md b/Algorithms/Palindrome Linked List/README.md index 77eaee8..47adfe3 100644 --- a/Algorithms/Palindrome Linked List/README.md +++ b/Algorithms/Palindrome Linked List/README.md @@ -1,7 +1,7 @@ # [Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list/) ###### No:`234` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Palindrome Number/README.md b/Algorithms/Palindrome Number/README.md index ec97da2..ea0aae2 100644 --- a/Algorithms/Palindrome Number/README.md +++ b/Algorithms/Palindrome Number/README.md @@ -1,7 +1,7 @@ # [Palindrome Number](https://leetcode.com/problems/palindrome-number/) ###### No:`9` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Pascal's Triangle II/README.md b/Algorithms/Pascal's Triangle II/README.md index 923dfc6..f89764b 100644 --- a/Algorithms/Pascal's Triangle II/README.md +++ b/Algorithms/Pascal's Triangle II/README.md @@ -1,7 +1,7 @@ # [Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii/) ###### No:`119` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Pascal's Triangle/README.md b/Algorithms/Pascal's Triangle/README.md index db2e1f2..964c1e9 100644 --- a/Algorithms/Pascal's Triangle/README.md +++ b/Algorithms/Pascal's Triangle/README.md @@ -1,7 +1,7 @@ # [Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/) ###### No:`118` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Path Sum III/README.md b/Algorithms/Path Sum III/README.md index ca1460f..55eaac5 100644 --- a/Algorithms/Path Sum III/README.md +++ b/Algorithms/Path Sum III/README.md @@ -1,7 +1,7 @@ # [Path Sum III](https://leetcode.com/problems/path-sum-iii/#/description) ###### No:`437` ###### Difficulty:`Easy` -## JavaScript + ```js diff --git a/Algorithms/Path Sum/README.md b/Algorithms/Path Sum/README.md index d4bd6c6..7bec17f 100644 --- a/Algorithms/Path Sum/README.md +++ b/Algorithms/Path Sum/README.md @@ -1,7 +1,7 @@ # [Path Sum](https://leetcode.com/problems/path-sum/) ###### No:`112` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Permutations/README.md b/Algorithms/Permutations/README.md index 34cd7e8..8796ab8 100644 --- a/Algorithms/Permutations/README.md +++ b/Algorithms/Permutations/README.md @@ -1,7 +1,7 @@ # [Permutations](https://leetcode.com/problems/permutations/) ###### No:`46` ###### Difficulty:`Medium` -## JavaScript + ```javascript /** diff --git a/Algorithms/Plus One/README.md b/Algorithms/Plus One/README.md index 365db73..a3343fb 100644 --- a/Algorithms/Plus One/README.md +++ b/Algorithms/Plus One/README.md @@ -1,7 +1,7 @@ # [Plus One](https://leetcode.com/problems/plus-one/) ###### No:`66` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Power of Four/README.md b/Algorithms/Power of Four/README.md index 816eea3..4b84e65 100644 --- a/Algorithms/Power of Four/README.md +++ b/Algorithms/Power of Four/README.md @@ -1,7 +1,7 @@ # [Power of Four](https://leetcode.com/problems/power-of-four/#/description) ###### No:`342` ###### Difficulty:`Easy` -## JavaScript + ```js diff --git a/Algorithms/Power of Three/README.md b/Algorithms/Power of Three/README.md index d60d49f..dc607a5 100644 --- a/Algorithms/Power of Three/README.md +++ b/Algorithms/Power of Three/README.md @@ -1,7 +1,7 @@ # [Power of Three](https://leetcode.com/problems/power-of-three/) ###### No:`326` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Power of Two/README.md b/Algorithms/Power of Two/README.md index fd5860d..632ea69 100644 --- a/Algorithms/Power of Two/README.md +++ b/Algorithms/Power of Two/README.md @@ -1,7 +1,7 @@ # [Power of Two](https://leetcode.com/problems/power-of-two/) ###### No:`231` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Product of Array Except Self/README.md b/Algorithms/Product of Array Except Self/README.md index dab9786..b7a3ae9 100644 --- a/Algorithms/Product of Array Except Self/README.md +++ b/Algorithms/Product of Array Except Self/README.md @@ -1,7 +1,7 @@ # [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/) ###### No:`238` ###### Difficulty:`Medium` -## JavaScript + ```javascript /** diff --git a/Algorithms/Queue Reconstruction by Height/README.md b/Algorithms/Queue Reconstruction by Height/README.md index e2cf4d5..a04efa9 100644 --- a/Algorithms/Queue Reconstruction by Height/README.md +++ b/Algorithms/Queue Reconstruction by Height/README.md @@ -1,7 +1,7 @@ # [Queue Reconstruction by Height](https://leetcode.com/problems/queue-reconstruction-by-height/#/description) ###### No:`406` ###### Difficulty:`Medium` -## JavaScript + ```js diff --git a/Algorithms/Ransom Note/README.md b/Algorithms/Ransom Note/README.md index 747dea1..6fee5f3 100644 --- a/Algorithms/Ransom Note/README.md +++ b/Algorithms/Ransom Note/README.md @@ -1,7 +1,7 @@ # [Ransom Note](https://leetcode.com/problems/ransom-note/) ###### No:`383` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Rectangle Area/README.md b/Algorithms/Rectangle Area/README.md index 98e2878..18f17be 100644 --- a/Algorithms/Rectangle Area/README.md +++ b/Algorithms/Rectangle Area/README.md @@ -1,7 +1,7 @@ # [Rectangle Area](https://leetcode.com/problems/rectangle-area/) ###### No:`223` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Relative Ranks/README.md b/Algorithms/Relative Ranks/README.md index 5ee5f0c..74e6188 100644 --- a/Algorithms/Relative Ranks/README.md +++ b/Algorithms/Relative Ranks/README.md @@ -1,7 +1,7 @@ # [Relative Ranks](https://leetcode.com/problems/relative-ranks/#/description) ###### No:`506` ###### Difficulty:`Easy` -## JavaScript + ```js diff --git a/Algorithms/Remove Duplicates from Sorted Array/README.md b/Algorithms/Remove Duplicates from Sorted Array/README.md index 330ff90..89ece2f 100644 --- a/Algorithms/Remove Duplicates from Sorted Array/README.md +++ b/Algorithms/Remove Duplicates from Sorted Array/README.md @@ -1,7 +1,7 @@ # [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) ###### No:`26` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Remove Duplicates from Sorted List/README.md b/Algorithms/Remove Duplicates from Sorted List/README.md index 8fa7855..dc0b19a 100644 --- a/Algorithms/Remove Duplicates from Sorted List/README.md +++ b/Algorithms/Remove Duplicates from Sorted List/README.md @@ -1,7 +1,7 @@ # [Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list/) ###### No:`83` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Remove Element/README.md b/Algorithms/Remove Element/README.md index f9b81ec..f5bf0ef 100644 --- a/Algorithms/Remove Element/README.md +++ b/Algorithms/Remove Element/README.md @@ -1,7 +1,7 @@ # [Remove Element](https://leetcode.com/problems/remove-element/) ###### No:`27` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Remove Linked List Elements/README.md b/Algorithms/Remove Linked List Elements/README.md index ebcbe4a..773160c 100644 --- a/Algorithms/Remove Linked List Elements/README.md +++ b/Algorithms/Remove Linked List Elements/README.md @@ -1,7 +1,7 @@ # [Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements/) ###### No:`203` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Remove Nth Node From End of List/README.md b/Algorithms/Remove Nth Node From End of List/README.md index 39dd652..f014c26 100644 --- a/Algorithms/Remove Nth Node From End of List/README.md +++ b/Algorithms/Remove Nth Node From End of List/README.md @@ -1,7 +1,7 @@ # [Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/) ###### No:`19` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Repeated Substring Pattern/README.md b/Algorithms/Repeated Substring Pattern/README.md index 74f3a15..34805d9 100644 --- a/Algorithms/Repeated Substring Pattern/README.md +++ b/Algorithms/Repeated Substring Pattern/README.md @@ -1,7 +1,7 @@ # [Repeated Substring Pattern](https://leetcode.com/problems/repeated-substring-pattern/#/description) ###### No:`459` ###### Difficulty:`Easy` -## JavaScript + ```js diff --git a/Algorithms/Reverse Integer/README.md b/Algorithms/Reverse Integer/README.md index 9893006..9626fc5 100644 --- a/Algorithms/Reverse Integer/README.md +++ b/Algorithms/Reverse Integer/README.md @@ -1,7 +1,7 @@ # [Reverse Integer](https://leetcode.com/problems/reverse-integer/) ###### No:`7` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Reverse Linked List II/README.md b/Algorithms/Reverse Linked List II/README.md index 86cc9be..c11dd06 100644 --- a/Algorithms/Reverse Linked List II/README.md +++ b/Algorithms/Reverse Linked List II/README.md @@ -1,7 +1,7 @@ # [Reverse Linked List II](https://leetcode.com/problems/reverse-linked-list-ii/) ###### No:`92` ###### Difficulty:`Medium` -## JavaScript + ```javascript /** diff --git a/Algorithms/Reverse Linked List/README.md b/Algorithms/Reverse Linked List/README.md index 3a9f3ce..1a9aec0 100644 --- a/Algorithms/Reverse Linked List/README.md +++ b/Algorithms/Reverse Linked List/README.md @@ -1,7 +1,7 @@ # [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) ###### No:`206` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Reverse String/README.md b/Algorithms/Reverse String/README.md index 17f5487..2959562 100644 --- a/Algorithms/Reverse String/README.md +++ b/Algorithms/Reverse String/README.md @@ -1,7 +1,7 @@ # [Reverse String](https://leetcode.com/problems/reverse-string/) ###### No:`344` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Reverse Vowels of a String/README.md b/Algorithms/Reverse Vowels of a String/README.md index 4fd1aa8..4fbf546 100644 --- a/Algorithms/Reverse Vowels of a String/README.md +++ b/Algorithms/Reverse Vowels of a String/README.md @@ -1,7 +1,7 @@ # [Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/#/description) ###### No:`345` ###### Difficulty:`Easy` -## JavaScript + ```js diff --git a/Algorithms/Reverse Words in a String/README.md b/Algorithms/Reverse Words in a String/README.md index c134414..1b565f4 100644 --- a/Algorithms/Reverse Words in a String/README.md +++ b/Algorithms/Reverse Words in a String/README.md @@ -1,7 +1,7 @@ # [Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) ###### No:`151` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Roman to Integer/README.md b/Algorithms/Roman to Integer/README.md index 8814420..af02a17 100644 --- a/Algorithms/Roman to Integer/README.md +++ b/Algorithms/Roman to Integer/README.md @@ -1,7 +1,7 @@ # [Roman to Integer](https://leetcode.com/problems/roman-to-integer/) ###### No:`13` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Rotate Array/README.md b/Algorithms/Rotate Array/README.md index 92d6400..939d490 100644 --- a/Algorithms/Rotate Array/README.md +++ b/Algorithms/Rotate Array/README.md @@ -1,7 +1,7 @@ # [Rotate Array](https://leetcode.com/problems/rotate-array/) ###### No:`189` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Same Tree/README.md b/Algorithms/Same Tree/README.md index 44659f8..44d45d5 100644 --- a/Algorithms/Same Tree/README.md +++ b/Algorithms/Same Tree/README.md @@ -1,7 +1,7 @@ # [Same Tree](https://leetcode.com/problems/same-tree/) ###### No:`100` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Set Matrix Zeroes/README.md b/Algorithms/Set Matrix Zeroes/README.md index 1ed5956..418c0cd 100644 --- a/Algorithms/Set Matrix Zeroes/README.md +++ b/Algorithms/Set Matrix Zeroes/README.md @@ -1,7 +1,7 @@ # [Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/) ###### No:`73` ###### Difficulty:`Medium` -## JavaScript + ```javascript /** diff --git a/Algorithms/Single Number/README.md b/Algorithms/Single Number/README.md index 714a3bc..cb4a38b 100644 --- a/Algorithms/Single Number/README.md +++ b/Algorithms/Single Number/README.md @@ -1,7 +1,7 @@ # [Single Number](https://leetcode.com/problems/single-number/) ###### No:`136` ###### Difficulty:`Medium` -## JavaScript + ```javascript /** diff --git a/Algorithms/Sort List/README.md b/Algorithms/Sort List/README.md index 5e74c5d..2a2d19a 100644 --- a/Algorithms/Sort List/README.md +++ b/Algorithms/Sort List/README.md @@ -1,7 +1,7 @@ # [Sort List](https://leetcode.com/problems/sort-list/) ###### No:`148` ###### Difficulty:`Medium` -## JavaScript + ```javascript /** diff --git a/Algorithms/String to Integer (atoi)/README.md b/Algorithms/String to Integer (atoi)/README.md index a58758c..cff1434 100644 --- a/Algorithms/String to Integer (atoi)/README.md +++ b/Algorithms/String to Integer (atoi)/README.md @@ -1,7 +1,7 @@ # [String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/) ###### No:`8` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Sum of Left Leaves/README.md b/Algorithms/Sum of Left Leaves/README.md index dc6cbb5..ac2ff0c 100644 --- a/Algorithms/Sum of Left Leaves/README.md +++ b/Algorithms/Sum of Left Leaves/README.md @@ -1,7 +1,7 @@ # [Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) ###### No:`404` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Summary Ranges/README.md b/Algorithms/Summary Ranges/README.md index dee84bf..080e0f2 100644 --- a/Algorithms/Summary Ranges/README.md +++ b/Algorithms/Summary Ranges/README.md @@ -1,7 +1,7 @@ # [Summary Ranges](https://leetcode.com/problems/summary-ranges/) ###### No:`228` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Swap Nodes in Pairs/README.md b/Algorithms/Swap Nodes in Pairs/README.md index 6dec208..68ee3a7 100644 --- a/Algorithms/Swap Nodes in Pairs/README.md +++ b/Algorithms/Swap Nodes in Pairs/README.md @@ -1,7 +1,7 @@ # [Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) ###### No:`24` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Symmetric Tree/README.md b/Algorithms/Symmetric Tree/README.md index 467f4ee..7d62f44 100644 --- a/Algorithms/Symmetric Tree/README.md +++ b/Algorithms/Symmetric Tree/README.md @@ -1,7 +1,7 @@ # [Symmetric Tree](https://leetcode.com/problems/symmetric-tree/) ###### No:`101` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Third Maximum Number/README.md b/Algorithms/Third Maximum Number/README.md index 23c813f..34ca21e 100644 --- a/Algorithms/Third Maximum Number/README.md +++ b/Algorithms/Third Maximum Number/README.md @@ -1,7 +1,7 @@ # [Third Maximum Number](https://leetcode.com/problems/third-maximum-number/#/description) ###### No:`414` ###### Difficulty:`Easy` -## JavaScript + ```js diff --git a/Algorithms/Top K Frequent Elements/README.md b/Algorithms/Top K Frequent Elements/README.md index 68c18c1..69f986b 100644 --- a/Algorithms/Top K Frequent Elements/README.md +++ b/Algorithms/Top K Frequent Elements/README.md @@ -1,7 +1,7 @@ # [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) ###### No:`347` ###### Difficulty:`Medium` -## JavaScript + ```javascript /** diff --git a/Algorithms/Two Sum/README.md b/Algorithms/Two Sum/README.md index 3f1c708..da17064 100644 --- a/Algorithms/Two Sum/README.md +++ b/Algorithms/Two Sum/README.md @@ -1,7 +1,7 @@ # [Two Sum](https://leetcode.com/problems/two-sum/) ###### No:`1` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Ugly Number/README.md b/Algorithms/Ugly Number/README.md index 6154158..e9f9e04 100644 --- a/Algorithms/Ugly Number/README.md +++ b/Algorithms/Ugly Number/README.md @@ -1,7 +1,7 @@ # [Ugly Number](https://leetcode.com/problems/ugly-number/) ###### No:`263` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Valid Anagram/README.md b/Algorithms/Valid Anagram/README.md index d1ea03e..2ceaf86 100644 --- a/Algorithms/Valid Anagram/README.md +++ b/Algorithms/Valid Anagram/README.md @@ -1,7 +1,7 @@ # [Valid Anagram](https://leetcode.com/problems/valid-anagram/) ###### No:`242` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Valid Palindrome/README.md b/Algorithms/Valid Palindrome/README.md index 1583e59..56cbaa0 100644 --- a/Algorithms/Valid Palindrome/README.md +++ b/Algorithms/Valid Palindrome/README.md @@ -1,7 +1,7 @@ # [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) ###### No:`125` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Valid Parentheses/README.md b/Algorithms/Valid Parentheses/README.md index 6a24feb..4c34ce2 100644 --- a/Algorithms/Valid Parentheses/README.md +++ b/Algorithms/Valid Parentheses/README.md @@ -1,7 +1,7 @@ # [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) ###### No:`20` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/Valid Perfect Square/README.md b/Algorithms/Valid Perfect Square/README.md index 9ed3931..52ec1fa 100644 --- a/Algorithms/Valid Perfect Square/README.md +++ b/Algorithms/Valid Perfect Square/README.md @@ -1,7 +1,7 @@ # [Valid Perfect Square](https://leetcode.com/problems/valid-perfect-square/#/description) ###### No:`367` ###### Difficulty:`Easy` -## JavaScript + ```js diff --git a/Algorithms/Valid Sudoku/README.md b/Algorithms/Valid Sudoku/README.md index 323b9cd..475f14c 100644 --- a/Algorithms/Valid Sudoku/README.md +++ b/Algorithms/Valid Sudoku/README.md @@ -1,7 +1,7 @@ # [Valid Sudoku](https://leetcode.com/problems/valid-sudoku/) ###### No:`36` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** diff --git a/Algorithms/ZigZag Conversion/README.md b/Algorithms/ZigZag Conversion/README.md index 1906b42..d88436d 100644 --- a/Algorithms/ZigZag Conversion/README.md +++ b/Algorithms/ZigZag Conversion/README.md @@ -1,7 +1,7 @@ # [ZigZag Conversion](https://leetcode.com/problems/zigzag-conversion/) ###### No:`6` ###### Difficulty:`Easy` -## JavaScript + ```javascript /** From 872eef3720b6b22c4d48107c0d9c720f830d161c Mon Sep 17 00:00:00 2001 From: Teng DU Date: Tue, 21 Mar 2017 03:57:53 +0800 Subject: [PATCH 107/126] add --- .../README.md | 8 +++++ Algorithms/Count and Say/README.md | 8 +++++ Algorithms/Divide Two Integers/README.md | 8 +++++ Algorithms/Length of Last Word/README.md | 8 +++++ Algorithms/Longest Common Prefix/README.md | 8 +++++ Algorithms/Maximum Subarray/README.md | 8 +++++ Algorithms/Next Permutation/README.md | 8 +++++ Algorithms/Search Insert Position/README.md | 8 +++++ .../Search in Rotated Sorted Array/README.md | 8 +++++ Algorithms/Spiral Matrix/README.md | 8 +++++ Algorithms/Sqrt(x)/README.md | 8 +++++ .../Validate Binary Search Tree/README.md | 8 +++++ README.md | 14 ++++++++- request-problem.js | 30 ++++++++----------- 14 files changed, 121 insertions(+), 19 deletions(-) create mode 100644 Algorithms/Convert Sorted Array to Binary Search Tree/README.md create mode 100644 Algorithms/Count and Say/README.md create mode 100644 Algorithms/Divide Two Integers/README.md create mode 100644 Algorithms/Length of Last Word/README.md create mode 100644 Algorithms/Longest Common Prefix/README.md create mode 100644 Algorithms/Maximum Subarray/README.md create mode 100644 Algorithms/Next Permutation/README.md create mode 100644 Algorithms/Search Insert Position/README.md create mode 100644 Algorithms/Search in Rotated Sorted Array/README.md create mode 100644 Algorithms/Spiral Matrix/README.md create mode 100644 Algorithms/Sqrt(x)/README.md create mode 100644 Algorithms/Validate Binary Search Tree/README.md diff --git a/Algorithms/Convert Sorted Array to Binary Search Tree/README.md b/Algorithms/Convert Sorted Array to Binary Search Tree/README.md new file mode 100644 index 0000000..1d51937 --- /dev/null +++ b/Algorithms/Convert Sorted Array to Binary Search Tree/README.md @@ -0,0 +1,8 @@ +# [Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/#/description) +###### No:`108` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/Algorithms/Count and Say/README.md b/Algorithms/Count and Say/README.md new file mode 100644 index 0000000..86092ea --- /dev/null +++ b/Algorithms/Count and Say/README.md @@ -0,0 +1,8 @@ +# [Count and Say](https://leetcode.com/problems/count-and-say/#/description) +###### No:`38` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/Algorithms/Divide Two Integers/README.md b/Algorithms/Divide Two Integers/README.md new file mode 100644 index 0000000..936e4a4 --- /dev/null +++ b/Algorithms/Divide Two Integers/README.md @@ -0,0 +1,8 @@ +# [Divide Two Integers](https://leetcode.com/problems/divide-two-integers/#/description) +###### No:`29` +###### Difficulty:`Medium` +## JavaScript + + +```js +``` diff --git a/Algorithms/Length of Last Word/README.md b/Algorithms/Length of Last Word/README.md new file mode 100644 index 0000000..6825964 --- /dev/null +++ b/Algorithms/Length of Last Word/README.md @@ -0,0 +1,8 @@ +# [Length of Last Word](https://leetcode.com/problems/length-of-last-word/#/description) +###### No:`58` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/Algorithms/Longest Common Prefix/README.md b/Algorithms/Longest Common Prefix/README.md new file mode 100644 index 0000000..c7f94eb --- /dev/null +++ b/Algorithms/Longest Common Prefix/README.md @@ -0,0 +1,8 @@ +# [Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/#/description) +###### No:`14` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/Algorithms/Maximum Subarray/README.md b/Algorithms/Maximum Subarray/README.md new file mode 100644 index 0000000..259078f --- /dev/null +++ b/Algorithms/Maximum Subarray/README.md @@ -0,0 +1,8 @@ +# [Maximum Subarray](https://leetcode.com/problems/maximum-subarray/#/description) +###### No:`53` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/Algorithms/Next Permutation/README.md b/Algorithms/Next Permutation/README.md new file mode 100644 index 0000000..80e0b27 --- /dev/null +++ b/Algorithms/Next Permutation/README.md @@ -0,0 +1,8 @@ +# [Next Permutation](https://leetcode.com/problems/next-permutation/#/description) +###### No:`31` +###### Difficulty:`Medium` +## JavaScript + + +```js +``` diff --git a/Algorithms/Search Insert Position/README.md b/Algorithms/Search Insert Position/README.md new file mode 100644 index 0000000..84c9dc7 --- /dev/null +++ b/Algorithms/Search Insert Position/README.md @@ -0,0 +1,8 @@ +# [Search Insert Position](https://leetcode.com/problems/search-insert-position/#/description) +###### No:`35` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/Algorithms/Search in Rotated Sorted Array/README.md b/Algorithms/Search in Rotated Sorted Array/README.md new file mode 100644 index 0000000..d426cb9 --- /dev/null +++ b/Algorithms/Search in Rotated Sorted Array/README.md @@ -0,0 +1,8 @@ +# [Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/#/description) +###### No:`33` +###### Difficulty:`Medium` +## JavaScript + + +```js +``` diff --git a/Algorithms/Spiral Matrix/README.md b/Algorithms/Spiral Matrix/README.md new file mode 100644 index 0000000..73c0dfc --- /dev/null +++ b/Algorithms/Spiral Matrix/README.md @@ -0,0 +1,8 @@ +# [Spiral Matrix](https://leetcode.com/problems/spiral-matrix/#/description) +###### No:`54` +###### Difficulty:`Medium` +## JavaScript + + +```js +``` diff --git a/Algorithms/Sqrt(x)/README.md b/Algorithms/Sqrt(x)/README.md new file mode 100644 index 0000000..0898ff8 --- /dev/null +++ b/Algorithms/Sqrt(x)/README.md @@ -0,0 +1,8 @@ +# [Sqrt(x)](https://leetcode.com/problems/sqrtx/#/description) +###### No:`69` +###### Difficulty:`Easy` +## JavaScript + + +```js +``` diff --git a/Algorithms/Validate Binary Search Tree/README.md b/Algorithms/Validate Binary Search Tree/README.md new file mode 100644 index 0000000..43c1f3e --- /dev/null +++ b/Algorithms/Validate Binary Search Tree/README.md @@ -0,0 +1,8 @@ +# [Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/) +###### No:`98` +###### Difficulty:`Medium` +## JavaScript + + +```js +``` diff --git a/README.md b/README.md index b8908d2..2d4b4de 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ leetcode ======== -#### Total solved: 140 (Easy: 104 Medium: 35 Hard: 1 ) +#### Total solved: 152 (Easy: 111 Medium: 40 Hard: 1 ) My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) @@ -13,6 +13,7 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |9|Palindrome Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Palindrome%20Number)|Easy| |12|Integer to Roman|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Integer%20to%20Roman)|Medium| |13|Roman to Integer|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Roman%20to%20Integer)|Easy| +|14|Longest Common Prefix|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Longest%20Common%20Prefix)|Easy| |15|3Sum|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/3Sum)|Medium| |16|3Sum Closest|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/3Sum%20Closest)|Medium| |17|Letter Combinations of a Phone Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Letter%20Combinations%20of%20a%20Phone%20Number)|Medium| @@ -24,19 +25,30 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |26|Remove Duplicates from Sorted Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Duplicates%20from%20Sorted%20Array)|Easy| |27|Remove Element|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Element)|Easy| |28|Implement strStr()|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Implement%20strStr())|Easy| +|29|Divide Two Integers|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Divide%20Two%20Integers)|Medium| +|31|Next Permutation|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Next%20Permutation)|Medium| +|33|Search in Rotated Sorted Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Search%20in%20Rotated%20Sorted%20Array)|Medium| +|35|Search Insert Position|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Search%20Insert%20Position)|Easy| |36|Valid Sudoku|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Sudoku)|Easy| +|38|Count and Say|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Count%20and%20Say)|Easy| |46|Permutations|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Permutations)|Medium| +|53|Maximum Subarray|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Maximum%20Subarray)|Easy| +|54|Spiral Matrix|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Spiral%20Matrix)|Medium| +|58|Length of Last Word|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Length%20of%20Last%20Word)|Easy| |66|Plus One|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Plus%20One)|Easy| +|69|Sqrt(x)|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Sqrt(x))|Easy| |70|Climbing Stairs|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Climbing%20Stairs)|Easy| |73|Set Matrix Zeroes|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Set%20Matrix%20Zeroes)|Medium| |83|Remove Duplicates from Sorted List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Duplicates%20from%20Sorted%20List)|Easy| |88|Merge Sorted Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Merge%20Sorted%20Array)|Easy| |92|Reverse Linked List II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Linked%20List%20II)|Medium| +|98|Validate Binary Search Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Validate%20Binary%20Search%20Tree)|Medium| |100|Same Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Same%20Tree)|Easy| |101|Symmetric Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Symmetric%20Tree)|Easy| |102|Binary Tree Level Order Traversal|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Binary%20Tree%20Level%20Order%20Traversal)|Easy| |104|Maximum Depth of Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Maximum%20Depth%20of%20Binary%20Tree)|Easy| |107|Binary Tree Level Order Traversal II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Binary%20Tree%20Level%20Order%20Traversal%20II)|Easy| +|108|Convert Sorted Array to Binary Search Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Convert%20Sorted%20Array%20to%20Binary%20Search%20Tree)|Easy| |110|Balanced Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Balanced%20Binary%20Tree)|Easy| |111|Minimum Depth of Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Minimum%20Depth%20of%20Binary%20Tree)|Easy| |112|Path Sum|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Path%20Sum)|Easy| diff --git a/request-problem.js b/request-problem.js index 9f8c340..4e90edb 100644 --- a/request-problem.js +++ b/request-problem.js @@ -3,24 +3,18 @@ var request = require('request'); var cheerio = require('cheerio'); var urls = [ - 'https://leetcode.com/problems/nth-digit/#/description', - 'https://leetcode.com/problems/queue-reconstruction-by-height/#/description', - 'https://leetcode.com/problems/arithmetic-slices/#/description', - 'https://leetcode.com/problems/third-maximum-number/#/description', - 'https://leetcode.com/problems/number-of-segments-in-a-string/#/description', - 'https://leetcode.com/problems/path-sum-iii/#/description', - 'https://leetcode.com/problems/arranging-coins/#/description', - 'https://leetcode.com/problems/find-all-duplicates-in-an-array/#/description', - 'https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/#/description', - 'https://leetcode.com/problems/delete-node-in-a-bst/#/description', - 'https://leetcode.com/problems/minimum-moves-to-equal-array-elements/#/description', - 'https://leetcode.com/problems/repeated-substring-pattern/#/description', - 'https://leetcode.com/problems/hamming-distance/#/description', - 'https://leetcode.com/problems/first-bad-version/#/description', - 'https://leetcode.com/problems/power-of-four/#/description', - 'https://leetcode.com/problems/reverse-vowels-of-a-string/#/description', - 'https://leetcode.com/problems/valid-perfect-square/#/description', - 'https://leetcode.com/problems/guess-number-higher-or-lower/#/description' + 'https://leetcode.com/problems/longest-common-prefix/#/description', + 'https://leetcode.com/problems/divide-two-integers/#/description', + 'https://leetcode.com/problems/next-permutation/#/description', + 'https://leetcode.com/problems/search-in-rotated-sorted-array/#/description', + 'https://leetcode.com/problems/search-insert-position/#/description', + 'https://leetcode.com/problems/count-and-say/#/description', + 'https://leetcode.com/problems/maximum-subarray/#/description', + 'https://leetcode.com/problems/spiral-matrix/#/description', + 'https://leetcode.com/problems/length-of-last-word/#/description', + 'https://leetcode.com/problems/sqrtx/#/description', + 'https://leetcode.com/problems/validate-binary-search-tree/', + 'https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/#/description', ]; urls.forEach((url) => { From ad93bbf98256958c08ac26254cdd2988953e591e Mon Sep 17 00:00:00 2001 From: Teng DU Date: Tue, 21 Mar 2017 04:11:09 +0800 Subject: [PATCH 108/126] from ase to desc --- README.md | 302 ++++++++++++++++++++++++------------------------- generate-md.js | 2 +- 2 files changed, 152 insertions(+), 152 deletions(-) diff --git a/README.md b/README.md index 2d4b4de..91af914 100644 --- a/README.md +++ b/README.md @@ -6,155 +6,155 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) | No | Title | Source Code | Difficulty | |----| ----- | -------- | ---------- | -|1|Two Sum|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Two%20Sum)|Easy| -|6|ZigZag Conversion|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/ZigZag%20Conversion)|Easy| -|7|Reverse Integer|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Integer)|Easy| -|8|String to Integer (atoi)|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/String%20to%20Integer%20(atoi))|Easy| -|9|Palindrome Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Palindrome%20Number)|Easy| -|12|Integer to Roman|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Integer%20to%20Roman)|Medium| -|13|Roman to Integer|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Roman%20to%20Integer)|Easy| -|14|Longest Common Prefix|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Longest%20Common%20Prefix)|Easy| -|15|3Sum|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/3Sum)|Medium| -|16|3Sum Closest|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/3Sum%20Closest)|Medium| -|17|Letter Combinations of a Phone Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Letter%20Combinations%20of%20a%20Phone%20Number)|Medium| -|19|Remove Nth Node From End of List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Nth%20Node%20From%20End%20of%20List)|Easy| -|20|Valid Parentheses|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Parentheses)|Easy| -|21|Merge Two Sorted Lists|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Merge%20Two%20Sorted%20Lists)|Easy| -|22|Generate Parentheses|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Generate%20Parentheses)|Medium| -|24|Swap Nodes in Pairs|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Swap%20Nodes%20in%20Pairs)|Easy| -|26|Remove Duplicates from Sorted Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Duplicates%20from%20Sorted%20Array)|Easy| -|27|Remove Element|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Element)|Easy| -|28|Implement strStr()|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Implement%20strStr())|Easy| -|29|Divide Two Integers|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Divide%20Two%20Integers)|Medium| -|31|Next Permutation|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Next%20Permutation)|Medium| -|33|Search in Rotated Sorted Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Search%20in%20Rotated%20Sorted%20Array)|Medium| -|35|Search Insert Position|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Search%20Insert%20Position)|Easy| -|36|Valid Sudoku|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Sudoku)|Easy| -|38|Count and Say|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Count%20and%20Say)|Easy| -|46|Permutations|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Permutations)|Medium| -|53|Maximum Subarray|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Maximum%20Subarray)|Easy| -|54|Spiral Matrix|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Spiral%20Matrix)|Medium| -|58|Length of Last Word|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Length%20of%20Last%20Word)|Easy| -|66|Plus One|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Plus%20One)|Easy| -|69|Sqrt(x)|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Sqrt(x))|Easy| -|70|Climbing Stairs|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Climbing%20Stairs)|Easy| -|73|Set Matrix Zeroes|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Set%20Matrix%20Zeroes)|Medium| -|83|Remove Duplicates from Sorted List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Duplicates%20from%20Sorted%20List)|Easy| -|88|Merge Sorted Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Merge%20Sorted%20Array)|Easy| -|92|Reverse Linked List II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Linked%20List%20II)|Medium| -|98|Validate Binary Search Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Validate%20Binary%20Search%20Tree)|Medium| -|100|Same Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Same%20Tree)|Easy| -|101|Symmetric Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Symmetric%20Tree)|Easy| -|102|Binary Tree Level Order Traversal|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Binary%20Tree%20Level%20Order%20Traversal)|Easy| -|104|Maximum Depth of Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Maximum%20Depth%20of%20Binary%20Tree)|Easy| -|107|Binary Tree Level Order Traversal II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Binary%20Tree%20Level%20Order%20Traversal%20II)|Easy| -|108|Convert Sorted Array to Binary Search Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Convert%20Sorted%20Array%20to%20Binary%20Search%20Tree)|Easy| -|110|Balanced Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Balanced%20Binary%20Tree)|Easy| -|111|Minimum Depth of Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Minimum%20Depth%20of%20Binary%20Tree)|Easy| -|112|Path Sum|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Path%20Sum)|Easy| -|118|Pascal's Triangle|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Pascal's%20Triangle)|Easy| -|119|Pascal's Triangle II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Pascal's%20Triangle%20II)|Easy| -|121|Best Time to Buy and Sell Stock|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Best%20Time%20to%20Buy%20and%20Sell%20Stock)|Easy| -|122|Best Time to Buy and Sell Stock II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Best%20Time%20to%20Buy%20and%20Sell%20Stock%20II)|Medium| -|125|Valid Palindrome|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Palindrome)|Easy| -|136|Single Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Single%20Number)|Medium| -|138|Copy List with Random Pointer|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Copy%20List%20with%20Random%20Pointer)|Medium| -|141|Linked List Cycle|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Linked%20List%20Cycle)|Easy| -|144|Binary Tree Preorder Traversal|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Binary%20Tree%20Preorder%20Traversal)|Medium| -|148|Sort List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Sort%20List)|Medium| -|149|Max Points on a Line|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Max%20Points%20on%20a%20Line)|Hard| -|150|Evaluate Reverse Polish Notation|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Evaluate%20Reverse%20Polish%20Notation)|Medium| -|151|Reverse Words in a String|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Words%20in%20a%20String)|Easy| -|155|Min Stack|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Min%20Stack)|Easy| -|160|Intersection of Two Linked Lists|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Intersection%20of%20Two%20Linked%20Lists)|Easy| -|165|Compare Version Numbers|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Compare%20Version%20Numbers)|Easy| -|168|Excel Sheet Column Title|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Excel%20Sheet%20Column%20Title)|Easy| -|169|Majority Element|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Majority%20Element)|Easy| -|171|Excel Sheet Column Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Excel%20Sheet%20Column%20Number)|Easy| -|172|Factorial Trailing Zeroes|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Factorial%20Trailing%20Zeroes)|Easy| -|179|Largest Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Largest%20Number)|Medium| -|189|Rotate Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Rotate%20Array)|Easy| -|191|Number of 1 Bits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Number%20of%201%20Bits)|Easy| -|198|House Robber|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/House%20Robber)|Easy| -|202|Happy Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Happy%20Number)|Easy| -|203|Remove Linked List Elements|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Linked%20List%20Elements)|Easy| -|204|Count Primes|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Count%20Primes)|Easy| -|205|Isomorphic Strings|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Isomorphic%20Strings)|Easy| -|206|Reverse Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Linked%20List)|Easy| -|217|Contains Duplicate|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Contains%20Duplicate)|Easy| -|219|Contains Duplicate II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Contains%20Duplicate%20II)|Easy| -|223|Rectangle Area|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Rectangle%20Area)|Easy| -|226|Invert Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Invert%20Binary%20Tree)|Easy| -|228|Summary Ranges|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Summary%20Ranges)|Easy| -|231|Power of Two|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Power%20of%20Two)|Easy| -|232|Implement Queue using Stacks|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Implement%20Queue%20using%20Stacks)|Easy| -|234|Palindrome Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Palindrome%20Linked%20List)|Easy| -|235|Lowest Common Ancestor of a Binary Search Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree)|Easy| -|237|Delete Node in a Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Delete%20Node%20in%20a%20Linked%20List)|Easy| -|238|Product of Array Except Self|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Product%20of%20Array%20Except%20Self)|Medium| -|242|Valid Anagram|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Anagram)|Easy| -|257|Binary Tree Paths|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Binary%20Tree%20Paths)|Easy| -|258|Add Digits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Add%20Digits)|Easy| -|263|Ugly Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Ugly%20Number)|Easy| -|268|Missing Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Missing%20Number)|Medium| -|278|First Bad Version|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/First%20Bad%20Version)|Easy| -|283|Move Zeroes|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Move%20Zeroes)|Easy| -|292|Nim Game|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Nim%20Game)|Easy| -|319|Add Digits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Add%20Digits)|Medium| -|326|Power of Three|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Power%20of%20Three)|Easy| -|328|Odd Even Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Odd%20Even%20Linked%20List)|Medium| -|338|Counting Bits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Counting%20Bits)|Medium| -|342|Power of Four|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Power%20of%20Four)|Easy| -|343|Integer Break|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Integer%20Break)|Medium| -|344|Reverse String|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20String)|Easy| -|345|Reverse Vowels of a String|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Vowels%20of%20a%20String)|Easy| -|347|Top K Frequent Elements|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Top%20K%20Frequent%20Elements)|Medium| -|349|Intersection of Two Arrays|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Intersection%20of%20Two%20Arrays)|Easy| -|350|Intersection of Two Arrays II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Intersection%20of%20Two%20Arrays%20II)|Easy| -|367|Valid Perfect Square|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Perfect%20Square)|Easy| -|374|Guess Number Higher or Lower|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Guess%20Number%20Higher%20or%20Lower)|Easy| -|383|Ransom Note|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Ransom%20Note)|Easy| -|387|First Unique Character in a String|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/First%20Unique%20Character%20in%20a%20String)|Easy| -|389|Find the Difference|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Find%20the%20Difference)|Easy| -|400|Nth Digit|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Nth%20Digit)|Easy| -|401|Binary Watch|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Binary%20Watch)|Easy| -|404|Sum of Left Leaves|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Sum%20of%20Left%20Leaves)|Easy| -|406|Queue Reconstruction by Height|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Queue%20Reconstruction%20by%20Height)|Medium| -|409|Longest Palindrome|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Longest%20Palindrome)|Easy| -|412|Fizz Buzz|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Fizz%20Buzz)|Easy| -|413|Arithmetic Slices|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Arithmetic%20Slices)|Medium| -|414|Third Maximum Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Third%20Maximum%20Number)|Easy| -|415|Add Strings|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Add%20Strings)|Easy| -|434|Number of Segments in a String|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Number%20of%20Segments%20in%20a%20String)|Easy| -|437|Path Sum III|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Path%20Sum%20III)|Easy| -|441|Arranging Coins|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Arranging%20Coins)|Easy| -|442|Find All Duplicates in an Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Find%20All%20Duplicates%20in%20an%20Array)|Medium| -|447|Number of Boomerangs|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Number%20of%20Boomerangs)|Easy| -|448|Find All Numbers Disappeared in an Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Find%20All%20Numbers%20Disappeared%20in%20an%20Array)|Easy| -|450|Delete Node in a BST|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Delete%20Node%20in%20a%20BST)|Medium| -|453|Minimum Moves to Equal Array Elements|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Minimum%20Moves%20to%20Equal%20Array%20Elements)|Easy| -|455|Assign Cookies|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Assign%20Cookies)|Easy| -|459|Repeated Substring Pattern|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Repeated%20Substring%20Pattern)|Easy| -|461|Hamming Distance|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Hamming%20Distance)|Easy| -|463|Add Digits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Add%20Digits)|Easy| -|476|Number Complement|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Number%20Complement)|Easy| -|482|License Key Formatting|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/License%20Key%20Formatting)|Medium| -|485|Max Consecutive Ones|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Max%20Consecutive%20Ones)|Easy| -|492|Construct the Rectangle|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Construct%20the%20Rectangle)|Easy| -|496|Next Greater Element I|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Next%20Greater%20Element%20I)|Easy| -|500|Keyboard Row|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Keyboard%20Row)|Easy| -|501|Find Mode in Binary Search Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Find%20Mode%20in%20Binary%20Search%20Tree)|Easy| -|503|Next Greater Element II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Next%20Greater%20Element%20II)|Medium| -|504|Base 7|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Base%207)|Easy| -|506|Relative Ranks|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Relative%20Ranks)|Easy| -|508|Most Frequent Subtree Sum|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Most%20Frequent%20Subtree%20Sum)|Medium| -|513|Find Bottom Left Tree Value|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Find%20Bottom%20Left%20Tree%20Value)|Medium| -|515|Find Largest Value in Each Tree Row|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Find%20Largest%20Value%20in%20Each%20Tree%20Row)|Medium| -|520|Detect Capital|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Detect%20Capital)|Easy| -|526|Beautiful Arrangement|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Beautiful%20Arrangement)|Medium| -|529|Minesweeper|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Minesweeper)|Medium| -|530|Minimum Absolute Difference in BST|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Minimum%20Absolute%20Difference%20in%20BST)|Easy| -|532|K-diff Pairs in an Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/K-diff%20Pairs%20in%20an%20Array)|Easy| -|542|01 Matrix|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/01%20Matrix)|Medium| -|543|Diameter of Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Diameter%20of%20Binary%20Tree)|Easy| |544|Output Contest Matches|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Output%20Contest%20Matches)|Medium| +|543|Diameter of Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Diameter%20of%20Binary%20Tree)|Easy| +|542|01 Matrix|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/01%20Matrix)|Medium| +|532|K-diff Pairs in an Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/K-diff%20Pairs%20in%20an%20Array)|Easy| +|530|Minimum Absolute Difference in BST|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Minimum%20Absolute%20Difference%20in%20BST)|Easy| +|529|Minesweeper|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Minesweeper)|Medium| +|526|Beautiful Arrangement|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Beautiful%20Arrangement)|Medium| +|520|Detect Capital|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Detect%20Capital)|Easy| +|515|Find Largest Value in Each Tree Row|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Find%20Largest%20Value%20in%20Each%20Tree%20Row)|Medium| +|513|Find Bottom Left Tree Value|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Find%20Bottom%20Left%20Tree%20Value)|Medium| +|508|Most Frequent Subtree Sum|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Most%20Frequent%20Subtree%20Sum)|Medium| +|506|Relative Ranks|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Relative%20Ranks)|Easy| +|504|Base 7|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Base%207)|Easy| +|503|Next Greater Element II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Next%20Greater%20Element%20II)|Medium| +|501|Find Mode in Binary Search Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Find%20Mode%20in%20Binary%20Search%20Tree)|Easy| +|500|Keyboard Row|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Keyboard%20Row)|Easy| +|496|Next Greater Element I|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Next%20Greater%20Element%20I)|Easy| +|492|Construct the Rectangle|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Construct%20the%20Rectangle)|Easy| +|485|Max Consecutive Ones|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Max%20Consecutive%20Ones)|Easy| +|482|License Key Formatting|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/License%20Key%20Formatting)|Medium| +|476|Number Complement|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Number%20Complement)|Easy| +|463|Add Digits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Add%20Digits)|Easy| +|461|Hamming Distance|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Hamming%20Distance)|Easy| +|459|Repeated Substring Pattern|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Repeated%20Substring%20Pattern)|Easy| +|455|Assign Cookies|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Assign%20Cookies)|Easy| +|453|Minimum Moves to Equal Array Elements|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Minimum%20Moves%20to%20Equal%20Array%20Elements)|Easy| +|450|Delete Node in a BST|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Delete%20Node%20in%20a%20BST)|Medium| +|448|Find All Numbers Disappeared in an Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Find%20All%20Numbers%20Disappeared%20in%20an%20Array)|Easy| +|447|Number of Boomerangs|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Number%20of%20Boomerangs)|Easy| +|442|Find All Duplicates in an Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Find%20All%20Duplicates%20in%20an%20Array)|Medium| +|441|Arranging Coins|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Arranging%20Coins)|Easy| +|437|Path Sum III|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Path%20Sum%20III)|Easy| +|434|Number of Segments in a String|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Number%20of%20Segments%20in%20a%20String)|Easy| +|415|Add Strings|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Add%20Strings)|Easy| +|414|Third Maximum Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Third%20Maximum%20Number)|Easy| +|413|Arithmetic Slices|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Arithmetic%20Slices)|Medium| +|412|Fizz Buzz|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Fizz%20Buzz)|Easy| +|409|Longest Palindrome|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Longest%20Palindrome)|Easy| +|406|Queue Reconstruction by Height|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Queue%20Reconstruction%20by%20Height)|Medium| +|404|Sum of Left Leaves|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Sum%20of%20Left%20Leaves)|Easy| +|401|Binary Watch|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Binary%20Watch)|Easy| +|400|Nth Digit|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Nth%20Digit)|Easy| +|389|Find the Difference|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Find%20the%20Difference)|Easy| +|387|First Unique Character in a String|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/First%20Unique%20Character%20in%20a%20String)|Easy| +|383|Ransom Note|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Ransom%20Note)|Easy| +|374|Guess Number Higher or Lower|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Guess%20Number%20Higher%20or%20Lower)|Easy| +|367|Valid Perfect Square|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Perfect%20Square)|Easy| +|350|Intersection of Two Arrays II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Intersection%20of%20Two%20Arrays%20II)|Easy| +|349|Intersection of Two Arrays|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Intersection%20of%20Two%20Arrays)|Easy| +|347|Top K Frequent Elements|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Top%20K%20Frequent%20Elements)|Medium| +|345|Reverse Vowels of a String|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Vowels%20of%20a%20String)|Easy| +|344|Reverse String|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20String)|Easy| +|343|Integer Break|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Integer%20Break)|Medium| +|342|Power of Four|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Power%20of%20Four)|Easy| +|338|Counting Bits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Counting%20Bits)|Medium| +|328|Odd Even Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Odd%20Even%20Linked%20List)|Medium| +|326|Power of Three|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Power%20of%20Three)|Easy| +|319|Add Digits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Add%20Digits)|Medium| +|292|Nim Game|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Nim%20Game)|Easy| +|283|Move Zeroes|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Move%20Zeroes)|Easy| +|278|First Bad Version|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/First%20Bad%20Version)|Easy| +|268|Missing Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Missing%20Number)|Medium| +|263|Ugly Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Ugly%20Number)|Easy| +|258|Add Digits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Add%20Digits)|Easy| +|257|Binary Tree Paths|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Binary%20Tree%20Paths)|Easy| +|242|Valid Anagram|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Anagram)|Easy| +|238|Product of Array Except Self|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Product%20of%20Array%20Except%20Self)|Medium| +|237|Delete Node in a Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Delete%20Node%20in%20a%20Linked%20List)|Easy| +|235|Lowest Common Ancestor of a Binary Search Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree)|Easy| +|234|Palindrome Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Palindrome%20Linked%20List)|Easy| +|232|Implement Queue using Stacks|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Implement%20Queue%20using%20Stacks)|Easy| +|231|Power of Two|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Power%20of%20Two)|Easy| +|228|Summary Ranges|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Summary%20Ranges)|Easy| +|226|Invert Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Invert%20Binary%20Tree)|Easy| +|223|Rectangle Area|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Rectangle%20Area)|Easy| +|219|Contains Duplicate II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Contains%20Duplicate%20II)|Easy| +|217|Contains Duplicate|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Contains%20Duplicate)|Easy| +|206|Reverse Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Linked%20List)|Easy| +|205|Isomorphic Strings|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Isomorphic%20Strings)|Easy| +|204|Count Primes|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Count%20Primes)|Easy| +|203|Remove Linked List Elements|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Linked%20List%20Elements)|Easy| +|202|Happy Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Happy%20Number)|Easy| +|198|House Robber|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/House%20Robber)|Easy| +|191|Number of 1 Bits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Number%20of%201%20Bits)|Easy| +|189|Rotate Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Rotate%20Array)|Easy| +|179|Largest Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Largest%20Number)|Medium| +|172|Factorial Trailing Zeroes|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Factorial%20Trailing%20Zeroes)|Easy| +|171|Excel Sheet Column Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Excel%20Sheet%20Column%20Number)|Easy| +|169|Majority Element|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Majority%20Element)|Easy| +|168|Excel Sheet Column Title|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Excel%20Sheet%20Column%20Title)|Easy| +|165|Compare Version Numbers|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Compare%20Version%20Numbers)|Easy| +|160|Intersection of Two Linked Lists|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Intersection%20of%20Two%20Linked%20Lists)|Easy| +|155|Min Stack|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Min%20Stack)|Easy| +|151|Reverse Words in a String|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Words%20in%20a%20String)|Easy| +|150|Evaluate Reverse Polish Notation|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Evaluate%20Reverse%20Polish%20Notation)|Medium| +|149|Max Points on a Line|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Max%20Points%20on%20a%20Line)|Hard| +|148|Sort List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Sort%20List)|Medium| +|144|Binary Tree Preorder Traversal|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Binary%20Tree%20Preorder%20Traversal)|Medium| +|141|Linked List Cycle|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Linked%20List%20Cycle)|Easy| +|138|Copy List with Random Pointer|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Copy%20List%20with%20Random%20Pointer)|Medium| +|136|Single Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Single%20Number)|Medium| +|125|Valid Palindrome|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Palindrome)|Easy| +|122|Best Time to Buy and Sell Stock II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Best%20Time%20to%20Buy%20and%20Sell%20Stock%20II)|Medium| +|121|Best Time to Buy and Sell Stock|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Best%20Time%20to%20Buy%20and%20Sell%20Stock)|Easy| +|119|Pascal's Triangle II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Pascal's%20Triangle%20II)|Easy| +|118|Pascal's Triangle|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Pascal's%20Triangle)|Easy| +|112|Path Sum|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Path%20Sum)|Easy| +|111|Minimum Depth of Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Minimum%20Depth%20of%20Binary%20Tree)|Easy| +|110|Balanced Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Balanced%20Binary%20Tree)|Easy| +|108|Convert Sorted Array to Binary Search Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Convert%20Sorted%20Array%20to%20Binary%20Search%20Tree)|Easy| +|107|Binary Tree Level Order Traversal II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Binary%20Tree%20Level%20Order%20Traversal%20II)|Easy| +|104|Maximum Depth of Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Maximum%20Depth%20of%20Binary%20Tree)|Easy| +|102|Binary Tree Level Order Traversal|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Binary%20Tree%20Level%20Order%20Traversal)|Easy| +|101|Symmetric Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Symmetric%20Tree)|Easy| +|100|Same Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Same%20Tree)|Easy| +|98|Validate Binary Search Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Validate%20Binary%20Search%20Tree)|Medium| +|92|Reverse Linked List II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Linked%20List%20II)|Medium| +|88|Merge Sorted Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Merge%20Sorted%20Array)|Easy| +|83|Remove Duplicates from Sorted List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Duplicates%20from%20Sorted%20List)|Easy| +|73|Set Matrix Zeroes|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Set%20Matrix%20Zeroes)|Medium| +|70|Climbing Stairs|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Climbing%20Stairs)|Easy| +|69|Sqrt(x)|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Sqrt(x))|Easy| +|66|Plus One|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Plus%20One)|Easy| +|58|Length of Last Word|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Length%20of%20Last%20Word)|Easy| +|54|Spiral Matrix|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Spiral%20Matrix)|Medium| +|53|Maximum Subarray|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Maximum%20Subarray)|Easy| +|46|Permutations|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Permutations)|Medium| +|38|Count and Say|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Count%20and%20Say)|Easy| +|36|Valid Sudoku|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Sudoku)|Easy| +|35|Search Insert Position|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Search%20Insert%20Position)|Easy| +|33|Search in Rotated Sorted Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Search%20in%20Rotated%20Sorted%20Array)|Medium| +|31|Next Permutation|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Next%20Permutation)|Medium| +|29|Divide Two Integers|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Divide%20Two%20Integers)|Medium| +|28|Implement strStr()|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Implement%20strStr())|Easy| +|27|Remove Element|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Element)|Easy| +|26|Remove Duplicates from Sorted Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Duplicates%20from%20Sorted%20Array)|Easy| +|24|Swap Nodes in Pairs|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Swap%20Nodes%20in%20Pairs)|Easy| +|22|Generate Parentheses|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Generate%20Parentheses)|Medium| +|21|Merge Two Sorted Lists|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Merge%20Two%20Sorted%20Lists)|Easy| +|20|Valid Parentheses|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Parentheses)|Easy| +|19|Remove Nth Node From End of List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Nth%20Node%20From%20End%20of%20List)|Easy| +|17|Letter Combinations of a Phone Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Letter%20Combinations%20of%20a%20Phone%20Number)|Medium| +|16|3Sum Closest|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/3Sum%20Closest)|Medium| +|15|3Sum|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/3Sum)|Medium| +|14|Longest Common Prefix|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Longest%20Common%20Prefix)|Easy| +|13|Roman to Integer|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Roman%20to%20Integer)|Easy| +|12|Integer to Roman|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Integer%20to%20Roman)|Medium| +|9|Palindrome Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Palindrome%20Number)|Easy| +|8|String to Integer (atoi)|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/String%20to%20Integer%20(atoi))|Easy| +|7|Reverse Integer|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Integer)|Easy| +|6|ZigZag Conversion|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/ZigZag%20Conversion)|Easy| +|1|Two Sum|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Two%20Sum)|Easy| diff --git a/generate-md.js b/generate-md.js index 1085f1d..fab5af6 100644 --- a/generate-md.js +++ b/generate-md.js @@ -44,7 +44,7 @@ targetDirs = fs.readdir(dir, function(err, files) { proxy.once('readDone', function() { ret.sort(function(a, b) { - return a.no - b.no; + return b.no - a.no; }); var content = [ From 8b7833c71bcc226e0ff12fb16460e0eb52bea283 Mon Sep 17 00:00:00 2001 From: Teng DU Date: Tue, 21 Mar 2017 04:16:47 +0800 Subject: [PATCH 109/126] update --- .../Convert BST to Greater Tree/README.md | 49 +++++++++++++++++++ README.md | 3 +- 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 Algorithms/Convert BST to Greater Tree/README.md diff --git a/Algorithms/Convert BST to Greater Tree/README.md b/Algorithms/Convert BST to Greater Tree/README.md new file mode 100644 index 0000000..b199004 --- /dev/null +++ b/Algorithms/Convert BST to Greater Tree/README.md @@ -0,0 +1,49 @@ +# [Convert BST to Greater Tree](https://leetcode.com/problems/convert-bst-to-greater-tree/#/description) +###### No:`538` +###### Difficulty:`Medium` + + +### LeetCode Weekly Contest 24 +### 538. Convert BST to Greater Tree +Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST. + +#### Example +``` +Input: The root of a Binary Search Tree like this: + 5 + / \ + 2 13 + +Output: The root of a Greater Tree like this: + 18 + / \ + 20 13 +``` +#### Solution +```js +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {TreeNode} + */ +var convertBST = function(root) { + var sum = 0; + function traverse (root) { + if (root === null) return; + traverse(root.right); + root.val = root.val + sum; + sum = root.val; + traverse(root.left); + } + + traverse(root); + return root; +}; +``` + diff --git a/README.md b/README.md index 91af914..64c367a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ leetcode ======== -#### Total solved: 152 (Easy: 111 Medium: 40 Hard: 1 ) +#### Total solved: 153 (Easy: 111 Medium: 41 Hard: 1 ) My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) @@ -9,6 +9,7 @@ My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) |544|Output Contest Matches|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Output%20Contest%20Matches)|Medium| |543|Diameter of Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Diameter%20of%20Binary%20Tree)|Easy| |542|01 Matrix|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/01%20Matrix)|Medium| +|538|Convert BST to Greater Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Convert%20BST%20to%20Greater%20Tree)|Medium| |532|K-diff Pairs in an Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/K-diff%20Pairs%20in%20an%20Array)|Easy| |530|Minimum Absolute Difference in BST|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Minimum%20Absolute%20Difference%20in%20BST)|Easy| |529|Minesweeper|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Minesweeper)|Medium| From 0a9d4e3fce8da0437c0e0b718fd218198d3c8a83 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 22:56:22 +0800 Subject: [PATCH 110/126] Update README.md --- .../README.md | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/Algorithms/Convert Sorted Array to Binary Search Tree/README.md b/Algorithms/Convert Sorted Array to Binary Search Tree/README.md index 1d51937..a2c02ff 100644 --- a/Algorithms/Convert Sorted Array to Binary Search Tree/README.md +++ b/Algorithms/Convert Sorted Array to Binary Search Tree/README.md @@ -5,4 +5,47 @@ ```js +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {number[]} nums + * @return {TreeNode} + */ + +var sortedArrayToBST = function(nums) { + if (nums.length === 0) {return null;} + var start = 0; + var end = nums.length - 1; + var middle = parseInt((start + end) / 2); + + var root, node1, node2, node3; + + if (nums.length <= 3) { + node1 = new TreeNode(nums[0]); + if (nums.length === 1) { + return node1; + } + + node2 = new TreeNode(nums[1]); + node2.left = node1; + if (nums.length === 2) { + return node2; + } + + node3 = new TreeNode(nums[2]); + node2.right = node3; + return node2; + + } else { + root = new TreeNode(nums[middle]); + root.left = sortedArrayToBST(nums.slice(0, middle)); + root.right = sortedArrayToBST(nums.slice(middle + 1)); + } + return root; +}; ``` From 48d4814e90726f128aae19f8aabf828174dfc3ad Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 22:57:01 +0800 Subject: [PATCH 111/126] Update README.md --- Algorithms/Count and Say/README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Algorithms/Count and Say/README.md b/Algorithms/Count and Say/README.md index 86092ea..2feb67e 100644 --- a/Algorithms/Count and Say/README.md +++ b/Algorithms/Count and Say/README.md @@ -5,4 +5,29 @@ ```js +/** + * @param {number} n + * @return {string} + */ + +var countAndSay = function(n) { + if (n === 1) return '1'; + + var last = countAndSay(n - 1); + var digit = last[0]; + var count = 0; + var ret = ''; + + for (var i = 0; i < last.length; i++) { + if (last[i] !== digit) { + ret += count + digit; + digit = last[i]; + count = 0; + } + count++; + } + ret += count + digit; + + return ret; +}; ``` From 6a1ed120220f566f5df2f187ce5559d1b6bf1ef0 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 22:57:34 +0800 Subject: [PATCH 112/126] Update README.md --- Algorithms/Divide Two Integers/README.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/Algorithms/Divide Two Integers/README.md b/Algorithms/Divide Two Integers/README.md index 936e4a4..5a68b07 100644 --- a/Algorithms/Divide Two Integers/README.md +++ b/Algorithms/Divide Two Integers/README.md @@ -4,5 +4,26 @@ ## JavaScript -```js +```java +class Solution { +public: + int divide(int dividend, int divisor) { + if (!divisor || (dividend == INT_MIN && divisor == -1)) + return INT_MAX; + int sign = ((dividend < 0) ^ (divisor < 0)) ? -1 : 1; + long long dvd = labs(dividend); + long long dvs = labs(divisor); + int res = 0; + while (dvd >= dvs) { + long long temp = dvs, multiple = 1; + while (dvd >= (temp << 1)) { + temp <<= 1; + multiple <<= 1; + } + dvd -= temp; + res += multiple; + } + return sign == 1 ? res : -res; + } +}; ``` From 70448a17ead33d0da52e9ff12c23339af5872c9f Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 22:58:00 +0800 Subject: [PATCH 113/126] Update README.md --- Algorithms/Length of Last Word/README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Algorithms/Length of Last Word/README.md b/Algorithms/Length of Last Word/README.md index 6825964..3f99375 100644 --- a/Algorithms/Length of Last Word/README.md +++ b/Algorithms/Length of Last Word/README.md @@ -5,4 +5,11 @@ ```js +/** + * @param {string} s + * @return {number} + */ +var lengthOfLastWord = function(s) { + return s.trim().split(' ').pop().length; +}; ``` From 13281283f8f9d73f2b971525f2e69817b204e84a Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 22:58:28 +0800 Subject: [PATCH 114/126] Update README.md --- Algorithms/Longest Common Prefix/README.md | 30 ++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/Algorithms/Longest Common Prefix/README.md b/Algorithms/Longest Common Prefix/README.md index c7f94eb..5816c4b 100644 --- a/Algorithms/Longest Common Prefix/README.md +++ b/Algorithms/Longest Common Prefix/README.md @@ -5,4 +5,34 @@ ```js +/** + * @param {string[]} strs + * @return {string} + */ +var longestCommonPrefix = function(strs) { + if (strs.length === 0) return ''; + if (strs.length === 0) return strs[1]; + + var ret = ''; + var isBreak = false; + var isCommon = true; + var char; + + for (var j = 0; j < strs[0].length; j++) { + char = strs[0][j]; + + for (var i = 1; i < strs.length; i++) { + if (strs[i].length <= j || strs[i][j] !== char) { + isBreak = true; + break; + } + } + if (isBreak) { + break; + } + ret += strs[0][j]; + } + return ret; +}; + ``` From 022fd60ae6051d18e3f5a4b9dc71cd0c713c7da7 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 22:58:51 +0800 Subject: [PATCH 115/126] Update README.md --- Algorithms/Maximum Subarray/README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Algorithms/Maximum Subarray/README.md b/Algorithms/Maximum Subarray/README.md index 259078f..dd4c476 100644 --- a/Algorithms/Maximum Subarray/README.md +++ b/Algorithms/Maximum Subarray/README.md @@ -5,4 +5,21 @@ ```js +/** + * @param {number[]} nums + * @return {number} + */ + // https://discuss.leetcode.com/topic/6413/dp-solution-some-thoughts +var maxSubArray = function(nums) { + var max = nums[0]; + var dp = {}; + dp[0] = nums[0]; + + for (var i = 1; i < nums.length; i++) { + dp[i] = dp[i - 1] > 0 ? dp[i - 1] + nums[i] : nums[i]; + max = Math.max(max, dp[i]); + } + + return max; +}; ``` From 583d1890b63d5988ba614df0c101083fe8cea8e2 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 22:59:16 +0800 Subject: [PATCH 116/126] Update README.md --- Algorithms/Next Permutation/README.md | 33 +++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/Algorithms/Next Permutation/README.md b/Algorithms/Next Permutation/README.md index 80e0b27..ba0b25d 100644 --- a/Algorithms/Next Permutation/README.md +++ b/Algorithms/Next Permutation/README.md @@ -5,4 +5,37 @@ ```js +var nextPermutation = function(nums) { + if (nums.length <= 1) { + return; + } + + var i = nums.length - 2; + var j = nums.length - 1; + while (nums[i] >= nums[i + 1] && i > -1) { + i--; + } + if (i >= 0) { + while (nums[j] <= nums[i]) { + j--; + } + + var temp; + temp = nums[i]; + nums[i] = nums[j]; + nums[j] = temp; + } + + var start = i + 1; + var end = nums.length - 1; + + while (start < end) { + temp = nums[start]; + nums[start] = nums[end]; + nums[end] = temp; + start++; + end--; + } + +}; ``` From b8e3a2d04f3b9e1b4473e19ec4531d6330d277f5 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 22:59:54 +0800 Subject: [PATCH 117/126] Update README.md --- Algorithms/Search Insert Position/README.md | 29 +++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/Algorithms/Search Insert Position/README.md b/Algorithms/Search Insert Position/README.md index 84c9dc7..be12e74 100644 --- a/Algorithms/Search Insert Position/README.md +++ b/Algorithms/Search Insert Position/README.md @@ -5,4 +5,33 @@ ```js +/** + * @param {number[]} nums + * @param {number} target + * @return {number} + */ +var searchInsert = function(nums, target) { + var start = 0; + var end = nums.length - 1; + var middle = parseInt((start + end) / 2); + + while(start <= end) { + if (target === nums[middle]) { + return middle; + } else if (target > nums[middle]) { + start = middle + 1; + } else if (target < nums[middle] ) { + end = middle - 1; + } + if (start > end) { + if (target < nums[middle]) { + return middle; + } else { + return middle + 1; + } + } else { + middle = parseInt((start + end) / 2); + } + } +}; ``` From e8f90f2c54c734bd871c69c52c5a835e220b55aa Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 23:00:51 +0800 Subject: [PATCH 118/126] Update README.md --- .../Search in Rotated Sorted Array/README.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/Algorithms/Search in Rotated Sorted Array/README.md b/Algorithms/Search in Rotated Sorted Array/README.md index d426cb9..ad7af47 100644 --- a/Algorithms/Search in Rotated Sorted Array/README.md +++ b/Algorithms/Search in Rotated Sorted Array/README.md @@ -5,4 +5,43 @@ ```js +/** + * @param {number[]} nums + * @param {number} target + * @return {number} + */ +function binarySearch(nums, target) { + var end = nums.length - 1; + var start = 0; + var middle = parseInt((start + end) / 2); + + while (end >= start) { + if (nums[middle] == target) { + return middle; + } else if(nums[middle] > target) { + end = middle - 1; + } else if(nums[middle] < target) { + start = middle + 1; + } + middle = parseInt((start + end) / 2); + } + return -1; +} + +var search = function(nums, target) { + var i = 0; + while(nums[i] <= nums[i+1]) { + i++; + } + + if (nums[0] <= target && target <= nums[i]) { + return binarySearch(nums.slice(0, i + 1), target); + } else if (nums[i + 1] <= target && target <= nums[nums.length - 1]){ + var ret = binarySearch(nums.slice(i + 1), target); + return ret == -1 ? -1 : i + 1 + ret; + } else { + return -1; + } +}; + ``` From 1d80186f3fa3d9b70bf75b7d33c5c715c07395ef Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 23:01:16 +0800 Subject: [PATCH 119/126] Update README.md --- Algorithms/Spiral Matrix/README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Algorithms/Spiral Matrix/README.md b/Algorithms/Spiral Matrix/README.md index 73c0dfc..73ca55d 100644 --- a/Algorithms/Spiral Matrix/README.md +++ b/Algorithms/Spiral Matrix/README.md @@ -5,4 +5,31 @@ ```js +/** + * @param {number[][]} matrix + * @return {number[]} + */ +var spiralOrder = function(matrix) { + if (matrix.length == 0) {return [];} + var n = matrix.length; + var m = matrix[0].length; + var up = 0, right = m - 1, down = n - 1, left = 0; + var ret = []; + + while(true) { + for(var r = left; r <= right; r++) ret.push(matrix[up][r]); + if (++up > down) break; + + for(var d = up; d <= down; d++) ret.push(matrix[d][right]); + if (--right < left) break; + + for(var l = right; l >= left; l--) ret.push(matrix[down][l]); + if (--down < up) break; + + for(var u = down; u >= up; u--) ret.push(matrix[u][left]); + if (++left > right) break; + } + return ret; +}; + ``` From d307b3a3e1caf3e2635a2ed3cf528a156664dbc5 Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 23:01:34 +0800 Subject: [PATCH 120/126] Update README.md --- Algorithms/Sqrt(x)/README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Algorithms/Sqrt(x)/README.md b/Algorithms/Sqrt(x)/README.md index 0898ff8..277ec32 100644 --- a/Algorithms/Sqrt(x)/README.md +++ b/Algorithms/Sqrt(x)/README.md @@ -5,4 +5,27 @@ ```js +/** + * @param {number} x + * @return {number} + */ +var mySqrt = function(x) { + var start = 1; + var end = x; + var middle; + + while (start <= end) { + middle = start + parseInt((end - start) / 2); + if (x / middle === middle) { + return middle; + } else if (x / middle > middle) { + start = middle + 1; + } else { + end = middle - 1; + } + } + + return end; +}; + ``` From 4648b52b34685dbc788ccb80f4ee3d00128e59bd Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 21 Mar 2017 23:02:02 +0800 Subject: [PATCH 121/126] Update README.md --- .../Validate Binary Search Tree/README.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/Algorithms/Validate Binary Search Tree/README.md b/Algorithms/Validate Binary Search Tree/README.md index 43c1f3e..d73ea67 100644 --- a/Algorithms/Validate Binary Search Tree/README.md +++ b/Algorithms/Validate Binary Search Tree/README.md @@ -5,4 +5,38 @@ ```js +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {boolean} + */ + +var traverse = function(root, cb) { + if (root === null) { + return; + } + traverse(root.left, cb); + cb(root); + traverse(root.right, cb); + +}; +var isValidBST = function(root) { + var arr = []; + traverse(root, function(node) { + arr.push(node.val); + }); + + for(var i = 0; i < arr.length - 1; i++) { + if (arr[i] >= arr[i + 1]) { + return false; + } + } + return true; +}; ``` From 9fbc0ba49c8401681861fb3bef7f892d398c5fa8 Mon Sep 17 00:00:00 2001 From: Teng DU Date: Wed, 22 Mar 2017 00:35:17 +0800 Subject: [PATCH 122/126] new --- Algorithms/Linked List Random Node/README.md | 48 ++++++++++++++++++++ Algorithms/Teemo Attacking/README.md | 28 ++++++++++++ request-problem.js | 15 +----- 3 files changed, 78 insertions(+), 13 deletions(-) create mode 100644 Algorithms/Linked List Random Node/README.md create mode 100644 Algorithms/Teemo Attacking/README.md diff --git a/Algorithms/Linked List Random Node/README.md b/Algorithms/Linked List Random Node/README.md new file mode 100644 index 0000000..2258879 --- /dev/null +++ b/Algorithms/Linked List Random Node/README.md @@ -0,0 +1,48 @@ +# [Linked List Random Node](https://leetcode.com/problems/linked-list-random-node/#/description) +###### No:`382` +###### Difficulty:`Medium` + + +```js +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param head The linked list's head. + Note that the head is guaranteed to be not null, so it contains at least one node. + * @param {ListNode} head + */ +var Solution = function(head) { + this.head = head; +}; + +/** + * Returns a random node's value. + * @return {number} + */ +Solution.prototype.getRandom = function() { + var ret = this.head.val; + var p = this.head.next; + var i = 2; + while (p !== null) { + var random = Math.ceil(Math.random() * i); + if (random === 1) { + ret = p.val; + } + i++; + p = p.next; + } + + return ret; +}; + +/** + * Your Solution object will be instantiated and called as such: + * var obj = Object.create(Solution).createNew(head) + * var param_1 = obj.getRandom() + */ +``` diff --git a/Algorithms/Teemo Attacking/README.md b/Algorithms/Teemo Attacking/README.md new file mode 100644 index 0000000..1f0b01a --- /dev/null +++ b/Algorithms/Teemo Attacking/README.md @@ -0,0 +1,28 @@ +# [Teemo Attacking](https://leetcode.com/problems/teemo-attacking/#/description) +###### No:`495` +###### Difficulty:`Medium` +## JavaScript + + +```js +/** + * @param {number[]} timeSeries + * @param {number} duration + * @return {number} + */ +var findPoisonedDuration = function(timeSeries, duration) { + if (timeSeries.length === 0) return 0; + + var time = 0; + for (var i = 0; i < timeSeries.length - 1; i++) { + if (timeSeries[i + 1] - timeSeries[i] < duration) { + time += timeSeries[i + 1] - timeSeries[i]; + } else { + time += duration; + } + } + time += duration; + + return time; +}; +``` diff --git a/request-problem.js b/request-problem.js index 4e90edb..e5f405b 100644 --- a/request-problem.js +++ b/request-problem.js @@ -3,22 +3,12 @@ var request = require('request'); var cheerio = require('cheerio'); var urls = [ - 'https://leetcode.com/problems/longest-common-prefix/#/description', - 'https://leetcode.com/problems/divide-two-integers/#/description', - 'https://leetcode.com/problems/next-permutation/#/description', - 'https://leetcode.com/problems/search-in-rotated-sorted-array/#/description', - 'https://leetcode.com/problems/search-insert-position/#/description', - 'https://leetcode.com/problems/count-and-say/#/description', - 'https://leetcode.com/problems/maximum-subarray/#/description', - 'https://leetcode.com/problems/spiral-matrix/#/description', - 'https://leetcode.com/problems/length-of-last-word/#/description', - 'https://leetcode.com/problems/sqrtx/#/description', - 'https://leetcode.com/problems/validate-binary-search-tree/', - 'https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/#/description', + 'https://leetcode.com/problems/linked-list-random-node/#/description', ]; urls.forEach((url) => { request(url, (err, response, body) => { + console.log(url, err); var $ = cheerio.load(body); var textArr = $('.question-title h3').text().trim().split('. '); var no = textArr[0]; @@ -28,7 +18,6 @@ urls.forEach((url) => { var folderPath = `Algorithms/${title}`; var filePath = `${folderPath}/README.md`; - console.log(no, title, difficulty); fs.mkdir(folderPath, (err) => { fs.open(filePath, 'wx', (err) => { From 39420cb981e36eac9933945e2ad449d03027b29d Mon Sep 17 00:00:00 2001 From: Teng Du Date: Mon, 3 Apr 2017 22:30:52 +0800 Subject: [PATCH 123/126] Update README.md --- Algorithms/Number of 1 Bits/README.md | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/Algorithms/Number of 1 Bits/README.md b/Algorithms/Number of 1 Bits/README.md index 42312ae..dc6cfc9 100644 --- a/Algorithms/Number of 1 Bits/README.md +++ b/Algorithms/Number of 1 Bits/README.md @@ -1,8 +1,23 @@ # [Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/) ###### No:`191` ###### Difficulty:`Easy` - - +#### Solution 1 +```js +/** + * @param {number} n - a positive integer + * @return {number} + */ +var hammingWeight = function(n) { + var count = 0; + while (n) { + n = n & (n - 1); + count++; + } + + return count; +}; +``` +#### Solution 2 ```javascript /** * @param {number} n - a positive integer From 5532167a5dea12c14fbf05a3ea6bc3d572e3c45f Mon Sep 17 00:00:00 2001 From: Teng Du Date: Mon, 3 Apr 2017 22:57:43 +0800 Subject: [PATCH 124/126] Update README.md --- Algorithms/Power of Four/README.md | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/Algorithms/Power of Four/README.md b/Algorithms/Power of Four/README.md index 4b84e65..dc9c210 100644 --- a/Algorithms/Power of Four/README.md +++ b/Algorithms/Power of Four/README.md @@ -5,16 +5,19 @@ ```js -/** - * @param {number} num - * @return {boolean} - */ -/* -if (num & (num - 1)) === 0) { - // num only contains only one '1' bit -} -*/ - +var isPowerOfFour = function(num) { + if (num < 1) { + return false; + } else if (num === 1) { + return true; + } else if (num % 4 !== 0) { + return false; + } else { + return isPowerOfFour(num / 4); + } +}; +``` +```js var isPowerOfFour = function(num) { return num > 0 && (num & (num - 1)) === 0 && (num & 0x55555555) !== 0; }; From 9e094b8b7571781707e4b8ed2c3eb99a6d86990e Mon Sep 17 00:00:00 2001 From: Teng Du Date: Tue, 4 Apr 2017 22:15:54 +0800 Subject: [PATCH 125/126] Update README.md --- Algorithms/Missing Number/README.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Algorithms/Missing Number/README.md b/Algorithms/Missing Number/README.md index 218ba80..a314aa1 100644 --- a/Algorithms/Missing Number/README.md +++ b/Algorithms/Missing Number/README.md @@ -2,7 +2,7 @@ ###### No:`268` ###### Difficulty:`Medium` - +#### Solution 1 ```javascript /** * @param {number[]} nums @@ -12,3 +12,17 @@ var missingNumber = function(nums) { return (0 + nums.length) * (nums.length + 1) / 2 - nums.reduce((p, c) => p + c, 0); }; ``` + +#### Solution 2 +Any number XOR itself will be 0. +e.g.[0,1,3] +ret = (0 ^ 1 ^ 3) ^ (0 ^ 1 ^ 2 ^ 3) // will get 2 +```js +/** + * @param {number[]} nums + * @return {number} + */ +var missingNumber = function(nums) { + return nums.reduce((p, c, i) => p ^ c ^ i, 0) ^ nums.length; +}; +``` From e840506729f8adb105e217fa32394ee5c4a74c28 Mon Sep 17 00:00:00 2001 From: Teng DU Date: Thu, 22 Nov 2018 01:49:36 +0800 Subject: [PATCH 126/126] Single md file --- README.md | 8031 ++++++++++++++++- .../Algorithms}/01 Matrix/README.md | 0 .../Algorithms}/3Sum Closest/README.md | 0 .../Algorithms}/3Sum/README.md | 0 .../Algorithms}/Add Digits/README.md | 0 .../Algorithms}/Add Strings/README.md | 0 .../Algorithms}/Arithmetic Slices/README.md | 0 .../Algorithms}/Arranging Coins/README.md | 0 .../Algorithms}/Assign Cookies/README.md | 0 .../Balanced Binary Tree/README.md | 0 .../Algorithms}/Base 7/README.md | 0 .../Beautiful Arrangement/README.md | 0 .../README.md | 0 .../Best Time to Buy and Sell Stock/README.md | 0 .../README.md | 0 .../README.md | 0 .../Algorithms}/Binary Tree Paths/README.md | 0 .../Binary Tree Preorder Traversal/README.md | 0 .../Algorithms}/Binary Watch/README.md | 0 .../Algorithms}/Bulb Switcher/README.md | 0 .../Algorithms}/Climbing Stairs/README.md | 0 .../Compare Version Numbers/README.md | 0 .../Construct the Rectangle/README.md | 0 .../Contains Duplicate II/README.md | 0 .../Algorithms}/Contains Duplicate/README.md | 0 .../Convert BST to Greater Tree/README.md | 0 .../README.md | 0 .../Copy List with Random Pointer/README.md | 0 .../Algorithms}/Count Primes/README.md | 0 .../Algorithms}/Count and Say/README.md | 0 .../Algorithms}/Counting Bits/README.md | 0 .../Delete Node in a BST/README.md | 0 .../Delete Node in a Linked List/README.md | 0 .../Algorithms}/Detect Capital/README.md | 0 .../Diameter of Binary Tree/README.md | 0 .../Algorithms}/Divide Two Integers/README.md | 0 .../README.md | 0 .../Excel Sheet Column Number/README.md | 0 .../Excel Sheet Column Title/README.md | 0 .../Factorial Trailing Zeroes/README.md | 0 .../Find All Duplicates in an Array/README.md | 0 .../README.md | 0 .../Find Bottom Left Tree Value/README.md | 0 .../README.md | 0 .../Find Mode in Binary Search Tree/README.md | 0 .../Algorithms}/Find the Difference/README.md | 0 .../Algorithms}/First Bad Version/README.md | 0 .../README.md | 0 .../Algorithms}/Fizz Buzz/README.md | 0 .../Generate Parentheses/README.md | 0 .../Guess Number Higher or Lower/README.md | 0 .../Algorithms}/Hamming Distance/README.md | 0 .../Algorithms}/Happy Number/README.md | 0 .../Algorithms}/House Robber/README.md | 0 .../Implement Queue using Stacks/README.md | 0 .../Algorithms}/Implement strStr()/README.md | 0 .../Algorithms}/Integer Break/README.md | 0 .../Algorithms}/Integer to Roman/README.md | 0 .../Intersection of Two Arrays II/README.md | 0 .../Intersection of Two Arrays/README.md | 0 .../README.md | 0 .../Algorithms}/Invert Binary Tree/README.md | 0 .../Algorithms}/Island Perimeter/README.md | 0 .../Algorithms}/Isomorphic Strings/README.md | 0 .../K-diff Pairs in an Array/README.md | 0 .../Algorithms}/Keyboard Row/README.md | 0 .../Algorithms}/Largest Number/README.md | 0 .../Algorithms}/Length of Last Word/README.md | 0 .../README.md | 0 .../License Key Formatting/README.md | 0 .../Algorithms}/Linked List Cycle/README.md | 0 .../Linked List Random Node/README.md | 0 .../Longest Common Prefix/README.md | 0 .../Algorithms}/Longest Palindrome/README.md | 0 .../README.md | 0 .../Algorithms}/Majority Element/README.md | 0 .../Max Consecutive Ones/README.md | 0 .../Max Points on a Line/README.md | 0 .../Maximum Depth of Binary Tree/README.md | 0 .../Algorithms}/Maximum Subarray/README.md | 0 .../Algorithms}/Merge Sorted Array/README.md | 0 .../Merge Two Sorted Lists/README.md | 0 .../Algorithms}/Min Stack/README.md | 0 .../Algorithms}/Minesweeper/README.md | 0 .../README.md | 0 .../Minimum Depth of Binary Tree/README.md | 0 .../README.md | 0 .../Algorithms}/Missing Number/README.md | 0 .../Most Frequent Subtree Sum/README.md | 0 .../Algorithms}/Move Zeroes/README.md | 0 .../Next Greater Element I/README.md | 0 .../Next Greater Element II/README.md | 0 .../Algorithms}/Next Permutation/README.md | 0 .../Algorithms}/Nim Game/README.md | 0 .../Algorithms}/Nth Digit/README.md | 0 .../Algorithms}/Number Complement/README.md | 0 .../Algorithms}/Number of 1 Bits/README.md | 0 .../Number of Boomerangs/README.md | 0 .../Number of Segments in a String/README.md | 0 .../Odd Even Linked List/README.md | 0 .../Output Contest Matches/README.md | 0 .../Palindrome Linked List/README.md | 0 .../Algorithms}/Palindrome Number/README.md | 0 .../Pascal's Triangle II/README.md | 0 .../Algorithms}/Pascal's Triangle/README.md | 0 .../Algorithms}/Path Sum III/README.md | 0 .../Algorithms}/Path Sum/README.md | 0 .../Algorithms}/Permutations/README.md | 0 .../Algorithms}/Plus One/README.md | 0 .../Algorithms}/Power of Four/README.md | 0 .../Algorithms}/Power of Three/README.md | 0 .../Algorithms}/Power of Two/README.md | 0 .../Product of Array Except Self/README.md | 0 .../Queue Reconstruction by Height/README.md | 0 .../Algorithms}/Ransom Note/README.md | 0 .../Algorithms}/Rectangle Area/README.md | 0 .../Algorithms}/Relative Ranks/README.md | 0 .../README.md | 0 .../README.md | 0 .../Algorithms}/Remove Element/README.md | 0 .../Remove Linked List Elements/README.md | 0 .../README.md | 0 .../Repeated Substring Pattern/README.md | 0 .../Algorithms}/Reverse Integer/README.md | 0 .../Reverse Linked List II/README.md | 0 .../Algorithms}/Reverse Linked List/README.md | 0 .../Algorithms}/Reverse String/README.md | 0 .../Reverse Vowels of a String/README.md | 0 .../Reverse Words in a String/README.md | 0 .../Algorithms}/Roman to Integer/README.md | 0 .../Algorithms}/Rotate Array/README.md | 0 .../Algorithms}/Same Tree/README.md | 0 .../Search Insert Position/README.md | 0 .../Search in Rotated Sorted Array/README.md | 0 .../Algorithms}/Set Matrix Zeroes/README.md | 0 .../Algorithms}/Single Number/README.md | 0 .../Algorithms}/Sort List/README.md | 0 .../Algorithms}/Spiral Matrix/README.md | 0 .../Algorithms}/Sqrt(x)/README.md | 0 .../String to Integer (atoi)/README.md | 0 .../Algorithms}/Sum of Left Leaves/README.md | 0 .../Algorithms}/Summary Ranges/README.md | 0 .../Algorithms}/Swap Nodes in Pairs/README.md | 0 .../Algorithms}/Symmetric Tree/README.md | 0 .../Algorithms}/Teemo Attacking/README.md | 0 .../Third Maximum Number/README.md | 0 .../Top K Frequent Elements/README.md | 0 .../Algorithms}/Two Sum/README.md | 0 .../Algorithms}/Ugly Number/README.md | 0 .../Algorithms}/Valid Anagram/README.md | 0 .../Algorithms}/Valid Palindrome/README.md | 0 .../Algorithms}/Valid Parentheses/README.md | 0 .../Valid Perfect Square/README.md | 0 .../Algorithms}/Valid Sudoku/README.md | 0 .../Validate Binary Search Tree/README.md | 0 .../Algorithms}/ZigZag Conversion/README.md | 0 old(deprecated)/README.md | 161 + .../generate-md.js | 0 package.json => old(deprecated)/package.json | 0 .../request-problem.js | 0 160 files changed, 8031 insertions(+), 161 deletions(-) rename {Algorithms => old(deprecated)/Algorithms}/01 Matrix/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/3Sum Closest/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/3Sum/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Add Digits/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Add Strings/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Arithmetic Slices/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Arranging Coins/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Assign Cookies/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Balanced Binary Tree/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Base 7/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Beautiful Arrangement/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Best Time to Buy and Sell Stock II/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Best Time to Buy and Sell Stock/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Binary Tree Level Order Traversal II/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Binary Tree Level Order Traversal/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Binary Tree Paths/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Binary Tree Preorder Traversal/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Binary Watch/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Bulb Switcher/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Climbing Stairs/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Compare Version Numbers/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Construct the Rectangle/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Contains Duplicate II/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Contains Duplicate/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Convert BST to Greater Tree/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Convert Sorted Array to Binary Search Tree/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Copy List with Random Pointer/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Count Primes/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Count and Say/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Counting Bits/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Delete Node in a BST/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Delete Node in a Linked List/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Detect Capital/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Diameter of Binary Tree/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Divide Two Integers/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Evaluate Reverse Polish Notation/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Excel Sheet Column Number/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Excel Sheet Column Title/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Factorial Trailing Zeroes/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Find All Duplicates in an Array/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Find All Numbers Disappeared in an Array/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Find Bottom Left Tree Value/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Find Largest Value in Each Tree Row/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Find Mode in Binary Search Tree/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Find the Difference/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/First Bad Version/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/First Unique Character in a String/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Fizz Buzz/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Generate Parentheses/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Guess Number Higher or Lower/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Hamming Distance/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Happy Number/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/House Robber/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Implement Queue using Stacks/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Implement strStr()/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Integer Break/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Integer to Roman/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Intersection of Two Arrays II/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Intersection of Two Arrays/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Intersection of Two Linked Lists/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Invert Binary Tree/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Island Perimeter/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Isomorphic Strings/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/K-diff Pairs in an Array/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Keyboard Row/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Largest Number/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Length of Last Word/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Letter Combinations of a Phone Number/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/License Key Formatting/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Linked List Cycle/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Linked List Random Node/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Longest Common Prefix/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Longest Palindrome/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Lowest Common Ancestor of a Binary Search Tree/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Majority Element/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Max Consecutive Ones/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Max Points on a Line/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Maximum Depth of Binary Tree/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Maximum Subarray/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Merge Sorted Array/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Merge Two Sorted Lists/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Min Stack/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Minesweeper/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Minimum Absolute Difference in BST/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Minimum Depth of Binary Tree/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Minimum Moves to Equal Array Elements/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Missing Number/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Most Frequent Subtree Sum/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Move Zeroes/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Next Greater Element I/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Next Greater Element II/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Next Permutation/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Nim Game/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Nth Digit/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Number Complement/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Number of 1 Bits/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Number of Boomerangs/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Number of Segments in a String/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Odd Even Linked List/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Output Contest Matches/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Palindrome Linked List/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Palindrome Number/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Pascal's Triangle II/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Pascal's Triangle/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Path Sum III/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Path Sum/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Permutations/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Plus One/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Power of Four/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Power of Three/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Power of Two/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Product of Array Except Self/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Queue Reconstruction by Height/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Ransom Note/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Rectangle Area/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Relative Ranks/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Remove Duplicates from Sorted Array/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Remove Duplicates from Sorted List/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Remove Element/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Remove Linked List Elements/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Remove Nth Node From End of List/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Repeated Substring Pattern/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Reverse Integer/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Reverse Linked List II/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Reverse Linked List/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Reverse String/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Reverse Vowels of a String/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Reverse Words in a String/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Roman to Integer/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Rotate Array/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Same Tree/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Search Insert Position/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Search in Rotated Sorted Array/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Set Matrix Zeroes/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Single Number/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Sort List/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Spiral Matrix/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Sqrt(x)/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/String to Integer (atoi)/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Sum of Left Leaves/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Summary Ranges/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Swap Nodes in Pairs/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Symmetric Tree/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Teemo Attacking/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Third Maximum Number/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Top K Frequent Elements/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Two Sum/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Ugly Number/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Valid Anagram/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Valid Palindrome/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Valid Parentheses/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Valid Perfect Square/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Valid Sudoku/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/Validate Binary Search Tree/README.md (100%) rename {Algorithms => old(deprecated)/Algorithms}/ZigZag Conversion/README.md (100%) create mode 100644 old(deprecated)/README.md rename generate-md.js => old(deprecated)/generate-md.js (100%) rename package.json => old(deprecated)/package.json (100%) rename request-problem.js => old(deprecated)/request-problem.js (100%) diff --git a/README.md b/README.md index 64c367a..f3e94c0 100644 --- a/README.md +++ b/README.md @@ -1,161 +1,7870 @@ -leetcode -======== -#### Total solved: 153 (Easy: 111 Medium: 41 Hard: 1 ) -My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) - - -| No | Title | Source Code | Difficulty | -|----| ----- | -------- | ---------- | -|544|Output Contest Matches|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Output%20Contest%20Matches)|Medium| -|543|Diameter of Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Diameter%20of%20Binary%20Tree)|Easy| -|542|01 Matrix|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/01%20Matrix)|Medium| -|538|Convert BST to Greater Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Convert%20BST%20to%20Greater%20Tree)|Medium| -|532|K-diff Pairs in an Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/K-diff%20Pairs%20in%20an%20Array)|Easy| -|530|Minimum Absolute Difference in BST|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Minimum%20Absolute%20Difference%20in%20BST)|Easy| -|529|Minesweeper|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Minesweeper)|Medium| -|526|Beautiful Arrangement|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Beautiful%20Arrangement)|Medium| -|520|Detect Capital|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Detect%20Capital)|Easy| -|515|Find Largest Value in Each Tree Row|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Find%20Largest%20Value%20in%20Each%20Tree%20Row)|Medium| -|513|Find Bottom Left Tree Value|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Find%20Bottom%20Left%20Tree%20Value)|Medium| -|508|Most Frequent Subtree Sum|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Most%20Frequent%20Subtree%20Sum)|Medium| -|506|Relative Ranks|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Relative%20Ranks)|Easy| -|504|Base 7|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Base%207)|Easy| -|503|Next Greater Element II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Next%20Greater%20Element%20II)|Medium| -|501|Find Mode in Binary Search Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Find%20Mode%20in%20Binary%20Search%20Tree)|Easy| -|500|Keyboard Row|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Keyboard%20Row)|Easy| -|496|Next Greater Element I|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Next%20Greater%20Element%20I)|Easy| -|492|Construct the Rectangle|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Construct%20the%20Rectangle)|Easy| -|485|Max Consecutive Ones|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Max%20Consecutive%20Ones)|Easy| -|482|License Key Formatting|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/License%20Key%20Formatting)|Medium| -|476|Number Complement|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Number%20Complement)|Easy| -|463|Add Digits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Add%20Digits)|Easy| -|461|Hamming Distance|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Hamming%20Distance)|Easy| -|459|Repeated Substring Pattern|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Repeated%20Substring%20Pattern)|Easy| -|455|Assign Cookies|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Assign%20Cookies)|Easy| -|453|Minimum Moves to Equal Array Elements|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Minimum%20Moves%20to%20Equal%20Array%20Elements)|Easy| -|450|Delete Node in a BST|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Delete%20Node%20in%20a%20BST)|Medium| -|448|Find All Numbers Disappeared in an Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Find%20All%20Numbers%20Disappeared%20in%20an%20Array)|Easy| -|447|Number of Boomerangs|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Number%20of%20Boomerangs)|Easy| -|442|Find All Duplicates in an Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Find%20All%20Duplicates%20in%20an%20Array)|Medium| -|441|Arranging Coins|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Arranging%20Coins)|Easy| -|437|Path Sum III|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Path%20Sum%20III)|Easy| -|434|Number of Segments in a String|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Number%20of%20Segments%20in%20a%20String)|Easy| -|415|Add Strings|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Add%20Strings)|Easy| -|414|Third Maximum Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Third%20Maximum%20Number)|Easy| -|413|Arithmetic Slices|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Arithmetic%20Slices)|Medium| -|412|Fizz Buzz|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Fizz%20Buzz)|Easy| -|409|Longest Palindrome|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Longest%20Palindrome)|Easy| -|406|Queue Reconstruction by Height|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Queue%20Reconstruction%20by%20Height)|Medium| -|404|Sum of Left Leaves|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Sum%20of%20Left%20Leaves)|Easy| -|401|Binary Watch|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Binary%20Watch)|Easy| -|400|Nth Digit|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Nth%20Digit)|Easy| -|389|Find the Difference|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Find%20the%20Difference)|Easy| -|387|First Unique Character in a String|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/First%20Unique%20Character%20in%20a%20String)|Easy| -|383|Ransom Note|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Ransom%20Note)|Easy| -|374|Guess Number Higher or Lower|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Guess%20Number%20Higher%20or%20Lower)|Easy| -|367|Valid Perfect Square|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Perfect%20Square)|Easy| -|350|Intersection of Two Arrays II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Intersection%20of%20Two%20Arrays%20II)|Easy| -|349|Intersection of Two Arrays|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Intersection%20of%20Two%20Arrays)|Easy| -|347|Top K Frequent Elements|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Top%20K%20Frequent%20Elements)|Medium| -|345|Reverse Vowels of a String|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Vowels%20of%20a%20String)|Easy| -|344|Reverse String|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20String)|Easy| -|343|Integer Break|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Integer%20Break)|Medium| -|342|Power of Four|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Power%20of%20Four)|Easy| -|338|Counting Bits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Counting%20Bits)|Medium| -|328|Odd Even Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Odd%20Even%20Linked%20List)|Medium| -|326|Power of Three|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Power%20of%20Three)|Easy| -|319|Add Digits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Add%20Digits)|Medium| -|292|Nim Game|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Nim%20Game)|Easy| -|283|Move Zeroes|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Move%20Zeroes)|Easy| -|278|First Bad Version|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/First%20Bad%20Version)|Easy| -|268|Missing Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Missing%20Number)|Medium| -|263|Ugly Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Ugly%20Number)|Easy| -|258|Add Digits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Add%20Digits)|Easy| -|257|Binary Tree Paths|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Binary%20Tree%20Paths)|Easy| -|242|Valid Anagram|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Anagram)|Easy| -|238|Product of Array Except Self|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Product%20of%20Array%20Except%20Self)|Medium| -|237|Delete Node in a Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Delete%20Node%20in%20a%20Linked%20List)|Easy| -|235|Lowest Common Ancestor of a Binary Search Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree)|Easy| -|234|Palindrome Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Palindrome%20Linked%20List)|Easy| -|232|Implement Queue using Stacks|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Implement%20Queue%20using%20Stacks)|Easy| -|231|Power of Two|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Power%20of%20Two)|Easy| -|228|Summary Ranges|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Summary%20Ranges)|Easy| -|226|Invert Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Invert%20Binary%20Tree)|Easy| -|223|Rectangle Area|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Rectangle%20Area)|Easy| -|219|Contains Duplicate II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Contains%20Duplicate%20II)|Easy| -|217|Contains Duplicate|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Contains%20Duplicate)|Easy| -|206|Reverse Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Linked%20List)|Easy| -|205|Isomorphic Strings|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Isomorphic%20Strings)|Easy| -|204|Count Primes|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Count%20Primes)|Easy| -|203|Remove Linked List Elements|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Linked%20List%20Elements)|Easy| -|202|Happy Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Happy%20Number)|Easy| -|198|House Robber|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/House%20Robber)|Easy| -|191|Number of 1 Bits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Number%20of%201%20Bits)|Easy| -|189|Rotate Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Rotate%20Array)|Easy| -|179|Largest Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Largest%20Number)|Medium| -|172|Factorial Trailing Zeroes|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Factorial%20Trailing%20Zeroes)|Easy| -|171|Excel Sheet Column Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Excel%20Sheet%20Column%20Number)|Easy| -|169|Majority Element|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Majority%20Element)|Easy| -|168|Excel Sheet Column Title|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Excel%20Sheet%20Column%20Title)|Easy| -|165|Compare Version Numbers|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Compare%20Version%20Numbers)|Easy| -|160|Intersection of Two Linked Lists|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Intersection%20of%20Two%20Linked%20Lists)|Easy| -|155|Min Stack|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Min%20Stack)|Easy| -|151|Reverse Words in a String|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Words%20in%20a%20String)|Easy| -|150|Evaluate Reverse Polish Notation|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Evaluate%20Reverse%20Polish%20Notation)|Medium| -|149|Max Points on a Line|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Max%20Points%20on%20a%20Line)|Hard| -|148|Sort List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Sort%20List)|Medium| -|144|Binary Tree Preorder Traversal|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Binary%20Tree%20Preorder%20Traversal)|Medium| -|141|Linked List Cycle|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Linked%20List%20Cycle)|Easy| -|138|Copy List with Random Pointer|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Copy%20List%20with%20Random%20Pointer)|Medium| -|136|Single Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Single%20Number)|Medium| -|125|Valid Palindrome|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Palindrome)|Easy| -|122|Best Time to Buy and Sell Stock II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Best%20Time%20to%20Buy%20and%20Sell%20Stock%20II)|Medium| -|121|Best Time to Buy and Sell Stock|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Best%20Time%20to%20Buy%20and%20Sell%20Stock)|Easy| -|119|Pascal's Triangle II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Pascal's%20Triangle%20II)|Easy| -|118|Pascal's Triangle|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Pascal's%20Triangle)|Easy| -|112|Path Sum|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Path%20Sum)|Easy| -|111|Minimum Depth of Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Minimum%20Depth%20of%20Binary%20Tree)|Easy| -|110|Balanced Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Balanced%20Binary%20Tree)|Easy| -|108|Convert Sorted Array to Binary Search Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Convert%20Sorted%20Array%20to%20Binary%20Search%20Tree)|Easy| -|107|Binary Tree Level Order Traversal II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Binary%20Tree%20Level%20Order%20Traversal%20II)|Easy| -|104|Maximum Depth of Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Maximum%20Depth%20of%20Binary%20Tree)|Easy| -|102|Binary Tree Level Order Traversal|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Binary%20Tree%20Level%20Order%20Traversal)|Easy| -|101|Symmetric Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Symmetric%20Tree)|Easy| -|100|Same Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Same%20Tree)|Easy| -|98|Validate Binary Search Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Validate%20Binary%20Search%20Tree)|Medium| -|92|Reverse Linked List II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Linked%20List%20II)|Medium| -|88|Merge Sorted Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Merge%20Sorted%20Array)|Easy| -|83|Remove Duplicates from Sorted List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Duplicates%20from%20Sorted%20List)|Easy| -|73|Set Matrix Zeroes|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Set%20Matrix%20Zeroes)|Medium| -|70|Climbing Stairs|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Climbing%20Stairs)|Easy| -|69|Sqrt(x)|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Sqrt(x))|Easy| -|66|Plus One|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Plus%20One)|Easy| -|58|Length of Last Word|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Length%20of%20Last%20Word)|Easy| -|54|Spiral Matrix|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Spiral%20Matrix)|Medium| -|53|Maximum Subarray|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Maximum%20Subarray)|Easy| -|46|Permutations|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Permutations)|Medium| -|38|Count and Say|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Count%20and%20Say)|Easy| -|36|Valid Sudoku|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Sudoku)|Easy| -|35|Search Insert Position|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Search%20Insert%20Position)|Easy| -|33|Search in Rotated Sorted Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Search%20in%20Rotated%20Sorted%20Array)|Medium| -|31|Next Permutation|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Next%20Permutation)|Medium| -|29|Divide Two Integers|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Divide%20Two%20Integers)|Medium| -|28|Implement strStr()|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Implement%20strStr())|Easy| -|27|Remove Element|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Element)|Easy| -|26|Remove Duplicates from Sorted Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Duplicates%20from%20Sorted%20Array)|Easy| -|24|Swap Nodes in Pairs|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Swap%20Nodes%20in%20Pairs)|Easy| -|22|Generate Parentheses|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Generate%20Parentheses)|Medium| -|21|Merge Two Sorted Lists|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Merge%20Two%20Sorted%20Lists)|Easy| -|20|Valid Parentheses|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Parentheses)|Easy| -|19|Remove Nth Node From End of List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Nth%20Node%20From%20End%20of%20List)|Easy| -|17|Letter Combinations of a Phone Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Letter%20Combinations%20of%20a%20Phone%20Number)|Medium| -|16|3Sum Closest|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/3Sum%20Closest)|Medium| -|15|3Sum|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/3Sum)|Medium| -|14|Longest Common Prefix|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Longest%20Common%20Prefix)|Easy| -|13|Roman to Integer|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Roman%20to%20Integer)|Easy| -|12|Integer to Roman|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Integer%20to%20Roman)|Medium| -|9|Palindrome Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Palindrome%20Number)|Easy| -|8|String to Integer (atoi)|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/String%20to%20Integer%20(atoi))|Easy| -|7|Reverse Integer|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Integer)|Easy| -|6|ZigZag Conversion|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/ZigZag%20Conversion)|Easy| -|1|Two Sum|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Two%20Sum)|Easy| + +### 1. [Two Sum](https://leetcode.com/problems/two-sum/description/) +```javascript +/** + * @param {number[]} nums + * @param {number} target + * @return {number[]} + */ +var twoSum = function(nums, target) { + var ret = []; + for(var i = 0; i < nums.length; i++) { + for(var j = i + 1; j < nums.length; j++) { + if(nums[i] + nums[j] === target) { + ret.push(i); + ret.push(j); + } + } + } + return ret; +}; +``` + + +### 2. [Add Two Numbers](https://leetcode.com/problems/add-two-numbers/description/) +```javascript +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} l1 + * @param {ListNode} l2 + * @return {ListNode} + */ + +var getLengthList = function(head) { + var len = 0; + while (head !== null) { + len++; + head = head.next; + } + return len; +}; + +var addTwoNumbers = function(l1, l2) { + var len1 = getLengthList(l1); + var len2 = getLengthList(l2); + + if (len2 >= len1) { + var tmp; + tmp = l1; l1 = l2; l2 = tmp; + } + + var head = l1; + var val1, val2; + var isOverTen = false; + var prev; + while (l1 !== null) { + val1 = l1.val; + val2 = l2 !== null ? l2.val : 0; + + if (isOverTen) { + val1++; + isOverTen = false; + } + if (val1 + val2 >= 10) { + l1.val = val1 + val2 - 10; + isOverTen = true; + } else { + l1.val = val1 + val2; + } + + prev = l1; + l1 = l1.next; + if (l2 !== null) { + l2 = l2.next; + } + } + + if (isOverTen) { + prev.next = new ListNode(1); + } + + return head; +}; +``` + + +### 6. [ZigZag Conversion](https://leetcode.com/problems/zigzag-conversion/description/) +```javascript +/** + * @param {string} s + * @param {number} numRows + * @return {string} + */ +var convert = function(s, numRows) { + var chars = s.split(''); + var arrs = []; + for(var k = 0; k < numRows; k++) { + arrs.push([]); + } + + var i = 0; + while(i < chars.length) { + for(var x = 0; x < numRows && i < chars.length; x++) { + arrs[x].push(chars[i++]); + } + for(var x = numRows - 2; x > 0 && i < chars.length; x--) { + arrs[x].push(chars[i++]); + } + } + + var ret = ''; + arrs.map(function(item) { + ret = ret.concat(item.join('')); + }); + return ret; +}; +``` + + +### 7. [Reverse Integer](https://leetcode.com/problems/reverse-integer/description/) +```javascript +/** + * @param {number} x + * @return {number} + */ +var reverse = function(x) { + var flag = (x < 0 ? true : false); + x = Math.abs(x); + var reverse = parseInt(new String(x).split('').reverse().join(''), 10); + if(reverse > Math.pow(2, 31)) { + return 0; + } + if(flag) { + return 0 - reverse; + } else { + return reverse; + } + +}; +``` + + +### 8. [String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/description/) +```javascript +/** + * @param {string} str + * @return {number} + */ +var myAtoi = function(str) { + var ret = parseInt(str, 10); + if(Number.isNaN(ret)) { + return 0; + } + if(ret > 2147483647) { + return 2147483647; + } else if(ret < -2147483648) { + return -2147483648; + } else { + return ret; + } +}; +``` + + +### 9. [Palindrome Number](https://leetcode.com/problems/palindrome-number/description/) +```javascript +/** + * @param {number} x + * @return {boolean} + */ +var isPalindrome = function(x) { + x += ''; + var i = 0; + var j = x.length - 1; + while(i < j) { + if(x[i] != x[j]) { + return false + } + i++; + j--; + } + return true; +}; +``` + + +### 12. [Integer to Roman](https://leetcode.com/problems/integer-to-roman/description/) +```javascript +/** + * @param {number} num + * @return {string} + */ +var intToRoman = function(num) { + var M = ["", "M", "MM", "MMM"]; + var C = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"]; + var X = ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"]; + var I = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"]; + + return [ + M[parseInt(num / 1000)], + C[parseInt((num % 1000) / 100)], + X[parseInt((num % 100) / 10)], + I[num % 10] + ].join(''); +}; +``` + + +### 13. [Roman to Integer](https://leetcode.com/problems/roman-to-integer/description/) +```javascript +/** + * @param {string} s + * @return {number} + * + * Symbol Value + * I 1 + * V 5 + * X 10 + * L 50 + * C 100 + * D 500 + * M 1,000 + + */ +var romanToInt = function(s) { + var map = { + 'I':1, + 'V':5, + 'X':10, + 'L':50, + 'C':100, + 'D':500, + 'M':1000 + }; + + var sum = 0; + var right = 'I'; + + for(var i = s.length - 1; i >=0; i--) { + if(i < s.length - 1) { + right = s[i + 1]; + } + if(map[s[i]] < map[right]) { + sum -= map[s[i]]; + } else { + sum += map[s[i]]; + right = s[i]; + } + } + return sum; +}; + +``` + + +### 14. [Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/description/) +```javascript +/** + * @param {string[]} strs + * @return {string} + */ +var longestCommonPrefix = function(strs) { + if (strs.length === 0) return ''; + if (strs.length === 0) return strs[1]; + + var ret = ''; + var isBreak = false; + var isCommon = true; + var char; + + for (var j = 0; j < strs[0].length; j++) { + char = strs[0][j]; + + for (var i = 1; i < strs.length; i++) { + if (strs[i].length <= j || strs[i][j] !== char) { + isBreak = true; + break; + } + } + if (isBreak) { + break; + } + ret += strs[0][j]; + } + return ret; +}; + +``` + + +### 15. [3Sum](https://leetcode.com/problems/3sum/description/) +```javascript +/** + * @param {number[]} nums + * @return {number[][]} + */ + +var threeSum = function(nums) { + var ret = []; + + nums = nums.sort((a, b) => a - b); + for (var i = 0; i + 2 < nums.length; i++) { + if (i > 0 && nums[i] == nums[i - 1]) { + continue; + } + + var j = i + 1, k = nums.length - 1; + var target = -nums[i]; + + while (j < k) { + if (nums[j] + nums[k] == target) { + ret.push([nums[i], nums[j], nums[k]]); + j++; + k--; + while (j < k && nums[j] == nums[j - 1]) j++; + while (j < k && nums[k] == nums[k + 1]) k--; + } else if (nums[j] + nums[k] > target) { + k--; + } else { + j++; + } + } + } + + return ret; +}; + +``` + + +### 16. [3Sum Closest](https://leetcode.com/problems/3sum-closest/description/) +```javascript +/** + * @param {number[]} nums + * @param {number} target + * @return {number} + */ +var threeSumClosest = function(nums, target) { + var closet = Number.MAX_SAFE_INTEGER; + var closetTarget; + + nums = nums.sort((a, b) => a - b); + + for (var i = 0; i + 2 < nums.length; i++) { + if (i > 0 && nums[i] == nums[i - 1]) { + continue; + } + + var j = i + 1, k = nums.length - 1; + + while (j < k) { + if (Math.abs(nums[j] + nums[k] + nums[i] - target) < closet) { + closet = Math.abs(nums[j] + nums[k] + nums[i] - target); + closetTarget = nums[j] + nums[k] + nums[i]; + } + + if (nums[j] + nums[k] + nums[i] === target) { + return target; + } else if (nums[j] + nums[k] + nums[i] > target) { + k--; + } else { + j++; + } + } + } + + return closetTarget; +}; + +``` + + +### 17. [Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/) +```javascript +/** + * @param {string} digits + * @return {string[]} + */ +var letterCombinations = function(digits) { + if(digits.length === 0) { + return []; + } + + var map = { + 2: ['a', 'b', 'c'], + 3: ['d', 'e', 'f'], + 4: ['g', 'h', 'i'], + 5: ['j', 'k', 'l'], + 6: ['m', 'n', 'o'], + 7: ['p', 'q', 'r', 's'], + 8: ['t', 'u', 'v'], + 9: ['w', 'x', 'y', 'z'], + }; + + if(digits.length === 1) { + return map[digits]; + } else { + var front = map[digits.slice(0, 1)]; + var back = letterCombinations(digits.slice(1)); + var ret = []; + + front.forEach(f => { + back.forEach(b => { + ret.push(f + b); + }); + }); + + return ret; + } +}; + + +``` + + +### 19. [Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/) +```javascript +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @param {number} n + * @return {ListNode} + */ +var removeNthFromEnd = function(head, n) { + var front = head; + var end = head; + for(var i = 0; i <= n && front; i++) { + front = front.next; + } + + // delete first node + if(i < n + 1) { + return head.next; + } + + while(front) { + front = front.next; + end = end.next; + } + end.next = end.next.next; + return head; +}; +``` + + +### 20. [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/description/) +```javascript +/** + * @param {string} s + * @return {boolean} + */ +var isValid = function(s) { + var stack = []; + var match = { + ')': '(', + ']': '[', + '}': '{' + }; + var len; + for(var i = 0; i < s.length; i++) { + len = stack.length; + if(len > 0 && stack[len-1] == match[s[i]]) { + stack.pop(); + } else { + stack.push(s[i]); + } + } + return stack.length === 0; +}; + +``` + + +### 21. [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/description/) +```javascript +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} l1 + * @param {ListNode} l2 + * @return {ListNode} + */ +var mergeTwoLists = function(l1, l2) { + if(!l1) return l2; + if(!l2) return l1; + var head = null + if(l1.val < l2.val) { + head = l1; + l1 = l1.next; + } else { + head = l2; + l2 = l2.next; + } + + newlist = head; + while(l1 && l2) { + if(l1.val < l2.val) { + newlist.next = l1; + l1 = l1.next; + } else { + newlist.next = l2; + l2 = l2.next; + } + newlist = newlist.next; + } + if(!l1) { + newlist.next = l2; + } else { + newlist.next = l1; + } + + return head; +}; +``` + + +### 22. [Generate Parentheses](https://leetcode.com/problems/generate-parentheses/description/) +```javascript +/** + * @param {number} n + * @return {string[]} + */ +var generateParenthesis = function(n) { + if(n === 0) { + return []; + } else if(n === 1){ + return ['()']; + } else { + var parenthesis = generateParenthesis(n - 1); + var retObj = {}; + + parenthesis.forEach(p => { + for(var i = 0; i < p.length + 1; i++) { + var charArr = p.split(''); + + charArr.splice(i, 0, '()'); + retObj[charArr.join('')] = true; + } + }); + + return Object.keys(retObj); + } +}; + +``` + + +### 24. [Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/description/) +```javascript +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @return {ListNode} + */ +var swapPairs = function(head) { + if(head === null || head.next === null) { + return head; + } + + var newhead = null; + var tmp1 = head.next; + var tmp2 = head.next.next; + head.next.next = head; + head.next = tmp2; + newhead = tmp1; + + var p = head; + while(p && p.next && p.next.next) { + tmp1 = p.next.next; + tmp2 = p.next.next.next; + p.next.next.next = p.next; + p.next.next = tmp2; + p.next = tmp1; + p = p.next.next; + } + + return newhead; +}; +``` + + +### 26. [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/) +```javascript +/** + * @param {number[]} nums + * @return {number} + */ +var removeDuplicates = function(nums) { + var i = 0; + var j = 0; + while(j < nums.length) { + if(nums[i] == nums[j]) { + j++; + } else { + i++; + nums[i] = nums[j]; + j++; + } + } + return i + 1; + +}; +``` + + +### 27. [Remove Element](https://leetcode.com/problems/remove-element/description/) +```javascript +/** + * @param {number[]} nums + * @param {number} val + * @return {number} + */ +var removeElement = function(nums, val) { + var head = 0; + var tail = nums.length - 1; + while(head <= tail) { + if(nums[head] != val) { + head++; + continue; + } + + if(nums[tail] == val) { + tail--; + continue; + } + + nums[head] = nums[tail]; + tail--; + } + return head; +}; +``` + + +### 28. [Implement strStr()](https://leetcode.com/problems/implement-strstr/description/) +```javascript +/** + * @param {string} haystack + * @param {string} needle + * @return {number} + */ + +var strStr = function(haystack, needle) { + if (needle === '') return 0; + for(var i = 0;; i++) { + for(var j = 0;; j++) { + if (j == needle.length) return i; + if (i + j >= haystack.length) return -1; + if (haystack[i + j] != needle[j]) break; + } + } + return -1; +}; + +``` + + +### 29. [Divide Two Integers](https://leetcode.com/problems/divide-two-integers/description/) +```cpp +class Solution { +public: + int divide(int dividend, int divisor) { + if (!divisor || (dividend == INT_MIN && divisor == -1)) + return INT_MAX; + int sign = ((dividend < 0) ^ (divisor < 0)) ? -1 : 1; + long long dvd = labs(dividend); + long long dvs = labs(divisor); + int res = 0; + while (dvd >= dvs) { + long long temp = dvs, multiple = 1; + while (dvd >= (temp << 1)) { + temp <<= 1; + multiple <<= 1; + } + dvd -= temp; + res += multiple; + } + return sign == 1 ? res : -res; + } +}; +``` + + +### 31. [Next Permutation](https://leetcode.com/problems/next-permutation/description/) +```javascript +var nextPermutation = function(nums) { + if (nums.length <= 1) { + return; + } + + var i = nums.length - 2; + var j = nums.length - 1; + while (nums[i] >= nums[i + 1] && i > -1) { + i--; + } + if (i >= 0) { + while (nums[j] <= nums[i]) { + j--; + } + + var temp; + temp = nums[i]; + nums[i] = nums[j]; + nums[j] = temp; + } + + var start = i + 1; + var end = nums.length - 1; + + while (start < end) { + temp = nums[start]; + nums[start] = nums[end]; + nums[end] = temp; + start++; + end--; + } + +}; +``` + + +### 33. [Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/description/) +```javascript +/** + * @param {number[]} nums + * @param {number} target + * @return {number} + */ +function binarySearch(nums, target) { + var end = nums.length - 1; + var start = 0; + var middle = parseInt((start + end) / 2); + + while (end >= start) { + if (nums[middle] == target) { + return middle; + } else if(nums[middle] > target) { + end = middle - 1; + } else if(nums[middle] < target) { + start = middle + 1; + } + middle = parseInt((start + end) / 2); + } + return -1; +} + +var search = function(nums, target) { + var i = 0; + while(nums[i] <= nums[i+1]) { + i++; + } + + if (nums[0] <= target && target <= nums[i]) { + return binarySearch(nums.slice(0, i + 1), target); + } else if (nums[i + 1] <= target && target <= nums[nums.length - 1]){ + var ret = binarySearch(nums.slice(i + 1), target); + return ret == -1 ? -1 : i + 1 + ret; + } else { + return -1; + } +}; + +``` + + +### 35. [Search Insert Position](https://leetcode.com/problems/search-insert-position/description/) +```javascript +/** + * @param {number[]} nums + * @param {number} target + * @return {number} + */ +var searchInsert = function(nums, target) { + var start = 0; + var end = nums.length - 1; + var middle = parseInt((start + end) / 2); + + while(start <= end) { + if (target === nums[middle]) { + return middle; + } else if (target > nums[middle]) { + start = middle + 1; + } else if (target < nums[middle] ) { + end = middle - 1; + } + if (start > end) { + if (target < nums[middle]) { + return middle; + } else { + return middle + 1; + } + } else { + middle = parseInt((start + end) / 2); + } + } +}; +``` + + +### 36. [Valid Sudoku](https://leetcode.com/problems/valid-sudoku/description/) +```javascript +/** + * @param {character[][]} board + * @return {boolean} + */ +var isValidSudoku = function(board) { + var rowExist = {}; + var colExist = {}; + var boxExist = {}; + var k, value; + for (var i = 0; i < board.length; i++) { + for (var j = 0; j < board[i].length; j++) { + value = board[i][j]; + k = parseInt(i / 3, 10) * 3 + parseInt(j / 3, 10); //the number of the box + if (!rowExist[i]) { + rowExist[i] = {}; + } + if (!colExist[j]) { + colExist[j] = {}; + } + if (!boxExist[k]) { + boxExist[k] = {}; + } + + if (value !== '.') { + if (rowExist[i][value] || colExist[j][value] || boxExist[k][value]) { + return false; + } else { + rowExist[i][value] = true; + colExist[j][value] = true; + boxExist[k][value] = true; + } + } + } + } + return true; +}; +``` + + +### 38. [Count and Say](https://leetcode.com/problems/count-and-say/description/) +```javascript +/** + * @param {number} n + * @return {string} + */ + +var countAndSay = function(n) { + if (n === 1) return '1'; + + var last = countAndSay(n - 1); + var digit = last[0]; + var count = 0; + var ret = ''; + + for (var i = 0; i < last.length; i++) { + if (last[i] !== digit) { + ret += count + digit; + digit = last[i]; + count = 0; + } + count++; + } + ret += count + digit; + + return ret; +}; +``` + + +### 46. [Permutations](https://leetcode.com/problems/permutations/description/) +```javascript +/** + * @param {number[]} nums + * @return {number[][]} + */ +var permute = function(nums) { + var ret = []; + if(nums.length == 1) { + ret.push(nums); + return ret; + } + + var head, pre_ret, item, item_copy; + head = nums.shift(); + pre_ret = permute(nums); + for(var i = 0; i < pre_ret.length; i++ ) { + item = pre_ret[i]; + for(var j = 0; j <= item.length; j++) { + item_copy = [].slice.call(item); + item_copy.splice(j, 0, head); + ret.push(item_copy); + } + } + return ret; +}; +``` + + +### 53. [Maximum Subarray](https://leetcode.com/problems/maximum-subarray/description/) +```python +class Solution(object): + def maxSubArray(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + # dp[i] = dp[i - 1] + nums[i] (dp[i - i] > 0) + # dp[i] = nums[i] (dp[i - i] < 0) + dp = {} + dp[0] = nums[0] + maxv = nums[0] + + for i in range(1, len(nums)): + if dp[i - 1] > 0: + dp[i] = dp[i - 1] + nums[i] + else: + dp[i] = nums[i] + maxv = max(maxv, dp[i]) + + return maxv +``` + + +### 54. [Spiral Matrix](https://leetcode.com/problems/spiral-matrix/description/) +```javascript +/** + * @param {number[][]} matrix + * @return {number[]} + */ +var spiralOrder = function(matrix) { + if (matrix.length == 0) {return [];} + var n = matrix.length; + var m = matrix[0].length; + var up = 0, right = m - 1, down = n - 1, left = 0; + var ret = []; + + while(true) { + for(var r = left; r <= right; r++) ret.push(matrix[up][r]); + if (++up > down) break; + + for(var d = up; d <= down; d++) ret.push(matrix[d][right]); + if (--right < left) break; + + for(var l = right; l >= left; l--) ret.push(matrix[down][l]); + if (--down < up) break; + + for(var u = down; u >= up; u--) ret.push(matrix[u][left]); + if (++left > right) break; + } + return ret; +}; + +``` + + +### 58. [Length of Last Word](https://leetcode.com/problems/length-of-last-word/description/) +```javascript +/** + * @param {string} s + * @return {number} + */ +var lengthOfLastWord = function(s) { + return s.trim().split(' ').pop().length; +}; +``` + + +### 62. [Unique Paths](https://leetcode.com/problems/unique-paths/description/) +```javascript +/** + * @param {number} m + * @param {number} n + * @return {number} + */ +var uniquePaths = function(m, n) { + var dp = Array(n).fill(1).map(() => Array(m).fill(1)); + + for (var i = 1; i < n; i++) { + for (var j = 1; j < m; j++) { + dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; + } + } + + return dp[n - 1][m - 1]; +}; +``` + + +### 63. [Unique Paths II](https://leetcode.com/problems/unique-paths-ii/description/) +```javascript +/** + * @param {number[][]} obstacleGrid + * @return {number} + */ +var uniquePathsWithObstacles = function(obstacleGrid) { + var n = obstacleGrid.length; + var m = obstacleGrid[0].length; + + if (obstacleGrid[n - 1][m - 1] === 1 || obstacleGrid[0][0] === 1) return 0; + + var dp = Array(n).fill(null).map(() => Array(m).fill(0)); + for (var i = 0; i < m && obstacleGrid[0][i] === 0; i++) dp[0][i] = 1; + for (var j = 0; j < n && obstacleGrid[j][0] === 0; j++) dp[j][0] = 1; + + var top, left; + for (var i = 1; i < n; i++) { + for (var j = 1; j < m; j++) { + top = obstacleGrid[i - 1][j] === 1 ? 0 : dp[i - 1][j]; + left = obstacleGrid[i][j - 1] === 1 ? 0 : dp[i][j - 1]; + dp[i][j] = top + left; + } + } + + return dp[n - 1][m - 1]; +}; +``` + + +### 66. [Plus One](https://leetcode.com/problems/plus-one/description/) +```javascript +/** + * @param {number[]} digits + * @return {number[]} + */ +var plusOne = function(digits) { + digits.unshift(0); + var tail = digits.length - 1; + digits[tail]++; + while(tail > 0) { + if(digits[tail] == 10) { + digits[tail] = 0; + tail--; + digits[tail]++; + } else { + tail--; + } + } + if(digits[0] == 0) { + digits.shift(); + } + return digits; +}; +``` + + +### 69. [Sqrt(x)](https://leetcode.com/problems/sqrtx/description/) +```javascript +/** + * @param {number} x + * @return {number} + */ +var mySqrt = function(x) { + var start = 1; + var end = x; + var middle; + + while (start <= end) { + middle = start + parseInt((end - start) / 2); + if (x / middle === middle) { + return middle; + } else if (x / middle > middle) { + start = middle + 1; + } else { + end = middle - 1; + } + } + + return end; +}; + +``` + + +### 70. [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/description/) +```python +class Solution(object): + def climbStairs(self, n): + """ + :type n: int + :rtype: int + """ + # dp[i] 爬i阶台阶有多少种方式 + # dp[i] = dp[i - 1] + dp[i - 2] + dp = {} + dp[1] = 1 + dp[2] = 2 + + for i in range(3, n + 1): + dp[i] = dp[i - 1] + dp[i - 2] + + return dp[n] + +``` + + +### 73. [Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/description/) +```javascript +/** + * @param {number[][]} matrix + * @return {void} Do not return anything, modify matrix in-place instead. + */ +var setZeroes = function(matrix) { + var hasZeroFirstCol = false; + for(var i = 0; i < matrix.length; i++) { + if(matrix[i][0] === 0) { + hasZeroFirstCol = true; + } + for(var j = 1; j < matrix[i].length; j++) { + if(matrix[i][j] === 0) { + matrix[0][j] = matrix[i][0] = 0; + } + } + } + for(var y = matrix.length - 1; y >= 0; y--) { + for(var x = matrix[y].length - 1; x >= 1; x--) { + if(matrix[0][x] === 0 || matrix[y][0] === 0) { + matrix[y][x] = 0; + } + } + if(hasZeroFirstCol) { + matrix[y][0] = 0; + } + } +}; +``` + + +### 83. [Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/) +```javascript +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @return {ListNode} + */ +var deleteDuplicates = function(head) { + var iter = head; + while(iter) { + if(iter.next && iter.val === iter.next.val) { + iter.next = iter.next.next; + } else { + iter = iter.next; + } + } + return head; +}; +``` + + +### 88. [Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/description/) +```javascript +/** + * @param {number[]} nums1 + * @param {number} m + * @param {number[]} nums2 + * @param {number} n + * @return {void} Do not return anything, modify nums1 in-place instead. + */ +var merge = function(nums1, m, nums2, n) { + while(n > 0) { + if(m <= 0 || nums1[m - 1] <= nums2[n -1]) { + nums1[m + n - 1] = nums2[n - 1]; + n--; + } else { + nums1[m + n - 1] = nums1[m - 1]; + m--; + } + } +}; +``` + + +### 92. [Reverse Linked List II](https://leetcode.com/problems/reverse-linked-list-ii/description/) +```javascript +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @param {number} m + * @param {number} n + * @return {ListNode} + */ + +var reverseBetween = function(head, m, n) { + function reverseList(head) { + if(!head) { + return head; + } + var prev = null; + var next = null; + while(head) { + next = head.next; + head.next = prev; + prev = head; + head = next; + } + return prev; + } + if(m === n) { + return head; + } + var middleLeft = null; + var middleRight = null; + var left = null; + var right = null; + + var i = 1; + var point = head; + while(point) { + if(i + 1 === m) { + left = point; + } + if(i === m) { + middleLeft = point; + } + if(i === n) { + middleRight = point; + } + if(i === n + 1) { + right = point; + } + i++; + point = point.next; + } + if(left) { + left.next = null; + } + + middleRight.next = null; + reverseList(middleLeft); + middleLeft.next = right; + + if(left) { + left.next = middleRight; + return head; + } else { + return middleRight; + } +}; + +``` + + +### 94. [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/description/) +```python +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution(object): + def inorder(self, root, list): + if root == None: + return + self.inorder(root.left, list) + list.append(root.val) + self.inorder(root.right, list) + + def inorderTraversal(self, root): + l = [] + self.inorder(root, l) + return l +``` + + +### 98. [Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/description/) +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {boolean} + */ +function isValid(root, max, min) { + if (root === null) return true; + if (root.val >= max || root.val <= min) return false; + + return isValid(root.left, root.val, min) && isValid(root.right, max, root.val); +} +var isValidBST = function(root) { + return isValid(root, Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER); +}; + +``` + + +### 100. [Same Tree](https://leetcode.com/problems/same-tree/description/) +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} p + * @param {TreeNode} q + * @return {boolean} + */ +var isSameTree = function(p, q) { + if(p == null || q == null) { + return p == q; + } + if(p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right)) { + return true; + } else { + return false; + } +}; +``` + + +### 101. [Symmetric Tree](https://leetcode.com/problems/symmetric-tree/description/) +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {boolean} + */ + +var isSymmetric = function(root) { + function isSymmetricNode(left, right) { + if(!left && !right) { + return true; + } + if((left && !right) || (!left && right)) { + return false; + } + return (left.val == right.val) + && isSymmetricNode(left.left, right.right) + && isSymmetricNode(left.right, right.left); + } + if(root === null) { + return true; + } + var left = root.left; + var right = root.right; + return isSymmetricNode(left, right); +}; + +``` + + +### 102. [Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/description/) +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number[][]} + */ +var traversal = function(ret, root, depth) { + if(root === null) { + return; + } + if(!Array.isArray(ret[depth])) { + ret[depth] = []; + } + ret[depth].push(root.val); + traversal(ret, root.left, depth + 1); + traversal(ret, root.right, depth + 1); +} +var levelOrder = function(root) { + var ret = []; + traversal(ret, root, 0); + return ret; +}; +``` + + +### 104. [Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/description/) +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number} + */ +var maxDepth = function(root) { + if(root == null) { + return 0; + } else { + return Math.max(maxDepth(root.left), maxDepth(root.right)) +1; + } +}; +``` + + +### 107. [Binary Tree Level Order Traversal II](https://leetcode.com/problems/binary-tree-level-order-traversal-ii/description/) +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number[][]} + */ +var traversal = function(ret, root, depth) { + if(root === null) { + return; + } + if(!Array.isArray(ret[depth])) { + ret[depth] = []; + } + ret[depth].push(root.val); + traversal(ret, root.left, depth + 1); + traversal(ret, root.right, depth + 1); +} +var levelOrderBottom = function(root) { + var ret = []; + traversal(ret, root, 0); + return ret.reverse(); +}; +``` + + +### 108. [Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/) +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {number[]} nums + * @return {TreeNode} + */ + +var sortedArrayToBST = function(nums) { + if (nums.length === 0) {return null;} + var start = 0; + var end = nums.length - 1; + var middle = parseInt((start + end) / 2); + + var root, node1, node2, node3; + + if (nums.length <= 3) { + node1 = new TreeNode(nums[0]); + if (nums.length === 1) { + return node1; + } + + node2 = new TreeNode(nums[1]); + node2.left = node1; + if (nums.length === 2) { + return node2; + } + + node3 = new TreeNode(nums[2]); + node2.right = node3; + return node2; + + } else { + root = new TreeNode(nums[middle]); + root.left = sortedArrayToBST(nums.slice(0, middle)); + root.right = sortedArrayToBST(nums.slice(middle + 1)); + } + return root; +}; +``` + + +### 110. [Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/description/) +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {boolean} + */ +var maxDeepth = function(root) { + if(root === null) { + return 0; + } else { + return Math.max(maxDeepth(root.left), maxDeepth(root.right)) + 1; + } +} +var isBalanced = function(root) { + if(root === null) { + return true; + } + var leftDeepth = maxDeepth(root.left); + var rightDeepth = maxDeepth(root.right); + + if(Math.abs(leftDeepth - rightDeepth) <= 1 + && isBalanced(root.left) + && isBalanced(root.right) + ) { + return true; + } else { + return false; + } +}; +``` + + +### 111. [Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/description/) +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number} + */ +var minDepth = function(root) { + if(root === null) { + return 0; + } + var leftDepth = minDepth(root.left); + var rightDepth = minDepth(root.right); + + if(leftDepth !== 0 && rightDepth !== 0) { + return Math.min(leftDepth, rightDepth) + 1; + } else if(leftDepth === 0) { + return rightDepth + 1; + } else { + return leftDepth + 1; + } +}; +``` + + +### 112. [Path Sum](https://leetcode.com/problems/path-sum/description/) +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @param {number} sum + * @return {boolean} + */ +var hasPathSum = function(root, sum) { + if(root == null) { + return false; + } + if(root.left == null && root.right == null && sum == root.val) { + return true; + } + if(hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val)) { + return true; + } else { + return false; + } +}; +``` + + +### 118. [Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/description/) +```javascript +/** + * @param {number} numRows + * @return {number[][]} + */ +var generate = function(numRows) { + if(numRows === 0) { + return []; + } + if(numRows === 1) { + return [[1]]; + } + + var ret = [[1]]; + for(var i = 1; i < numRows; i++) { + ret.push([]); + for(var j = 0; j < ret[i - 1].length - 1; j++) { + ret[i].push(ret[i-1][j] + ret[i-1][j + 1]); + } + ret[i].unshift(1); + ret[i].push(1); + } + return ret; +}; +``` + + +### 119. [Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii/description/) +```javascript +/** + * @param {number} rowIndex + * @return {number[]} + */ +var getRow = function(rowIndex) { + var ret = []; + ret[0] = 1; + for(var i = 1; i <= rowIndex; i++ ) { + ret[i] = 1; + for(j = i - 1; j > 0; j--) { + ret[j] = ret[j] + ret[j - 1]; + } + } + return ret; +}; +``` + + +### 120. [Triangle](https://leetcode.com/problems/triangle/description/) +```javascript +/** + * @param {number[][]} triangle + * @return {number} + */ +var minimumTotal = function(triangle) { + if (triangle.length === 0 || triangle[0].length === 0) return 0; + var dp = Array(triangle.length).fill(null); + dp[0] = triangle[0][0]; + var minv = dp[0]; + var prev; + + for (var i = 1; i < triangle.length; i++) { + prev = dp.slice(); // copy dp + dp[0] = prev[0] + triangle[i][0]; + for (var j = 1; j < triangle[i].length - 1; j++) { + dp[j] = Math.min(prev[j - 1], prev[j]) + triangle[i][j]; + } + dp[triangle[i].length - 1] = prev[triangle[i].length - 2] + triangle[i][triangle[i].length - 1]; + } + + return Math.min.apply(null, dp); +}; +``` + + +### 121. [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/) +```javascript +/** + * @param {number[]} prices + * @return {number} + */ +var maxProfit = function(prices) { + var min = Number.MAX_VALUE; + var profit = 0; + + for(var i = 0; i < prices.length; i++) { + min = Math.min(min, prices[i]); + profit = Math.max(profit, prices[i] - min); + } + + return profit; +}; +``` + + +### 122. [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/) +```javascript +/** + * @param {number[]} prices + * @return {number} + */ +var maxProfit = function(prices) { + var len = prices.length; + var minsum = 0; + var maxsum = 0; + var isUp; + var lastIsUp; + + for(var i = 0; i < prices.length - 1; i++) { + if(prices[i] < prices[i+1]) { + isUp = true; + } else { + isUp = false; + } + if(isUp && (i === 0 || lastIsUp === false)) { + minsum += prices[i]; + } + if(lastIsUp && isUp === false) { + maxsum += prices[i]; + } + if(isUp && i === prices.length - 2) { + maxsum += prices[i + 1]; + } + lastIsUp = isUp; + } + return maxsum - minsum; +}; +``` + + +### 125. [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/description/) +```javascript +/** + * @param {string} s + * @return {boolean} + */ +var isPalindrome = function(s) { + var str = s.split('').filter(function(c) { + return c.match(/[\w\d]/); + }).join('').toLowerCase(); + + if(str === '') { + return true; + } + var i = 0; + var j = str.length - 1; + while(i < j) { + if(str[i] != str[j]) { + return false; + } + i++; + j--; + } + return true; +}; +``` + + +### 136. [Single Number](https://leetcode.com/problems/single-number/description/) +```javascript +/** + * @param {number[]} nums + * @return {number} + */ +var singleNumber = function(nums) { + return nums.reduce(function(prev, cur) { + return prev ^ cur; + }); +}; +``` + + +### 138. [Copy List with Random Pointer](https://leetcode.com/problems/copy-list-with-random-pointer/description/) +```javascript +/** + * Definition for singly-linked list with a random pointer. + * function RandomListNode(label) { + * this.label = label; + * this.next = this.random = null; + * } + */ + +/** + * @param {RandomListNode} head + * @return {RandomListNode} + */ + +var copyLinkList = function(head) { + if (head === null) { + return null; + } + + var new_head = new RandomListNode(head.label); + var new_current = new_head; + var node; + + head = head.next; + while(head !== null) { + node = new RandomListNode(head.label); + new_current.next = node; + new_current = node; + head = head.next; + } + + return new_head; +}; + +var copyRandomList = function(head) { + if (head === null) { + return null; + } + + var new_head = copyLinkList(head); + var new_list = []; + var old_list = []; + + var new_curr = new_head; + var old_curr = head; + while (new_curr !== null) { + new_list.push(new_curr); + old_list.push(old_curr); + new_curr = new_curr.next; + old_curr = old_curr.next; + } + + for (var i = 0; i < new_list.length; i++) { + new_list[i].random = old_list[i]; + old_list[i].next = new_list[i]; + } + + for (i = 0; i < new_list.length; i++) { + if (old_list[i].random === null) { + new_list[i].random = null; + } else { + new_list[i].random = old_list[i].random.next; + } + } + + for (i = 0; i < old_list.length - 1; i++) { + old_list[i].next = old_list[i + 1]; + } + old_list[old_list.length - 1].next = null; + + + return new_head; +}; + + +``` + + +### 139. [Word Break](https://leetcode.com/problems/word-break/description/) +```python +class Solution(object): + def wordBreak(self, s, wordDict): + """ + :type s: str + :type wordDict: List[str] + :rtype: bool + """ + + #dp[i] measn word[:i+1] can be segmented + + dp = {} + dp[0] = True + for i in range(1, len(s) + 1): + for j in range(0, i): + if dp.get(j, False) and s[j:i] in wordDict: + dp[i] = True + + return dp.get(len(s), False) +``` + + +### 141. [Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/description/) +```javascript +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ + +/** + * @param {ListNode} head + * @return {boolean} + */ +var hasCycle = function(head) { + if (head === null) {return false;} + var fast = head; + var slow = head; + + while(slow.next !== null && slow.next.next !== null) { + fast = fast.next; + slow = slow.next.next; + if (slow === fast) return true; + } + + return false; +}; +``` + + +### 144. [Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/description/) +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number[]} + */ + +var preorderTraversal = function(root) { + if(root === null) return []; + var ret = []; + + function pre(root) { + if(root) { + ret.push(root.val); + pre(root.left); + pre(root.right); + } + } + pre(root); + return ret; +}; +``` + + +### 146. [LRU Cache](https://leetcode.com/problems/lru-cache/description/) +```python +class Node: + def __init__(self, k, v): + self.key = k + self.val = v + self.prev = None + self.next = None + +class LRUCache(object): + + def __init__(self, capacity): + """ + :type capacity: int + """ + self.capacity = capacity + self.head = Node('head', None) + self.tail = Node('tail', None) + self.map = {} + self.head.next = self.tail + self.tail.prev = self.head + + + def get(self, key): + """ + :type key: int + :rtype: int + """ + if not self.map.has_key(key): + return -1 + target = self.map[key] + self._remove(target) + self._add(target) + return target.val + + def put(self, key, value): + """ + :type key: int + :type value: int + :rtype: void + """ + if self.map.has_key(key): + self._remove(self.map[key]) + target = Node(key, value) + self._add(target) + self.map[key] = target + + if len(self.map) > self.capacity: + delete_node = self.head.next + self._remove(delete_node) + del self.map[delete_node.key] + + # add before the tail + def _add(self, node): + p_node = self.tail.prev + p_node.next = node + node.next = self.tail + self.tail.prev = node + node.prev = p_node + + # remove the node + def _remove(self, node): + p_node = node.prev + n_node = node.next + p_node.next = n_node + n_node.prev = p_node + +``` + + +### 148. [Sort List](https://leetcode.com/problems/sort-list/description/) +```javascript +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @return {ListNode} + */ +var sortList = function(head) { + function merge(l1, l2) { + if(!l1) return l2; + if(!l2) return l1; + var head = null + if(l1.val < l2.val) { + head = l1; + l1 = l1.next; + } else { + head = l2; + l2 = l2.next; + } + var newlist = head; + while(l1 && l2) { + if(l1.val < l2.val) { + newlist.next = l1; + l1 = l1.next; + } else { + newlist.next = l2; + l2 = l2.next; + } + newlist = newlist.next; + } + if(!l1) { + newlist.next = l2; + } else { + newlist.next = l1; + } + return head; + } + + if(!head || !head.next) { + return head; + } + + var p1 = head; + var p2 = head; + + // p1 go step 1 + // p2 go step 2 + while(p1 && p2) { + p2 = p2.next; + if(p2) { + p2 = p2.next; + } + if(!p2) { + break; + } + p1 = p1.next; + } + + var right = p1.next; + p1.next = null; + var left = head; + + return merge(sortList(left), sortList(right)); +} +``` + + +### 149. [Max Points on a Line](https://leetcode.com/problems/max-points-on-a-line/description/) +```cpp +/** + * Definition for a point. + * struct Point { + * int x; + * int y; + * Point() : x(0), y(0) {} + * Point(int a, int b) : x(a), y(b) {} + * }; + */ +class Solution { +public: + int maxPoints(vector &points) { + if(points.size() < 3) { + return points.size(); + } + int maxPoints = 0; //the max point in line + int size = points.size(); + map count; + map::iterator iter; + for(int i = 0; i < size; i++ ) { + int x1 = points[i].x; + int y1 = points[i].y; + int coincideCount = 0; //number of duplicate points + count.clear(); + count[(double)INT_MIN] = 0; + for(int j = i + 1; j < size; j++) { + int x2 = points[j].x; + int y2 = points[j].y; + if(x1 == x2 && y1 == y2) { + coincideCount++; + } else if(x1 == x2){ + count[(double)INT_MIN]++; + } else { + double slope = 1.0*(y1-y2)/(x1-x2); + count[slope]++; + } + } + for(iter = count.begin(); iter != count.end(); iter++) { + if(iter->second + coincideCount > maxPoints) { + maxPoints = iter->second + coincideCount; + } + } + } + maxPoints = maxPoints + 1; + return maxPoints; + } +}; +``` + + +### 150. [Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/description/) +```javascript +/** + * @param {string[]} tokens + * @return {number} + */ + var evalRPN = function(tokens) { + var stack = []; + var a, b, result; + for(var i = 0; i < tokens.length; i++) { + if(Number.isNaN(parseInt(tokens[i]))) { + b = stack.pop(); + a = stack.pop(); + if(tokens[i] == '+') { + result = a + b; + } else if(tokens[i] == '-') { + result = a - b; + } else if(tokens[i] == '*') { + result = a * b; + } else if(tokens[i] == '/') { + result = a / b; + } + stack.push(parseInt(result, 10)); + } else { + stack.push(parseInt(tokens[i], 10)); + } + } + return stack.pop(); +}; +``` + + +### 151. [Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/description/) +```javascript +/** + * @param {string} str + * @returns {string} + */ +var reverseWords = function(str) { + return str.split(' ').reverse().filter(function(item) { + return '' != item; + }).join(' ').trim(); +}; +``` + + +### 152. [Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/description/) +```python +class Solution(object): + def maxProduct(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + ret = maxv = minv = nums[0] + + for i in nums[1:]: + if i >= 0: + maxv = max(i, maxv * i) + minv = min(i, minv * i) + else: + maxv_tmp = maxv + maxv = max(i, minv * i) + minv = min(i, maxv_tmp * i) + + ret = max(ret, maxv) + + return ret +``` + + +### 155. [Min Stack](https://leetcode.com/problems/min-stack/description/) +```javascript +/** + * @constructor + */ +var MinStack = function() { + this.stack = []; + this.min = Number.MAX_SAFE_INTEGER; +}; + +/** + * @param {number} x + * @returns {void} + */ +MinStack.prototype.push = function(x) { + if(x < this.min) { + this.min = x; + } + this.stack.push(x); +}; + +/** + * @returns {void} + */ +MinStack.prototype.pop = function() { + var number = this.stack.pop(); + if(number == this.min) { + this.min = Math.min.apply(null, this.stack); + } +}; + +/** + * @returns {number} + */ +MinStack.prototype.top = function() { + if(this.stack.length > 0) { + return this.stack[this.stack.length - 1]; + } else { + return undefined; + } +}; + +/** + * @returns {number} + */ +MinStack.prototype.getMin = function() { + return this.min; +}; +``` + + +### 160. [Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/description/) +```javascript +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ + +/** + * @param {ListNode} headA + * @param {ListNode} headB + * @return {ListNode} + */ +var getIntersectionNode = function(headA, headB) { + if(headA === null || headB === null) { + return null; + } + var pointA = headA; + var pointB = headB; + var i = 0; + var j = 0; + var k = 0; + while(pointA.next !== null) { + pointA = pointA.next; + i++; + } + while(pointB.next !== null) { + pointB = pointB.next; + j++; + } + if(pointB != pointA) { + return null; + } + pointA = headA; + pointB = headB; + + if(i > j) { + while(k < i - j){pointA = pointA.next;k++;} + } else { + while(k < j - i){pointB = pointB.next;k++;} + } + while(pointA != pointB) { + pointA = pointA.next; + pointB = pointB.next; + } + return pointA; +}; +``` + + +### 162. [Find Peak Element](https://leetcode.com/problems/find-peak-element/description/) +```javascript +/** + * @param {number[]} nums + * @return {number} + */ + +function find(nums, low, high) { + if (low === high) { + return low; + } else { + var mid1 = parseInt((low + high) / 2); + var mid2 = mid1 + 1; + + if (nums[mid1] > nums[mid2]) { + return find(nums, low, mid1); + } else { + return find(nums, mid2, high); + } + } +} +var findPeakElement = function(nums) { + return find(nums, 0, nums.length - 1); +}; + +``` + + +### 165. [Compare Version Numbers](https://leetcode.com/problems/compare-version-numbers/description/) +```javascript +/** + * @param {string} version1 + * @param {string} version2 + * @return {number} + */ +var compareVersion = function(version1, version2) { + var arr1 = version1.split('.').map(function(item) { + return parseInt(item); + }); + var arr2 = version2.split('.').map(function(item) { + return parseInt(item); + }); + var a, b; + for(var i = 0; i < arr1.length || i < arr2.length; i++) { + a = i >= arr1.length ? 0 : arr1[i]; + b = i >= arr2.length ? 0 : arr2[i]; + if(a > b) { + return 1; + } else if(a < b) { + return -1; + } + } + return 0; +}; +``` + + +### 166. [Fraction to Recurring Decimal](https://leetcode.com/problems/fraction-to-recurring-decimal/description/) +```javascript +/** + * @param {number} numerator + * @param {number} denominator + * @return {string} + */ +var fractionToDecimal = function(numerator, denominator) { + // be divided with no remainder + if (numerator % denominator === 0) { + return String(numerator / denominator); + } + + // plus or minus + var ret = ''; + if (numerator * denominator < 0) ret += '-'; + numerator = Math.abs(numerator); + denominator = Math.abs(denominator); + + // integral part + var remain; + ret += Math.floor(numerator / denominator) + '.'; + remain = numerator % denominator; + + // decimal part + var digit; + var decimal = ''; + var i = 0; + var remainIndexMap = {}; + /* + * remain and the decimal part index + * e.g. 2 / 333 = 0.(006) + * remainIndexMap['2'] = 0; + * remainIndexMap['20'] = 1; + * remainIndexMap['200'] = 2; + * The decimal is 006 + */ + remainIndexMap[remain] = 0; + + while (true) { + remain *= 10; + digit = Math.floor(remain / denominator); + decimal += digit; + remain = remain % denominator; + + if (remainIndexMap[remain] !== undefined) { + ret += `${decimal.slice(0, remainIndexMap[remain])}(${decimal.slice(remainIndexMap[remain])})`; + break; + } else { + remainIndexMap[remain] = ++i; + } + + if (remain === 0) { + ret += decimal; + break; + } + } + + return ret; +}; + + +``` + + +### 167. [Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/) +```javascript +/** + * @param {number[]} numbers + * @param {number} target + * @return {number[]} +*/ +var twoSum = function(numbers, target) { + var start = 0; + var end = numbers.length - 1; + + while (start < end) { + if (numbers[start] + numbers[end] === target) { + break; + } else if (numbers[start] + numbers[end] < target) { + start++; + } else { + end--; + } + } + + return [start + 1, end + 1]; +}; + +``` + + +### 168. [Excel Sheet Column Title](https://leetcode.com/problems/excel-sheet-column-title/description/) +```javascript +/** + * @param {number} n + * @return {string} + */ +var convertToTitle = function(n) { + var ret = '' + while(n > 0) { + n--; + ret = String.fromCharCode(65 + n % 26) + ret; + n = Math.floor(n / 26); + } + return ret; +}; +``` + + +### 169. [Majority Element](https://leetcode.com/problems/majority-element/description/) +```javascript +/** + * @param {number[]} nums + * @return {number} + */ +var majorityElement = function(nums) { + var count = 1; + var item = nums[0]; + for(var i = 1; i < nums.length; i++) { + if(count === 0) { + count = 1; + item = nums[i]; + } else { + if(nums[i] == item) { + count++; + } else { + count--; + } + } + } + return item; +}; +``` + + +### 170. [Two Sum III - Data structure design](https://leetcode.com/problems/two-sum-iii-data-structure-design/description/) +```python +class TwoSum: + + # initialize your data structure here + def __init__(self): + self.table = dict() + + # @return nothing + def add(self, number): + self.table[number] = self.table.get(number, 0) + 1; + + # @param value, an integer + # @return a Boolean + def find(self, value): + for i in self.table.keys(): + j = value - i + if i == j and self.table.get(i) > 1 or i != j and self.table.get(j, 0) > 0: + return True + return False +``` + + +### 171. [Excel Sheet Column Number](https://leetcode.com/problems/excel-sheet-column-number/description/) +```javascript +/** + * @param {string} s + * @return {number} + */ +var titleToNumber = function(s) { + var arr = s.split(''); + var digit; + var sum = 0; + for(var i = arr.length - 1, j = 0; i >= 0; i--, j++) { + digit = arr[i].charCodeAt(0) - 64; + sum += digit * Math.pow(26, j) + } + return sum; +}; +``` + + +### 172. [Factorial Trailing Zeroes](https://leetcode.com/problems/factorial-trailing-zeroes/description/) +```javascript +/** + * @param {number} n + * @return {number} + */ +var trailingZeroes = function(n) { + var sum = 0; + var divisor = 5; + while(n >= divisor) { + sum += Math.floor(n / divisor); + divisor *= 5; + } + return sum; +}; +``` + + +### 173. [Binary Search Tree Iterator](https://leetcode.com/problems/binary-search-tree-iterator/description/) +```javascript +/** + * Definition for binary tree + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ + +/** + * @constructor + * @param {TreeNode} root - root of the binary search tree + */ +var BSTIterator = function(root) { + this.root = root; + this.stack = []; + this._push(this.root); +}; + + +/** + * @this BSTIterator + * @returns {boolean} - whether we have a next smallest number + */ +BSTIterator.prototype.hasNext = function() { + return this.stack.length > 0; +}; + +/** + * @this BSTIterator + * @returns {number} - the next smallest number + */ +BSTIterator.prototype.next = function() { + if (this.stack.length > 0) { + var node = this.stack.pop(); + this._push(node.right); + + return node.val; + } +}; + +BSTIterator.prototype._push = function(curr) { + while (curr) { + this.stack.push(curr); + curr = curr.left; + } +}; + +/** + * Your BSTIterator will be called like this: + * var i = new BSTIterator(root), a = []; + * while (i.hasNext()) a.push(i.next()); +*/ +``` + + +### 179. [Largest Number](https://leetcode.com/problems/largest-number/description/) +```javascript +/** + * @param {number[]} nums + * @return {string} + */ +var largestNumber = function(nums) { + var retStr = nums.sort(function(num1, num2) { + var arr1 = num1 + ''.split(); + var arr2 = num2 + ''.split(); + var len1 = arr1.length; + var len2 = arr2.length; + var a, b; + + for (var i = 0; i < len1 || i < len2; i++) { + a = i >= arr1.length ? arr1[i % len1] : arr1[i]; + b = i >= arr2.length ? arr2[i % len2] : arr2[i]; + if (a != b) { + return b - a; + } + } + + var isRise; + var checkArr = len1 > len2 ? arr1 : arr2; + for (var j = 0; j < checkArr.length - 1; j++) { + if (checkArr[j] != checkArr[j + 1]) { + if (checkArr[j] > checkArr[j + 1]) { + isRise = false; + break; + } else { + isRise = true; + break; + } + } + } + if (isRise) { + return len1 - len2; + } else { + return len2 - len1; + } + + + }).join(''); + + if (retStr[0] == '0') { + return '0'; + } else { + return retStr; + } +}; +``` + + +### 187. [Repeated DNA Sequences](https://leetcode.com/problems/repeated-dna-sequences/description/) +```javascript +/** +* @param {string} s +* @return {string[]} +*/ +var findRepeatedDnaSequences = function(s) { + var ret = []; + var uniqMap = {}; + var valueMap = {}; + var charCodeA = 'A'.charCodeAt(0); + + valueMap['A'.charCodeAt(0) - charCodeA] = 0; + valueMap['C'.charCodeAt(0) - charCodeA] = 1; + valueMap['G'.charCodeAt(0) - charCodeA] = 2; + valueMap['T'.charCodeAt(0) - charCodeA] = 3; + + var bitValue = 0; + var offset = 0; + for (var i = 0; i < s.length - 9; i++) { + for (var j = i; j < i + 10; j++) { + bitValue |= valueMap[s.charCodeAt(j) - charCodeA] << offset; + offset += 2; + } + if (uniqMap[bitValue] === undefined) { + uniqMap[bitValue] = true; + } else if (uniqMap[bitValue] === true) { + ret.push(s.slice(i, 10 + i)); + uniqMap[bitValue] = false; + } + + offset = 0; + bitValue = 0; + } + + return ret; +}; + +``` + + +### 189. [Rotate Array](https://leetcode.com/problems/rotate-array/description/) +```javascript +/** + * @param {number[]} nums + * @param {number} k + * @return {void} Do not return anything, modify nums in-place instead. + */ +var rotate = function(nums, k) { + for(var i = 0; i < k; i++) { + nums.unshift(nums.pop()); + } +}; +``` + + +### 190. [Reverse Bits](https://leetcode.com/problems/reverse-bits/description/) +```c +uint32_t reverseBits(uint32_t n) { + uint32_t mask = 1 << 31; + uint32_t ret = 0; + + for (uint32_t i = 0; i < 32; i++) { + if (n & 1 == 1) { + ret = ret | mask; + } + n = n >> 1; + mask = mask >> 1; + } + + return ret; +} + +``` + + +### 191. [Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/description/) +```javascript +/** + * @param {number} n - a positive integer + * @return {number} + */ +var hammingWeight = function(n) { + var count = 0; + while (n) { + n = n & (n - 1); + count++; + } + + return count; +}; +``` + + +### 198. [House Robber](https://leetcode.com/problems/house-robber/description/) +```javascript +/** + * @param {number[]} nums + * @return {number} + */ +var rob = function(nums) { + if(nums.length === 0) { + return 0; + } + if(nums.length === 1) { + return nums[0]; + } + + var max = {}; + max[0] = nums[0]; + max[1] = Math.max(nums[0], nums[1]); + for(var i = 2; i < nums.length; i++) { + max[i] = Math.max(max[i - 2] + nums[i], max[i -1]); + } + return max[nums.length - 1]; +}; +``` + + +### 200. [Number of Islands](https://leetcode.com/problems/number-of-islands/description/) +```javascript +/** + * @param {character[][]} grid + * @return {number} + */ +var numIslands = function(grid) { + var m = grid.length; + if (m === 0) return 0; + var n = grid[0].length; + + var island = []; + for (var i = 0; i < m; i++) { + for (var j = 0; j < n; j++) { + if(grid[i][j] === '1') { + island.push([i, j]); + grid[i][j] = false; + } + } + } + function nextUnvisit(island) { + for (var k = 0; k < island.length; k++) { + var [i, j] = island[k]; + if (grid[i][j] === false) { + return [i, j]; + } + } + return null; + } + + var count = 0; + while(nextUnvisit(island) !== null) { + count++; + var queue = []; + queue.push(nextUnvisit(island)); + while (queue.length > 0) { + var [i, j] = queue.pop(); + grid[i][j] = true; + var list = [[-1, 0], [1, 0], [0, -1], [0, 1]]; + for(var key in list) { + var [p, q] = list[key]; + var x = i + p; + var y = j + q; + if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] === false) { + queue.push([x, y]); + } + } + } + } + + return count; +}; + +``` + + +### 201. [Bitwise AND of Numbers Range](https://leetcode.com/problems/bitwise-and-of-numbers-range/description/) +```javascript +/** + * @param {number} m + * @param {number} n + * @return {number} + */ +var rangeBitwiseAnd = function(m, n) { + var count = 0; + + while (m !== n) { + m = m >> 1; + n = n >> 1; + count++; + } + + return m << count; +}; +``` + + +### 202. [Happy Number](https://leetcode.com/problems/happy-number/description/) +```javascript +/** + * @param {number} n + * @return {boolean} + */ + +var isHappy = function(n) { + var nums = []; + var ret = n; + + var squire = x => Math.pow(parseInt(x), 2); + var add = (prev, cur) => prev + cur; + while(ret !== 1) { + if(nums.indexOf(ret) > -1) { + return false; + } else { + nums.push(ret); + } + digits = ret.toString().split('').map(squire); + ret = digits.reduce(add); + } + return true; +} +``` + + +### 203. [Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements/description/) +```javascript +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @param {number} val + * @return {ListNode} + */ +var removeElements = function(head, val) { + while(head && head.val == val) { + head = head.next; + } + + if(!head) { + return head; + } + + var prev = head; + var iter = head.next; + + while(iter) { + if(iter.val == val) { + prev.next = iter.next; + } else { + prev = iter; + } + iter = iter.next; + } + + return head; +}; +``` + + +### 204. [Count Primes](https://leetcode.com/problems/count-primes/description/) +```javascript +/** + * @param {number} n + * @return {number} + */ +var countPrimes = function(n) { + var isPrime = []; + for (var i = 2; i < n; i++) { + isPrime[i] = true; + } + + for (var i = 2; i * i < n; i++) { + if (isPrime[i]) { + for (var j = i * i; j < n; j += i) { + isPrime[j] = false; + } + } + } + + var count = 0; + for (var i = 2; i < n; i++) { + if (isPrime[i]) { + count++; + } + } + + return count; +}; + +``` + + +### 205. [Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings/description/) +```javascript +/** + * @param {string} s + * @param {string} t + * @return {boolean} + */ +var isIsomorphic = function(s, t) { + if (s.length !== t.length) return false; + + var smap = {}; + var tmap = {}; + + for (var i = 0; i < s.length; i++) { + if (smap[s[i]] === undefined && tmap[t[i]] === undefined) { + smap[s[i]] = t[i]; + tmap[t[i]] = s[i]; + } + + if (smap[s[i]] !== t[i] || tmap[t[i]] !== s[i] ){ + return false; + } + } + + return true; +}; +``` + + +### 206. [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/description/) +```javascript +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @return {ListNode} + */ +var reverseList = function(head) { + if (head === null || head.next === null) return head; + + var prev = head; + var p = head.next; + var tmp; + + head.next = null; + while (p !== null) { + tmp = p.next; + p.next = prev; + prev = p; + p = tmp; + } + + return prev; +}; +``` + + +### 208. [Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree/description/) +```python +class Node(object): + def __init__(self, val): + self.isEnd = False + self.val = val + self.dict = {} + +class Trie(object): + + def __init__(self): + """ + Initialize your data structure here. + """ + self.root = Node('root') + + def insert(self, word): + """ + Inserts a word into the trie. + :type word: str + :rtype: void + """ + + cur = self.root + for i in word: + if cur.dict.get(i) == None: + cur.dict[i] = Node(i) + cur = cur.dict[i] + + cur.isEnd = True + + def search(self, word): + """ + Returns if the word is in the trie. + :type word: str + :rtype: bool + """ + cur = self.root; + for i in word: + if cur.dict.get(i): + cur = cur.dict[i] + else: + return False + + return cur.isEnd + + def startsWith(self, prefix): + """ + Returns if there is any word in the trie that starts with the given prefix. + :type prefix: str + :rtype: bool + """ + cur = self.root + for i in prefix: + if cur.dict.get(i): + cur = cur.dict[i] + else: + return False + return True + + +# Your Trie object will be instantiated and called as such: +# obj = Trie() +# obj.insert(word) +# param_2 = obj.search(word) +# param_3 = obj.startsWith(prefix) +``` + + +### 217. [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/description/) +```javascript +/** + * @param {number[]} nums + * @return {boolean} + */ +var containsDuplicate = function(nums) { + for(var i = 0; i < nums.length; i++) { + for(var j = i + 1 ; j < nums.length; j++) { + if(nums[i] == nums[j]) { + return true; + } + } + } + return false; +}; +``` + + +### 219. [Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/description/) +```javascript +/** + * @param {number[]} nums + * @param {number} k + * @return {boolean} + */ + /* +var containsNearbyDuplicate = function(nums, k) { + for(var i = 0; i < nums.length; i++) { + for(var j = i + 1; j < nums.length && j - i <= k; j++) { + if(nums[i] === nums[j]) { + return true; + } + } + } + return false; +}; +*/ + +var containsNearbyDuplicate = function(nums, k) { + var index = {}; + var value; + for(var i = 0; i < nums.length; i++) { + value = nums[i]; + if(index[value] === undefined) { + index[value] = [i]; + } else if(i - index[value][index[value].length - 1] <= k){ + return true; + } else { + index[value].push(i); + } + } + return false; +} +``` + + +### 223. [Rectangle Area](https://leetcode.com/problems/rectangle-area/description/) +```javascript +/** + * @param {number} A + * @param {number} B + * @param {number} C + * @param {number} D + * @param {number} E + * @param {number} F + * @param {number} G + * @param {number} H + * @return {number} + */ +var computeArea = function(A, B, C, D, E, F, G, H) { + var areaA = (C-A) * (D-B); + var areaB = (G-E) * (H-F); + + var left = Math.max(A, E); + var right = Math.min(G, C); + var bottom = Math.max(F, B); + var top = Math.min(D, H); + + var overlap = 0; + + if(right > left && top > bottom) { + overlap = (right - left) * (top - bottom); + } + return areaA + areaB - overlap; +}; +``` + + +### 225. [Implement Stack using Queues](https://leetcode.com/problems/implement-stack-using-queues/description/) +```python +class MyStack(object): + + def __init__(self): + """ + Initialize your data structure here. + """ + self.queue = [] + + def push(self, x): + """ + Push element x onto stack. + :type x: int + :rtype: void + """ + self.queue.append(x) + for i in range(len(self.queue) - 1): + self.queue.append(self.queue.pop(0)) + + + def pop(self): + """ + Removes the element on top of the stack and returns that element. + :rtype: int + """ + return self.queue.pop(0) + + def top(self): + """ + Get the top element. + :rtype: int + """ + return self.queue[0] + + def empty(self): + """ + Returns whether the stack is empty. + :rtype: bool + """ + return len(self.queue) == 0 + + + +# Your MyStack object will be instantiated and called as such: +# obj = MyStack() +# obj.push(x) +# param_2 = obj.pop() +# param_3 = obj.top() +# param_4 = obj.empty() +``` + + +### 226. [Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/description/) +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {TreeNode} + */ +var invertTree = function(root) { + if(root !== null) { + var tmp; + tmp = root.left; + root.left = root.right; + root.right = tmp; + + invertTree(root.left); + invertTree(root.right); + } + return root; +}; +``` + + +### 228. [Summary Ranges](https://leetcode.com/problems/summary-ranges/description/) +```javascript +/** + * @param {number[]} nums + * @return {string[]} + */ +var summaryRanges = function(nums) { + if(nums.length === 0) { + return nums; + } + + var ret = []; + var start = 0; + + for(var i = 1; i < nums.length; i++) { + if(nums[i] - nums[i-1] != 1) { + ret.push(nums.slice(start, i)); + start = i; + } + } + ret.push(nums.slice(start)); + + ret = ret.map(function(item) { + if(item.length > 1) { + return item[0] + '->' + item[item.length - 1]; + } else { + return item[0] + ''; + } + }); + + return ret; +}; +``` + + +### 231. [Power of Two](https://leetcode.com/problems/power-of-two/description/) +```javascript +/** + * @param {number} n + * @return {boolean} + */ +var isPowerOfTwo = function(n) { + if(n === 0) return false; + while(n !== 1) { + if(n % 2 === 0) { + n = n / 2; + } else { + return false; + } + } + return true; +}; +``` + + +### 232. [Implement Queue using Stacks](https://leetcode.com/problems/implement-queue-using-stacks/description/) +```javascript +/** + * @constructor + */ +var Queue = function() { + this.stack1 = []; + this.stack2 =[]; +}; + +/** + * @param {number} x + * @returns {void} + */ +Queue.prototype.push = function(x) { + this.stack1.push(x); +}; + +/** + * @returns {void} + */ +Queue.prototype.pop = function() { + if(this.stack2.length > 0) { + return this.stack2.pop(); + } + if(this.stack1.length > 0) { + while(this.stack1.length > 0) { + this.stack2.push(this.stack1.pop()); + } + return this.stack2.pop(); + } + return null; +}; + +/** + * @returns {number} + */ +Queue.prototype.peek = function() { + if(this.stack2.length > 0) { + return this.stack2[this.stack2.length - 1]; + } + if(this.stack1.length > 0) { + return this.stack1[0]; + } + return null; +}; + +/** + * @returns {boolean} + */ +Queue.prototype.empty = function() { + return this.stack1.length === 0 && this.stack2.length === 0; +}; +``` + + +### 234. [Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list/description/) +```javascript +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @return {boolean} + */ +var isPalindrome = function(head) { + function reverseList(head) { + if(!head) { + return head; + } + var prev = null; + var next = null; + while(head) { + next = head.next; + head.next = prev; + prev = head; + head = next; + } + return prev; + } + if(!head || !head.next ) { + return true; + } + var slow = head; + var fast = head; + while(fast && fast.next && fast.next.next) { + slow = slow.next; + fast = fast.next.next; + } + + slow.next = reverseList(slow.next); + slow = slow.next; + + while(slow) { + if(head.val !== slow.val) { + return false; + } + head = head.next; + slow = slow.next; + } + + return true; +}; +``` + + +### 235. [Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/) +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @param {TreeNode} p + * @param {TreeNode} q + * @return {TreeNode} + */ +var lowestCommonAncestor = function(root, p, q) { + var tmp; + if(p.val > q.val) { + tmp = p; + p = q; + q = tmp; + } + if(root.val >= p.val && root.val <= q.val) { + return root; + }else if(p.val < root.val & q.val < root.val) { + return lowestCommonAncestor(root.left, p, q); + } else if(p.val > root.val && q.val > root.val){ + return lowestCommonAncestor(root.right, p, q); + } +}; +``` + + +### 237. [Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/description/) +```javascript +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} node + * @return {void} Do not return anything, modify node in-place instead. + */ +var deleteNode = function(node) { + node.val = node.next.val; + node.next = node.next.next; +}; +``` + + +### 238. [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/description/) +```javascript +/** + * @param {number[]} nums + * @return {number[]} + */ +var productExceptSelf = function(nums) { + var zeroCount = 0; + var products = nums + .filter((num) => { + if(num === 0) zeroCount++; + return num !== 0; + }) + .reduce((prev, curr) => prev * curr, 1); + return nums.map((num) => { + if(zeroCount >= 2) { + return 0; + } + if(zeroCount === 1) { + return num === 0 ? products : 0; + } + return products / num; + + }); +}; +``` + + +### 242. [Valid Anagram](https://leetcode.com/problems/valid-anagram/description/) +```javascript +/** + * @param {string} s + * @param {string} t + * @return {boolean} + */ +var isAnagram = function(s, t) { + var arr1 = s.split('').sort(); + var arr2 = t.split('').sort(); + if(arr1.length !== arr2.length) { + return false; + } + for(var i = 0; i < arr1.length; i++) { + if(arr1[i] !== arr2[i]) { + return false; + } + } + return true; +}; +``` + + +### 243. [Shortest Word Distance](https://leetcode.com/problems/shortest-word-distance/description/) +```python +import sys +class Solution(object): + def shortestDistance(self, words, word1, word2): + """ + :type words: List[str] + :type word1: str + :type word2: str + :rtype: int + """ + index1 = index2 = None + minDist = sys.maxint + + for k, v in enumerate(words): + if v == word1: + index1 = k + if v == word2: + index2 = k + if index1 != None and index2 != None: + minDist = min(minDist, abs(index1 - index2)) + + return minDist +``` + + +### 244. [Shortest Word Distance II](https://leetcode.com/problems/shortest-word-distance-ii/description/) +```python +import sys +class WordDistance(object): + + def __init__(self, words): + """ + :type words: List[str] + """ + self.position = {} + for index, word in enumerate(words): + self.position[word] = self.position.get(word, []) + [index] + + def shortest(self, word1, word2): + """ + :type word1: str + :type word2: str + :rtype: int + """ + l1, l2 = self.position[word1], self.position[word2] + i = j = 0 + res = sys.maxint + + while i < len(l1) and j < len(l2): + res = min(res, abs(l1[i] - l2[j])) + if l1[i] < l2[j]: + i += 1 + else: + j += 1 + return res + + + +# Your WordDistance object will be instantiated and called as such: +# obj = WordDistance(words) +# param_1 = obj.shortest(word1,word2) +``` + + +### 257. [Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/description/) +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {string[]} + */ +var binaryTreePaths = function(root) { + if(root === null) { + return []; + } + var ret = []; + var str = arguments[1] ? arguments[1] : ''; + if(str) { + str = str + '->' + root.val; + } else { + str = root.val + ''; + } + if(root.left === null && root.right === null) { + ret.push(str); + } + + if(root.left) { + var leftRet = binaryTreePaths(root.left, str); + Array.prototype.push.apply(ret, leftRet); + } + if(root.right) { + var rightRet = binaryTreePaths(root.right, str); + Array.prototype.push.apply(ret, rightRet); + } + return ret; +}; +``` + + +### 258. [Add Digits](https://leetcode.com/problems/add-digits/description/) +```javascript +/** + * @param {number} num + * @return {number} + */ +var addDigits = function(num) { + return 1 + (num - 1) % 9; +}; +``` + + +### 263. [Ugly Number](https://leetcode.com/problems/ugly-number/description/) +```javascript +/** + * @param {number} num + * @return {boolean} + */ +var isUgly = function(num) { + if(num <= 0) { + return false; + } + while(num % 2 === 0) {num /= 2;} + while(num % 3 === 0) {num /= 3;} + while(num % 5 === 0) {num /= 5;} + return num === 1 ? true : false; +}; +``` + + +### 268. [Missing Number](https://leetcode.com/problems/missing-number/description/) +```javascript +/** + * @param {number[]} nums + * @return {number} + */ +var missingNumber = function(nums) { + return (0 + nums.length) * (nums.length + 1) / 2 - nums.reduce((p, c) => p + c, 0); +}; +``` + + +### 278. [First Bad Version](https://leetcode.com/problems/first-bad-version/description/) +```javascript +/** + * Definition for isBadVersion() + * + * @param {integer} version number + * @return {boolean} whether the version is bad + * isBadVersion = function(version) { + * ... + * }; + */ + +/** + * @param {function} isBadVersion() + * @return {function} + */ +var solution = function(isBadVersion) { + /** + * @param {integer} n Total versions + * @return {integer} The first bad version + */ + return function(n) { + var start = 1; + var end = n; + var middle; + while (start <= end) { + middle = start + Math.floor((end - start) / 2); + if (middle + 1 <= n && !isBadVersion(middle) && isBadVersion(middle + 1)) { + return middle + 1; + } + if (middle - 1 > 0 && !isBadVersion(middle - 1) && isBadVersion(middle)) { + return middle + } + if (middle === 1 && isBadVersion(middle)) { + return middle; + } + if (isBadVersion(middle)) { + end = middle - 1; + } else { + start = middle + 1; + } + } + return 'null'; + }; +}; +``` + + +### 281. [Zigzag Iterator](https://leetcode.com/problems/zigzag-iterator/description/) +```python +class ZigzagIterator(object): + + def __init__(self, v1, v2): + self.data = [(len(v), iter(v)) for v in (v1, v2) if v] + + def next(self): + len, iter = self.data.pop(0) + if len > 1: + self.data.append((len-1, iter)) + return next(iter) + + def hasNext(self): + return bool(self.data) +``` + + +### 283. [Move Zeroes](https://leetcode.com/problems/move-zeroes/description/) +```javascript +/** + * @param {number[]} nums + * @return {void} Do not return anything, modify nums in-place instead. + */ +var moveZeroes = function(nums) { + var sum = 0; + for(var i = nums.length - 1; i >=0; i--) { + if(nums[i] === 0) { + nums.splice(i,1); + sum++; + } + } + [].push.apply(nums, new Array(sum).fill(0)) +}; +``` + + +### 290. [Word Pattern](https://leetcode.com/problems/word-pattern/description/) +```javascript +/** + * @param {string} pattern + * @param {string} str + * @return {boolean} + */ +var wordPattern = function(pattern, str) { + var s = pattern; + var t = str.split(' '); + if (s.length !== t.length) return false; + + var smap = {}; + var tmap = {}; + + for (var i = 0; i < s.length; i++) { + if (smap[s[i]] === undefined && tmap[t[i]] === undefined) { + smap[s[i]] = t[i]; + tmap[t[i]] = s[i]; + } + + if (smap[s[i]] !== t[i] || tmap[t[i]] !== s[i] ){ + return false; + } + } + + return true; +}; +``` + + +### 292. [Nim Game](https://leetcode.com/problems/nim-game/description/) +```javascript +/** + * @param {number} n + * @return {boolean} + */ +var canWinNim = function(n) { + return !(n !== 0 && n%4 === 0); +}; +``` + + +### 295. [Find Median from Data Stream](https://leetcode.com/problems/find-median-from-data-stream/description/) +```python +import Queue + +class MedianFinder(object): + + def __init__(self): + """ + initialize your data structure here. + """ + self.small = Queue.PriorityQueue() + self.large = Queue.PriorityQueue() + + + def addNum(self, num): + """ + :type num: int + :rtype: void + """ + self.small.put(num) + self.large.put(-self.small.get()) + + if self.small.qsize() < self.large.qsize(): + self.small.put(-self.large.get()) + + + def findMedian(self): + """ + :rtype: float + """ + if self.small.qsize() > self.large.qsize(): + return float(self.small.queue[0]) + else: + return (float(self.small.queue[0] - self.large.queue[0])) / 2 + + + + +# Your MedianFinder object will be instantiated and called as such: +# obj = MedianFinder() +# obj.addNum(num) +# param_2 = obj.findMedian() +``` + + +### 297. [Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/description/) +```python +class Codec: + + def serialize(self, root): + """Encodes a tree to a single string. + + :type root: TreeNode + :rtype: str + """ + if root == None: + return [] + left = self.serialize(root.left) + right = self.serialize(root.right) + + if len(right) == 0 and len(left) == 0: + return [root.val] + elif len(right) == 0: + return [root.val, left]; + else: + return [root.val, left, right]; + + # 1[2,3[4,5]] + def deserialize(self, data): + """Decodes your encoded data to tree. + + :type data: str + :rtype: TreeNode + """ + if data == []: + return + + if data == None: + return + elif isinstance(data, int): + return TreeNode(data) + elif isinstance(data, list): + root = TreeNode(data[0]) + if (len(data) > 1): + root.left = self.deserialize(data[1]) + if (len(data) > 2): + root.right = self.deserialize(data[2]) + return root + +``` + + +### 298. [Binary Tree Longest Consecutive Sequence](https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/description/) +```python +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution(object): + def longestConsecutive(self, root): + """ + :type root: TreeNode + :rtype: int + """ + if root == None: + return 0 + + self.maxLen = 1 + self.find(root, root.val, 1) + return self.maxLen + + def find(self, node, value, len): + if node.left: + if node.left.val == value + 1: + self.maxLen = max(self.maxLen, len + 1) + self.find(node.left, node.left.val, len + 1) + else: + self.find(node.left, node.left.val, 1) + + if node.right: + if node.right.val == value + 1: + self.maxLen = max(self.maxLen, len + 1) + self.find(node.right, node.right.val, len + 1) + else: + self.find(node.right, node.right.val, 1) +``` + + +### 303. [Range Sum Query - Immutable](https://leetcode.com/problems/range-sum-query-immutable/description/) +```javascript +/** + * @param {number[]} nums + */ +var NumArray = function(nums) { + this.dp = []; + this.dp[0] = nums[0]; + for (var i = 1; i < nums.length; i++) { + this.dp[i] = this.dp[i - 1] + nums[i]; + } +}; + +/** + * @param {number} i + * @param {number} j + * @return {number} + */ +NumArray.prototype.sumRange = function(i, j) { + return this.dp[j] - (i === 0 ? 0 : this.dp[i - 1]); +}; + +/** + * Your NumArray object will be instantiated and called as such: + * var obj = Object.create(NumArray).createNew(nums) + * var param_1 = obj.sumRange(i,j) + */ +``` + + +### 307. [Range Sum Query - Mutable](https://leetcode.com/problems/range-sum-query-mutable/description/) +```javascript +/** + * @param {number[]} nums + */ +var NumArray = function(nums) { + this.nums = [0].concat(nums); + this.BIT = [0]; + for (var i = 1; i < this.nums.length; i++) { + this.BIT[i] = 0; + for (var j = i - this.lowestbit(i) + 1; j <= i; j++) { + this.BIT[i] += this.nums[j]; + } + } +}; + +// lowestbit 指将i转为二进制后,最后一个1的位置所代表的数值 +NumArray.prototype.lowestbit = function(v) { + return (v & -v); +}; +/** +* @param {number} i +* @param {number} val +* @return {void} +*/ + +// (1)假如i是左子节点,那么其父节点下标为i+(lowestbit(i)) +// (2)假如i是右子节点,那么其父节点下标为i-(lowestbit(i)) +// 更新BIT[i], 以及包含了num[i]的BIT, 也就是i的右父节点 +NumArray.prototype.update = function(i, val) { + var k = i + 1; + var delta = val - this.nums[k]; + this.nums[k] = val; + while (k < this.nums.length) { + this.BIT[k] += delta; + k = k + this.lowestbit(k); + } +}; + +// 求当前结点,以及它的所有右侧父节点 +NumArray.prototype.sumLeft = function(i) { + var sum = 0; + while (i > 0) { + sum += this.BIT[i]; + i = i - this.lowestbit(i); + } + return sum; +}; +/** +* @param {number} i +* @param {number} j +* @return {number} +*/ +NumArray.prototype.sumRange = function(i, j) { + return this.sumLeft(j + 1) - this.sumLeft(i); +}; + +/** +* Your NumArray object will be instantiated and called as such: +* var obj = Object.create(NumArray).createNew(nums) +* obj.update(i,val) +* var param_2 = obj.sumRange(i,j) +*/ +``` + + +### 312. [Burst Balloons](https://leetcode.com/problems/burst-balloons/description/) +```javascript +/** + * @param {number[]} nums + * @return {number} + */ +function brust(memo, nums, left, right) { + if (memo[left][right] !== undefined) { + return memo[left][right]; + } + + var max = 0; + for (var i = left + 1; i < right; i++) { + max = Math.max(max, + nums[left] * nums[i] * nums[right] + + brust(memo, nums, left, i) + + brust(memo, nums, i, right)); + } + memo[left][right] = max; + + return max; +} + +var maxCoins = function(nums) { + // delete 0, and 1 in front and end + nums = [1].concat(nums.filter(item => { + return item !== 0; + })).concat(1); + + // init the memo + var memo = []; + for (var i = 0; i < nums.length; i++) { + memo.push(new Array(nums.length)); + } + + return brust(memo, nums, 0, nums.length - 1); +}; + +``` + + +### 319. [Bulb Switcher](https://leetcode.com/problems/bulb-switcher/description/) +```javascript +/** + * @param {number} n + * @return {number} + */ +var bulbSwitch = function(n) { + return parseInt(Math.sqrt(n)); +}; +``` + + +### 326. [Power of Three](https://leetcode.com/problems/power-of-three/description/) +```javascript +/** + * @param {number} n + * @return {boolean} + */ +var isPowerOfThree = function(n) { + while(true) { + if(n === 1) { + return true; + } + if(n === 0 || n === 2) { + return false; + } + if(n % 3 !== 0) { + return false; + } else { + n = n / 3; + } + } + return true; +}; +``` + + +### 328. [Odd Even Linked List](https://leetcode.com/problems/odd-even-linked-list/description/) +```javascript +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @return {ListNode} + */ +var oddEvenList = function(head) { + if(head === null) { + return null; + } + var second = head.next; + + var odd = head; + var even = head.next; + while(odd && odd.next && odd.next.next) { + odd.next = even.next; + odd = odd.next; + if(odd) { + even.next = odd.next; + even = even.next; + } + } + odd.next = second; + return head; +}; +``` + + +### 338. [Counting Bits](https://leetcode.com/problems/counting-bits/description/) +```javascript +var countBits = function(num) { + var dp = [0]; + for (var i = 1; i <= num; i++) { + dp[i] = i % 2 === 0 ? dp[i / 2] : dp[(i - 1) / 2] + 1; + } + + return dp +}; +``` + + +### 340. [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/description/) +```javascript +/** + * @param {string} s + * @param {number} k + * @return {number} + */ +var lengthOfLongestSubstringKDistinct = function(s, k) { + var map = {}; + var j = 0; + var maxLen = 0; + var charCount = 0; + var low, high; + + for (var i = 0; i < s.length; i++) { + var char = s[i]; + + if (map[char] === undefined || map[char] === 0) { + map[char] = 1; + charCount++; + } else { + map[char]++; + } + + if (charCount <= k && i - j + 1> maxLen) { + low = j; + high = i; + maxLen = i - j + 1; + } + if (charCount > k) { + while (charCount > k) { + var tail = s[j]; + map[tail]--; + if (map[tail] === 0) { + charCount--; + } + j++; + } + } + } + + // the longest string: s.slice(low, high + 1) + return maxLen; +}; + +``` + + +### 342. [Power of Four](https://leetcode.com/problems/power-of-four/description/) +```javascript +/** + * @param {number} num + * @return {boolean} + + +var isPowerOfFour = function(num) { + return num > 0 && (num & (num - 1)) === 0 && (num & 0x55555555) !== 0; +}; + */ + +var isPowerOfFour = function(num) { + if (num < 1) { + return false; + } else if (num === 1) { + return true; + } else if (num % 4 !== 0) { + return false; + } else { + return isPowerOfFour(num / 4); + } +}; +``` + + +### 343. [Integer Break](https://leetcode.com/problems/integer-break/description/) +```javascript +/** + * @param {number} n + * @return {number} + */ +var integerBreak = function(n) { + var maxArr = { + 1: 1, + 2: 1, + }; + var maxValue = null; + var value = null; + + for(var i = 3; i <= n; i++) { + maxValue = Number.MIN_SAFE_INTEGER; + for(var j = 1; j <= i / 2; j++) { + value = Math.max(j, maxArr[j]) * Math.max(i - j, maxArr[i - j]); + if(value > maxValue) { + maxValue = value; + } + } + maxArr[i] = maxValue; + } + + return maxArr[n]; +}; +``` + + +### 344. [Reverse String](https://leetcode.com/problems/reverse-string/description/) +```javascript +/** + * @param {string} s + * @return {string} + */ +var reverseString = function(s) { + return s.split('').reverse().join(''); +}; +``` + + +### 345. [Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/description/) +```javascript +/** + * @param {string} s + * @return {string} + */ +var reverseVowels = function(s) { + s = s.split(''); + var vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']; + var start = -1; + var end = s.length; + var temp; + + while (start < end) { + while (start < end && vowels.indexOf(s[++start]) === -1); + while (start < end && vowels.indexOf(s[--end]) === -1); + temp = s[start]; + s[start] = s[end]; + s[end] = temp; + } + + return s.join(''); +}; +``` + + +### 346. [Moving Average from Data Stream](https://leetcode.com/problems/moving-average-from-data-stream/description/) +```python +class MovingAverage(object): + + def __init__(self, size): + """ + Initialize your data structure here. + :type size: int + """ + self.size = size + self.queue = [] + self.count = 0 + self.sum = 0.0 + + + def next(self, val): + """ + :type val: int + :rtype: float + """ + self.count += 1 + self.queue.append(val) + self.sum += val + + if self.count > self.size: + self.sum -= self.queue[0] + self.queue.pop(0) + + return self.sum / min(self.size, self.count) + + + +# Your MovingAverage object will be instantiated and called as such: +# obj = MovingAverage(size) +# param_1 = obj.next(val) +``` + + +### 347. [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/description/) +```javascript +/** + * @param {number[]} nums + * @param {number} k + * @return {number[]} + */ + +function SortNumber(size) { + this.size = size; + this.container = []; +} +SortNumber.prototype.setCompare = function(compare) { + this.compare = compare; +} +SortNumber.prototype.getMax = function() { + return this.max; +} +SortNumber.prototype.getMin = function() { + return this.min; +} +SortNumber.prototype.getAll = function() { + return this.container; +} +SortNumber.prototype.setBigNumber = function(num) { + if (this.container.length < this.size) { + this.container.push(num); + } else if (this.compare(this.min, num) < 0) { + this.container.shift(); + this.container.push(num); + } else { + return; + } + + this.container.sort(this.compare); + this.max = this.container[this.container.length - 1]; + this.min = this.container[0]; +} + +var topKFrequent = function(nums, k) { + var topNumber = new SortNumber(k); + topNumber.setCompare((a, b) => a.count - b.count); + var showTimes = {}; + nums.map((num) => { + if(showTimes[num]) { + showTimes[num]++; + } else { + showTimes[num] = 1; + } + }); + for(var num in showTimes) { + topNumber.setBigNumber({ + value: num, + count: showTimes[num], + }); + } + + return topNumber.getAll().map((item) => parseInt(item.value)); +}; +``` + + +### 348. [Design Tic-Tac-Toe](https://leetcode.com/problems/design-tic-tac-toe/description/) +```python +class TicTacToe(object): + + def __init__(self, n): + """ + Initialize your data structure here. + :type n: int + """ + self.row = [0] * n + self.col = [0] * n + self.diagonal = 0 + self.antiDiagonal = 0 + self.size = n + + + def move(self, row, col, player): + """ + Player {player} makes a move at ({row}, {col}). + @param row The row of the board. + @param col The column of the board. + @param player The player, can be either 1 or 2. + @return The current winning condition, can be either: + 0: No one wins. + 1: Player 1 wins. + 2: Player 2 wins. + :type row: int + :type col: int + :type player: int + :rtype: int + """ + weight = 1 if player == 1 else -1 + self.col[col] += weight + self.row[row] += weight + if col == row: + self.diagonal += weight + if col + row == self.size - 1: + self.antiDiagonal += weight + + if self.size in [self.col[col], self.row[row], self.diagonal, self.antiDiagonal]: + return 1 + elif -self.size in [self.col[col], self.row[row], self.diagonal, self.antiDiagonal]: + return 2 + else: + return 0 + + +# Your TicTacToe object will be instantiated and called as such: +# obj = TicTacToe(n) +# param_1 = obj.move(row,col,player) +``` + + +### 349. [Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/description/) +```javascript +/** + * @param {number[]} nums1 + * @param {number[]} nums2 + * @return {number[]} + */ + +var intersection = function (nums1, nums2) { + var ret = []; + for (var i = 0; i < nums1.length; i++) { + for (var j = 0; j < nums2.length; j++) { + if (nums1[i] == nums2[j] && ret.indexOf(nums1[i]) === -1) { + ret.push(nums1[i]); + break; + } + } + } + return ret; +} +``` + + +### 350. [Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/description/) +```javascript +/** + * @param {number[]} nums1 + * @param {number[]} nums2 + * @return {number[]} + */ +var intersect = function(nums1, nums2) { + var map = {}; + var ret = []; + + nums1.forEach(i => map[i] === undefined ? map[i] = 1 : map[i]++); + + nums2.forEach(i => { + if(map[i] !== undefined && map[i] > 0) { + ret.push(i); + map[i]--; + } + }); + + return ret; +}; +``` + + +### 357. [Count Numbers with Unique Digits](https://leetcode.com/problems/count-numbers-with-unique-digits/description/) +```javascript +/** + * @param {number} n + * @return {number} + */ +var countNumbersWithUniqueDigits = function(n) { + var dp = []; + var ret = 0; + + dp[0] = 1; + dp[1] = 9; + for (var i = 2; i <= 10; i++) { + dp[i] = dp[i - 1] * (11 - i); + } + for (var i = 0; i <= Math.min(10, n); i++) { + ret += dp[i]; + } + + return ret; +}; + +``` + + +### 361. [Bomb Enemy](https://leetcode.com/problems/bomb-enemy/description/) +```python +class Solution(object): + def maxKilledEnemies(self, grid): + """ + :type grid: List[List[str]] + :rtype: int + """ + if grid == None or len(grid) == 0 or len(grid[0]) == 0: + return 0 + ret = 0 + colCount = {} + rowCount = 0 + for i in range(len(grid)): + for j in range(len(grid[0])): + if j == 0 or grid[i][j - 1] == 'W': + rowCount = self.getRowCount(grid, i, j) + if i == 0 or grid[i - 1][j] == 'W': + colCount[j] = self.getColCount(grid, i, j) + if grid[i][j] == '0': + ret = max(rowCount + colCount[j], ret) + + return ret + def getRowCount(self, grid, i, j): + count = 0 + while j < len(grid[0]) and grid[i][j] != 'W': + if grid[i][j] == 'E': + count += 1 + j += 1 + return count + def getColCount(self, grid, i, j): + count = 0 + while i < len(grid) and grid[i][j] != 'W': + if grid[i][j] == 'E': + count += 1 + i += 1 + return count + +``` + + +### 362. [Design Hit Counter](https://leetcode.com/problems/design-hit-counter/description/) +```python +class HitCounter(object): + + def __init__(self): + """ + Initialize your data structure here. + """ + self.time = [None] * 300 + self.hits = [0] * 300 + + def hit(self, timestamp): + """ + Record a hit. + @param timestamp - The current timestamp (in seconds granularity). + :type timestamp: int + :rtype: void + """ + index = timestamp % 300 + if self.time[index] != timestamp: + self.time[index] = timestamp + self.hits[index] = 1 + else: + self.hits[index] += 1 + + + def getHits(self, timestamp): + """ + Return the number of hits in the past 5 minutes. + @param timestamp - The current timestamp (in seconds granularity). + :type timestamp: int + :rtype: int + """ + total = 0 + for i in range(300): + if self.time[i] != None and timestamp - self.time[i] < 300: + total += self.hits[i] + + return total +``` + + +### 367. [Valid Perfect Square](https://leetcode.com/problems/valid-perfect-square/description/) +```javascript +/** + * @param {number} num + * @return {boolean} + */ +var isPerfectSquare = function(num) { + if (num < 1) return false; + if (num === 1) return true; + + var start = 0; + var end = num; + var middle = num / 2; + + while (start <= end) { + if (parseInt(num / middle) === middle && num % middle === 0) { + return true; + } else if (num / middle > middle) { + start = middle + 1; + } else if (num / middle < middle) { + end = middle - 1; + } + middle = parseInt((start + end) / 2); + } + + return false; +}; +``` + + +### 371. [Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/description/) +```javascript +/** + * @param {number} a + * @param {number} b + * @return {number} + */ +/* +var getSum = function(a, b) { + if (b === 0) { + return a; + } else { + return getSum(a ^ b, (a & b) << 1 ); + } +}; +*/ + +var getSum = function(a, b) { + var tmp; + + while (b !== 0) { + tmp = a ^ b; + b = (a & b) << 1; + a = tmp; + } + + return a; +}; +``` + + +### 374. [Guess Number Higher or Lower](https://leetcode.com/problems/guess-number-higher-or-lower/description/) +```java +/* The guess API is defined in the parent class GuessGame. + @param num, your guess + @return -1 if my number is lower, 1 if my number is higher, otherwise return 0 + int guess(int num); */ + +public class Solution extends GuessGame { + public int guessNumber(int n) { + int start = 1; + int end = n; + int middle; + + while (start <= end) { + middle = (end - start) / 2 + start; + if (guess(middle) == 0) { + return middle; + } else if (guess(middle) > 0) { + start = middle + 1; + } else { + end = middle - 1; + } + } + return 0; + } +} +``` + + +### 382. [Linked List Random Node](https://leetcode.com/problems/linked-list-random-node/description/) +```javascript +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param head The linked list's head. + Note that the head is guaranteed to be not null, so it contains at least one node. + * @param {ListNode} head + */ +var Solution = function(head) { + this.head = head; +}; + +/** + * Returns a random node's value. + * @return {number} + */ +Solution.prototype.getRandom = function() { + var ret = this.head.val; + var p = this.head.next; + var i = 2; + while (p !== null) { + var random = Math.ceil(Math.random() * i); + if (random === 1) { + ret = p.val; + } + i++; + p = p.next; + } + + return ret; +}; + +/** + * Your Solution object will be instantiated and called as such: + * var obj = Object.create(Solution).createNew(head) + * var param_1 = obj.getRandom() + */ +``` + + +### 383. [Ransom Note](https://leetcode.com/problems/ransom-note/description/) +```javascript +/** + * @param {string} ransomNote + * @param {string} magazine + * @return {boolean} + */ +var canConstruct = function(ransomNote, magazine) { + var map = {}; + var flag = true; + + magazine.split('').forEach(i => map[i] === undefined ? map[i] = 1 : map[i]++); + ransomNote.split('').forEach(i => { + if(map[i] === undefined || map[i] === 0) { + flag = false; + } else { + map[i]--; + } + }); + + return flag; +}; +``` + + +### 384. [Shuffle an Array](https://leetcode.com/problems/shuffle-an-array/description/) +```javascript +/** + * @param {number[]} nums + */ +var Solution = function(nums) { + this.nums = nums; +}; + +/** + * Resets the array to its original configuration and return it. + * @return {number[]} + */ +Solution.prototype.reset = function() { + return this.nums; +}; + +/** + * Returns a random shuffling of the array. + * @return {number[]} + */ + +/* Fisher–Yates shuffle +-- To shuffle an array a of n elements (indices 0..n-1): +for i from n−1 downto 1 do + j ← random integer such that 0 ≤ j ≤ i + exchange a[j] and a[i] +*/ +Solution.prototype.shuffle = function() { + var nums = this.nums.slice(); + var tmp, rand; + for (var i = nums.length - 1; i >= 0; i--) { + rand = Math.floor(Math.random() * (i + 1)); + tmp = nums[i]; + nums[i] = nums[rand]; + nums[rand] = tmp; + } + + return nums; +}; + +/** + * Your Solution object will be instantiated and called as such: + * var obj = Object.create(Solution).createNew(nums) + * var param_1 = obj.reset() + * var param_2 = obj.shuffle() + */ +``` + + +### 387. [First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/description/) +```javascript +/** + * @param {string} s + * @return {number} + */ +var firstUniqChar = function(s) { + var map = {}; + + s.split('').forEach(i => map[i] === undefined ? map[i] = 1 : map[i]++); + + for(var i = 0; i < s.length; i++) { + if(map[s[i]] === 1) { + return i; + } + } + + return -1; + +}; +``` + + +### 389. [Find the Difference](https://leetcode.com/problems/find-the-difference/description/) +```javascript +/** + * @param {string} s + * @param {string} t + * @return {character} + */ +var findTheDifference = function(s, t) { + if(!s) return t; + var sSum = s.split('').map(i => i.charCodeAt(0)).reduce((p, c) => p + c); + var tSum = t.split('').map(i => i.charCodeAt(0)).reduce((p, c) => p + c); + return String.fromCharCode(tSum - sSum); +}; +``` + + +### 392. [Is Subsequence](https://leetcode.com/problems/is-subsequence/description/) +```javascript +/** + * @param {string} s + * @param {string} t + * @return {boolean} + */ +var isSubsequence = function(s, t) { + var j = 0; + + for (var i = 0; i < t.length; i++) { + target = s[j]; + if(t[i] === target) { + j++; + } + if(j === s.length) { + break; + } + } + + if (j === s.length) { + return true; + } else { + return false; + } +}; +``` + + +### 394. [Decode String](https://leetcode.com/problems/decode-string/description/) +```python +class Solution(object): + def decodeString(self, s): + while '[' in s: + s = re.sub(r'(\d+)\[([a-z]*)\]', + lambda m: int(m.group(1)) * m.group(2), s) + return s + +``` + + +### 400. [Nth Digit](https://leetcode.com/problems/nth-digit/description/) +```javascript +/** + * @param {number} n + * @return {number} + */ +var findNthDigit = function(n) { + var length = 1; + var count = 9; + var digits = 9; + + while (n > digits) { + length++; + count *= 10; + digits += length * count; + } + n = n - (digits - length * count); + + var position = Math.ceil(n / length); + var number = Math.pow(10, (length - 1)) + position - 1; + + if (n % length === 0) { + return number % 10; + } else { + return parseInt(String(number)[n % length - 1]); + } +}; + +``` + + +### 401. [Binary Watch](https://leetcode.com/problems/binary-watch/description/) +```javascript +/** + * @param {number} num + * @return {string[]} + */ + +var bitcount1 = function (num) { + return (num).toString(2).split('').filter(i => i === '1').length; +} +var formatTime = function(h, m) { + if (m < 10) { + return `${h}:0${m}` + } else { + return `${h}:${m}` + } +} + +var readBinaryWatch = function(num) { + var ret = []; + + for (var h = 0; h < 12; h++) { + for (var m = 0; m < 60; m++) { + if(bitcount1(h << 6 | m) === num) { + ret.push(formatTime(h, m)); + } + } + } + + return ret; +}; +``` + + +### 404. [Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/description/) +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number} + */ +var sumOfLeftLeaves = function(root) { + if(root === null) return 0; + + if(root.left !== null && root.left.left === null && root.left.right === null) { + return root.left.val + sumOfLeftLeaves(root.right); + } else { + return sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right); + } +}; +``` + + +### 406. [Queue Reconstruction by Height](https://leetcode.com/problems/queue-reconstruction-by-height/description/) +```javascript +/** + * @param {number[][]} people + * @return {number[][]} + */ +var reconstructQueue = function(people) { + if (people.length <= 1) return people; + people.sort((a, b) => { + if (a[0] !== b[0]) { + return b[0] - a[0]; + } else { + return a[1] - b[1]; + } + }); + + var ret = []; + ret.push(people[0]); + + for (var i = 1; i < people.length; i++) { + if (people[i][0] === people[0][0]) { + ret.push(people[i]); + continue; + } + + var count = 0, index = 0; + while (count < people[i][1]) { + if (ret[index][0] >= people[i][0]) count++; + index++; + } + ret.splice(index, 0, people[i]); + } + + return ret; +}; +``` + + +### 409. [Longest Palindrome](https://leetcode.com/problems/longest-palindrome/description/) +```javascript +/** + * @param {string} s + * @return {number} + */ +var longestPalindrome = function(s) { + var map = {}; + var number = 0; + var hasOdd = false; + + s.split('').forEach(i => map[i] === undefined ? map[i] = 1 : map[i]++); + + for(var i in map) { + if(map[i] % 2 === 0) { + number += map[i]; + } else if(map[i] > 2) { + number += map[i] - 1; + hasOdd = true; + } else { + hasOdd = true; + } + } + + if(hasOdd) number++; + + return number; +}; +``` + + +### 412. [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/description/) +```javascript +/** + * @param {number} n + * @return {string[]} + */ +var fizzBuzz = function(n) { + var ret = []; + + for (var i = 1; i <= n; i++) { + if (i % 15 === 0) { + ret.push('FizzBuzz'); + } else if (i % 3 === 0) { + ret.push('Fizz'); + } else if (i % 5 === 0) { + ret.push('Buzz'); + } else { + ret.push(i + ''); + } + } + + return ret; +}; +``` + + +### 413. [Arithmetic Slices](https://leetcode.com/problems/arithmetic-slices/description/) +```javascript +/** + * @param {number[]} A + * @return {number} + */ +var count = function(len) { + return len < 3 ? 0 : (1+ len - 2) * (len - 2) / 2; +}; + +var numberOfArithmeticSlices = function(A) { + if (A.length < 3) return 0; + var len = 2; + var diff = A[1] - A[0]; + var ret = 0; + + for (var i = 1; i < A.length - 1; i++) { + if (diff === A[i + 1] - A[i]) { + len++ + } else { + ret += count(len); + diff = A[i + 1] - A[i]; + len = 2; + } + } + ret += count(len); + + return ret; +}; +``` + + +### 414. [Third Maximum Number](https://leetcode.com/problems/third-maximum-number/description/) +```javascript +/** + * @param {number[]} nums + * @return {number} + */ +var getMax = function(nums) { + return Math.max.apply(null, nums); +}; + +var removeItem = function(nums, item) { + return nums.filter((value) => value !== item); +}; + +var thirdMax = function(nums) { + var first = getMax(nums); + nums = removeItem(nums, first); + var second = getMax(nums); + nums = removeItem(nums, second); + + if (nums.length === 0) return first; + return getMax(nums); + +}; + +``` + + +### 415. [Add Strings](https://leetcode.com/problems/add-strings/description/) +```javascript +/** + * @param {string} num1 + * @param {string} num2 + * @return {string} + */ +var addStrings = function(num1, num2) { + var zeroString = Array(Math.abs(num1.length - num2.length) + 1).join(0); + if (num1.length > num2.length) { + num2 = zeroString + num2; + } else { + num1 = zeroString + num1; + } + + var d1 = num1.split(''); + var d2 = num2.split(''); + var ret = []; + var sum ; + var hasCarryOver = false; + + for(var i = d1.length - 1; i >= 0 ; i--) { + sum = parseInt(d1[i]) + parseInt(d2[i]); + if (hasCarryOver) { + sum++; + } + if(sum >= 10) { + sum -= 10; + hasCarryOver = true; + } else { + hasCarryOver = false; + } + ret.unshift(sum); + } + + if(hasCarryOver) { + ret.unshift(1); + } + + return ret.join(''); +}; + +``` + + +### 419. [Battleships in a Board](https://leetcode.com/problems/battleships-in-a-board/description/) +```javascript +/** + * @param {character[][]} board + * @return {number} + */ +var countBattleships = function(board) { + var m = board.length; + if (m === 0) return 0; + var n = board[0].length; + + var count = 0; + + for (var i = 0; i < m; i++) { + for (var j = 0; j < n; j++) { + if (board[i][j] === '.') continue; + if (i > 0 && board[i - 1][j] == 'X') continue; + if (j > 0 && board[i][j - 1] == 'X') continue; + count++; + } + } + + return count; +}; +``` + + +### 421. [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/description/) +```javascript +/** + * @param {number[]} nums + * @return {number} + */ +var findMaximumXOR = function(nums) { + var max = 0; + var mask = 0; + for (var i = 31; i >= 0; i--) { + var set = {}; + mask = mask | (1 << i); + nums.forEach((num) => set[num & mask] = true); + + var candidate = max | (1 << i); + + for (var prefix in set) { + if (set[prefix ^ candidate]) { + max = candidate; + } + } + } + + return max; +}; + +``` + + +### 432. [All O`one Data Structure](https://leetcode.com/problems/all-oone-data-structure/description/) +```python +from collections import defaultdict + +class Node(object): + def __init__(self, frequence): + self.prev = self.next = None + self.set = set([]) + self.frequence = frequence + def add(self, key): + self.set.add(key) + def remove(self, key): + self.set.remove(key) + def count(self): + return len(self.set) + def is_empty(self): + return self.count() == 0 + def get_key(self): + if self.is_empty(): + return None + else: + return list(self.set)[0] + +class DoubleLinkList(object): + def __init__(self, Node): + self.Node = Node + self.head = Node('head') + self.tail = Node('tail') + self.head.next = self.tail + self.tail.prev = self.head + + def insert_after(self, node, frequence): + newNode = self.Node(frequence) + nextNode = node.next + + node.next = newNode + newNode.next = nextNode + nextNode.prev = newNode + newNode.prev = node + + return newNode + + def insert_before(self, node, frequence): + return self.insert_after(node.prev, frequence) + + def remove(self, node): + prevNode = node.prev + prevNode.next = node.next + node.next.prev = prevNode + return + + def get_head(self): + return self.head + def get_tail(self): + return self.tail + +class AllOne(object): + + def __init__(self): + self.node_list = DoubleLinkList(Node) + self.key_counter_dict = defaultdict(int) + self.freq_node_dict = {0: self.node_list.get_head()} + + # firstly, remove key from node + # secondly, if the node is empty, remove the node from node_list and remove the key from freq_node_dict + def remove_key(self, key, frquence): + node = self.freq_node_dict[frquence] + node.remove(key) + if node.count() == 0: + self.node_list.remove(node) + self.freq_node_dict.pop(frquence) + + + # cf means current frequence + # pf means last state of frequence + def inc(self, key): + self.key_counter_dict[key] += 1 + cf = self.key_counter_dict[key] + pf = self.key_counter_dict[key] - 1 + + # add key to the new node + if not cf in self.freq_node_dict: + node = self.node_list.insert_after(self.freq_node_dict[pf], cf) + self.freq_node_dict[cf] = node + self.freq_node_dict[cf].add(key) + + # delete key from the old node + if pf > 0: + self.remove_key(key, pf) + + def dec(self, key): + if key in self.key_counter_dict: + self.key_counter_dict[key] -= 1 + cf = self.key_counter_dict[key] + pf = self.key_counter_dict[key] + 1 + + # add key to the new node + if not cf in self.freq_node_dict: + node = self.node_list.insert_before(self.freq_node_dict[pf], cf) + self.freq_node_dict[cf] = node + self.freq_node_dict[cf].add(key) + + # delete key from the old node + self.remove_key(key, pf) + + def getMinKey(self): + if self.node_list.get_head().next != self.node_list.get_tail(): + return self.node_list.get_head().next.get_key() + else: + return '' + + + def getMaxKey(self): + if self.node_list.get_tail().prev != self.node_list.get_head(): + return self.node_list.get_tail().prev.get_key() + else: + return '' + +``` + + +### 434. [Number of Segments in a String](https://leetcode.com/problems/number-of-segments-in-a-string/description/) +```javascript +/** + * @param {string} s + * @return {number} + */ +/* +var countSegments = function(s) { + return s.split(' ').filter((item) => item !== '').length; +}; +*/ + +var countSegments = function(s) { + var count = 0; + for (var i = 0; i < s.length; i++) { + if (s[i] !== ' ' && (i === 0 || s[i - 1] === ' ')) count++; + } + + return count; +}; +``` + + +### 437. [Path Sum III](https://leetcode.com/problems/path-sum-iii/description/) +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @param {number} sum + * @return {number} + */ +var traversal = function(root, list, sum, cb) { + if (root === null) return; + cb(list, root.val); + var newList = list.map((item) => item - root.val).concat(sum); + + traversal(root.left, newList, sum, cb); + traversal(root.right, newList, sum, cb); +}; +var pathSum = function(root, sum) { + var count = 0; + var cb = function (list, val) { + list.forEach((item) => { + if (item === val) { + count++; + } + }); + } + + traversal(root, [sum], sum, cb); + return count; +}; + +``` + + +### 441. [Arranging Coins](https://leetcode.com/problems/arranging-coins/description/) +```javascript +/** + * @param {number} n + * @return {number} + */ +var arrangeCoins = function(n) { + if (n === 0) {return 0;} + var start = 0; + var end = n; + var middle = parseInt((start + end + 1) / 2); + + while (start <= end) { + if ((1 + middle) * middle / 2 <= n && n < (2 + middle) * (middle + 1) / 2) { + return middle; + } else if ((2 + middle) * (middle + 1) / 2 <= n) { + start = middle + 1; + } else if ((1 + middle) * middle / 2 > n) { + end = middle - 1; + } + middle = parseInt((start + end) / 2); + } +}; + +``` + + +### 442. [Find All Duplicates in an Array](https://leetcode.com/problems/find-all-duplicates-in-an-array/description/) +```javascript +/** + * @param {number[]} nums + * @return {number[]} + */ +var findDuplicates = function(nums) { + var ret = []; + for (var i = 0; i < nums.length; i++) { + var index = Math.abs(nums[i]) - 1; + if (nums[index] < 0) { + ret.push(Math.abs(nums[i])); + } + nums[index] = -Math.abs(nums[index]); + } + + return ret; +}; +``` + + +### 445. [Add Two Numbers II](https://leetcode.com/problems/add-two-numbers-ii/description/) +```javascript +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} l1 + * @param {ListNode} l2 + * @return {ListNode} + */ + +var getLengthList = function(head) { + var len = 0; + while (head !== null) { + len++; + head = head.next; + } + return len; +}; + +var reverseList = function(head) { + if (head === null || head.next === null) return head; + + var prev = head; + var p = head.next; + var tmp; + + head.next = null; + while (p !== null) { + tmp = p.next; + p.next = prev; + prev = p; + p = tmp; + } + + return prev; +}; +var addTwoNumbers = function(l1, l2) { + var len1 = getLengthList(l1); + var len2 = getLengthList(l2); + + var tmp; + if (len1 >= len2) { + l1 = reverseList(l1); + l2 = reverseList(l2); + } else { + tmp = l2; + l2 = reverseList(l1); + l1 = reverseList(tmp); + } + + var head = l1; + var val1, val2; + var isOverTen = false; + var prev; + while (l1 !== null) { + val1 = l1.val; + val2 = l2 !== null ? l2.val : 0; + + if (isOverTen) { + val1++; + isOverTen = false; + } + if (val1 + val2 >= 10) { + l1.val = val1 + val2 - 10; + isOverTen = true; + } else { + l1.val = val1 + val2; + } + + prev = l1; + l1 = l1.next; + if (l2 !== null) { + l2 = l2.next; + } + } + + if (isOverTen) { + prev.next = new ListNode(1); + } + + return reverseList(head); +}; + +``` + + +### 448. [Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/) +```javascript +/** + * @param {number[]} nums + * @return {number[]} + */ +var findDisappearedNumbers = function(nums) { + var map = {}; + var ret = []; + + nums.forEach(function(num) { + map[num] = true; + }); + + for (var i = 1; i <= nums.length; i++) { + if (map[i] === undefined) { + ret.push(i); + } + } + + return ret; +}; +``` + + +### 450. [Delete Node in a BST](https://leetcode.com/problems/delete-node-in-a-bst/description/) +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @param {number} key + * @return {TreeNode} + */ +var update = function(parent, direction) { + if (parent === null) { + return; + } + var deleteNode = parent[direction]; + if (deleteNode.right) { + parent[direction] = deleteNode.right; + + var leftIsNullNode = deleteNode.right; + while(leftIsNullNode.left !== null) leftIsNullNode = leftIsNullNode.left; + leftIsNullNode.left = deleteNode.left; + return parent; + } + if(deleteNode.left) { + parent[direction] = deleteNode.left; + return parent; + } + parent[direction] = null; + return parent; +}; + +var search = function(root, direction, key, ret) { + var node = root[direction]; + if (node === null) { + return; + } + + if (node.val === key) { + ret.parent = root; + ret.direction = direction; + } else if (node.val > key) { + search(node, 'left', key, ret); + } else { + search(node, 'right', key, ret); + } +}; + +var deleteNode = function(root, key) { + if (root === null) { + return null; + } + + if (root.val === key) { + if (root.right) { + var leftIsNullNode = root.right; + while(leftIsNullNode.left !== null) leftIsNullNode = leftIsNullNode.left; + + leftIsNullNode.left = root.left; + return root.right; + } + return root.left; + } + + var ret = { + parent: null, + direction: null, + }; + search(root, 'left', key, ret); + search(root, 'right', key, ret); + update(ret.parent, ret.direction); + + return root; +}; + +``` + + +### 451. [Sort Characters By Frequency](https://leetcode.com/problems/sort-characters-by-frequency/description/) +```javascript +/** +* @param {string} s +* @return {string} +*/ +var frequencySort = function(s) { + var charCountMap = {}; + + for (var i = 0; i < s.length; i++) { + if (charCountMap[s[i]] === undefined) { + charCountMap[s[i]] = 1; + } else { + charCountMap[s[i]]++; + } + } + + var list = []; + + for (var key in charCountMap) { + list.push({ + char: key, + count: charCountMap[key] + }); + } + + list.sort((a, b) => b.count- a.count); + + return list.reduce((p, c) => { + return p + c.char.repeat(c.count) + }, ''); +}; + +``` + + +### 453. [Minimum Moves to Equal Array Elements](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/description/) +```javascript +/** + * @param {number[]} nums + * @return {number} + */ +var minMoves = function(nums) { + var min = Math.min.apply(null, nums) + + return nums.reduce((p, c) => p + c - min, 0); +}; +``` + + +### 454. [4Sum II](https://leetcode.com/problems/4sum-ii/description/) +```javascript +/** + * @param {number[]} A + * @param {number[]} B + * @param {number[]} C + * @param {number[]} D + * @return {number} + */ +var fourSumCount = function(A, B, C, D) { + var AB = []; + var CD = []; + for (var i = 0; i < A.length; i++) { + for (var j = 0; j < B.length; j++) { + AB.push(A[i] + B[j]); + CD.push(C[i] + D[j]); + } + } + + AB = AB.sort((a, b) => a - b); + CD = CD.sort((a, b) => a - b); + + var count = 0; + var m = 1, n = 1; + + i = 0; j = CD.length - 1; + while (i < AB.length && j >= 0) { + if (AB[i] + CD[j] > 0) { + j--; + continue; + } + if (AB[i] + CD[j] < 0) { + i++; + continue; + } + while (i < AB.length && AB[i] === AB[i + 1]) { + i++; m++; + } + + while (j >= 0 && CD[j] === CD[j - 1]) { + j--; n++; + } + + count += m * n; + i++; j--; + m = 1; n = 1; + } + + return count; +}; + +``` + + +### 455. [Assign Cookies](https://leetcode.com/problems/assign-cookies/description/) +```javascript +/** + * @param {number[]} g + * @param {number[]} s + * @return {number} + */ +var findContentChildren = function(g, s) { + var compare = (a, b) => a - b; + g = g.sort(compare); + s = s.sort(compare); + + var i = s.length - 1; // person index + var j = g.length - 1; // cookie index + var count = 0; + + while (i >= 0 && j >= 0) { + if(s[i] >= g[j]) { + i--; + j--; + count++ + } else { + j--; + } + } + + return count; +}; +``` + + +### 459. [Repeated Substring Pattern](https://leetcode.com/problems/repeated-substring-pattern/description/) +```javascript +/** + * @param {string} s + * @return {boolean} + */ +/* +var repeatedSubstringPattern = function(s) { + if (s.length === 1) return false; + var ret = false; + + for (var i = 1; i <= parseInt((s.length + 1) / 2); i++) { + for (var j = i; j < s.length; j++) { + if (s[j % i] !== s[j]) break; + } + if (j === s.length && (j - 1) % i === i - 1) { + ret = true; + break; + } + } + + return ret; +}; +*/ + +var repeatedSubstringPattern = function(s) { + return /^(.+)\1+$/.test(s); +} +``` + + +### 460. [LFU Cache](https://leetcode.com/problems/lfu-cache/description/) +```python +from collections import defaultdict +import collections + +class Node(object): + def __init__(self, frequence): + self.prev = self.next = None + self.dict = collections.OrderedDict() + self.frequence = frequence + def add(self, key): + self.dict[key] = True + def pop(self): + if not self.is_empty(): + return self.dict.popitem(last = False) + def remove(self, key): + del self.dict[key] + def count(self): + return len(self.dict) + def is_empty(self): + return self.count() == 0 + +class DoubleLinkList(object): + def __init__(self, Node): + self.Node = Node + self.head = Node('head') + self.tail = Node('tail') + self.head.next = self.tail + self.tail.prev = self.head + def insert_after(self, node, frequence): + newNode = self.Node(frequence) + nextNode = node.next + + node.next = newNode + newNode.next = nextNode + nextNode.prev = newNode + newNode.prev = node + return newNode + def insert_before(self, node, frequence): + return self.insert_after(node.prev, frequence) + def remove(self, node): + prevNode = node.prev + nextNode = node.next + prevNode.next = nextNode + nextNode.prev = prevNode + return + def get_head(self): + return self.head + def get_tail(self): + return self.tail + +class LFUCache(object): + + def __init__(self, capacity): + self.node_list = DoubleLinkList(Node) + self.key_value_dict = {} + self.key_freq_dict = defaultdict(int) + self.freq_node_dict = {0: self.node_list.get_head()} + self.capacity = self.remain = capacity + + # firstly, remove key from node or remove the laste key in lowest frequence node + # secondly, if the node is empty, remove the node from node_list and remove the key from freq_node_dict + def remove_key(self, node, key = None): + frequence = node.frequence + ret = None + if key == None: + ret = node.pop() + else: + node.remove(key) + if node.count() == 0: + self.node_list.remove(node) + self.freq_node_dict.pop(frequence) + + if ret: + delete_key = ret[0] + return delete_key + + # cf means current frequence + # pf means previous frequence + # remove from previous frequence node + # add into the current frequence node + def _update_node(self, key): + self.key_freq_dict[key] += 1 + cf = self.key_freq_dict[key] + pf = cf - 1 + + if not cf in self.freq_node_dict: + node = self.node_list.insert_after(self.freq_node_dict[pf], cf) + node.add(key) + self.freq_node_dict[cf] = node + self.freq_node_dict[cf].add(key) + + if pf > 0: + self.remove_key(self.freq_node_dict[pf], key) + + def get(self, key): + if not key in self.key_value_dict: + return -1 + + ret = self.key_value_dict[key] + self._update_node(key) + return ret + + def put(self, key, value): + if self.capacity == 0: + return + if not key in self.key_value_dict: + self.remain -= 1 + + if self.remain < 0: + deleted_key = self.remove_key(self.node_list.get_head().next) + self.remain += 1 + del self.key_freq_dict[deleted_key] + del self.key_value_dict[deleted_key] + + self.key_value_dict[key] = value + self._update_node(key) + +``` + + +### 461. [Hamming Distance](https://leetcode.com/problems/hamming-distance/description/) +```javascript +/** + * @param {number} x + * @param {number} y + * @return {number} + */ + +// Number of 1 Bits +var hammingWeight = function(n) { + var ret = 0; + for(var power = 32; power >= 0; power--) { + var exponent = Math.pow(2, power); + if (n >= exponent) { + ret++; + n -= exponent; + } + } + return ret; +}; + +var hammingDistance = function(x, y) { + return hammingWeight(x ^ y); +}; +``` + + +### 462. [Minimum Moves to Equal Array Elements II](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/description/) +```javascript +/** + * @param {number[]} nums + * @return {number} + */ +var minMoves2 = function(nums) { + nums = nums.sort((a, b) => a - b); + var median = nums[Math.floor((nums.length - 1) / 2)]; + + return nums.reduce((p, c) => { + return p + Math.abs(c - median); + }, 0); +}; + +``` + + +### 463. [Island Perimeter](https://leetcode.com/problems/island-perimeter/description/) +```javascript +/** + * @param {number[][]} grid + * @return {number} + */ +var islandPerimeter = function(grid) { + var perimeter = 4 * grid.reduce((p, c) => p.concat(c)).reduce((p, c) => p + c); + + for(var i = 0; i < grid.length; i++) { + for(var j = 0; j < grid[i].length; j++) { + if(grid[i][j] === 1) { + if(i + 1 < grid.length && grid[i+1][j] === 1) { + perimeter -= 2 ; + } + if(j + 1 < grid[i].length && grid[i][j+1] === 1) { + perimeter -= 2; + } + } + } + } + + return perimeter; +}; +``` + + +### 476. [Number Complement](https://leetcode.com/problems/number-complement/description/) +```javascript +/** + * @param {number} num + * @return {number} + +这道题给了我们一个数,让我们求补数。通过分析题目汇总的例子,我们知道需要做的就是每个位翻转一下就行了,但是翻转的起始位置上从最高位的1开始的,前面的0是不能被翻转的,所以我们从高往低遍历,如果遇到第一个1了后,我们的flag就赋值为true,然后就可以进行翻转了,翻转的方法就是对应位异或一个1即可,参见代码如下: + + + +解法一: + +复制代码 +class Solution { +public: + int findComplement(int num) { + bool start = false; + for (int i = 31; i >= 0; --i) { + if (num & (1 << i)) start = true; + if (start) num ^= (1 << i); + } + return num; + } +}; +复制代码 + + +由于位操作里面的取反符号~本身就可以翻转位,但是如果直接对num取反的话就是每一位都翻转了,而最高位1之前的0是不能翻转的,所以我们只要用一个mask来标记最高位1前面的所有0的位置,然后对mask取反后,与上对num取反的结果即可,参见代码如下: + + + +解法二: + +复制代码 +class Solution { +public: + int findComplement(int num) { + int mask = INT_MAX; + while (mask & num) mask <<= 1; + return ~mask & ~num; + } +}; +复制代码 + + +再来看一种迭代的写法,一行搞定碉堡了,思路就是每次都右移一位,并根据最低位的值先进行翻转,如果当前值小于等于1了,就不用再调用递归函数了,参见代码如下: + + + +解法三: + +class Solution { +public: + int findComplement(int num) { + return (1 - num % 2) + 2 * (num <= 1 ? 0 : findComplement(num / 2)); + } +}; + + */ + +var findComplement = function(num) { + var mask = ~0; + while (mask & num) mask <<= 1; + return ~mask & ~num; +}; +``` + + +### 482. [License Key Formatting](https://leetcode.com/problems/license-key-formatting/description/) +```javascript +/** + * @param {string} S + * @param {number} K + * @return {string} + */ +var format = function(chars, length, separator) { + if (chars.length <= length) { + return chars.join(''); + } + return format(chars.slice(0, chars.length - length), length, separator) + separator + chars.slice(-length).join(''); +}; +var licenseKeyFormatting = function(S, K) { + var chars = S.split('').filter((char) => { + return char !== '-'; + }); + + return format(chars, K, '-').toUpperCase(); + +}; +``` + + +### 485. [Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/description/) +```javascript +/** + * @param {number[]} nums + * @return {number} + */ +var findMaxConsecutiveOnes = function(nums) { + var max = 0; + var length = 0; + nums.forEach(function(num) { + if (num === 1) length++; + if (num === 0) { + if (length > max) max = length; + length = 0; + } + }); + if (length > max) max = length; + return max; +}; +``` + + +### 486. [Predict the Winner](https://leetcode.com/problems/predict-the-winner/description/) +```javascript +/** + * @param {number[]} nums + * @return {boolean} + */ +var PredictTheWinner = function(nums) { + var n = nums.length; + var dp = new Array(n).fill(undefined).map(() => []); + + nums.forEach((num, i) => dp[i][i] = num); + + for (var len = 1; len < n; len++) { + for (var i = 0; i < n - len; i++) { + var j = i + len; + dp[i][j] = Math.max(nums[i] - dp[i + 1][j], nums[j] - dp[i][j - 1]); + } + } + + return dp[0][n - 1] >= 0; + +}; + +``` + + +### 492. [Construct the Rectangle](https://leetcode.com/problems/construct-the-rectangle/description/) +```javascript +/** + * @param {number} area + * @return {number[]} + */ +var constructRectangle = function(area) { + var root = parseInt(Math.sqrt(area)); + var ret = []; + for (var i = root; i <= area; i++) { + if (area % i === 0) { + i > area / i ? ret.push(i, area / i) : ret.push(area / i, i); + break; + } + } + return ret; +}; +``` + + +### 494. [Target Sum](https://leetcode.com/problems/target-sum/description/) +```javascript + +var findTargetSumWays = function(nums, S) { + var count = 0; + var len = nums.length; + function find(index, sum) { + if (index === len) { + if (sum === S) count++; + } else { + find(index + 1, sum + nums[index]); + find(index + 1, sum - nums[index]); + } + } + find(0, 0); + + return count; +}; +``` + + +### 495. [Teemo Attacking](https://leetcode.com/problems/teemo-attacking/description/) +```javascript +/** + * @param {number[]} timeSeries + * @param {number} duration + * @return {number} + */ +var findPoisonedDuration = function(timeSeries, duration) { + if (timeSeries.length === 0) return 0; + + var time = 0; + for (var i = 0; i < timeSeries.length - 1; i++) { + if (timeSeries[i + 1] - timeSeries[i] < duration) { + time += timeSeries[i + 1] - timeSeries[i]; + } else { + time += duration; + } + } + time += duration; + + return time; +}; +``` + + +### 496. [Next Greater Element I](https://leetcode.com/problems/next-greater-element-i/description/) +```javascript +/** + * @param {number[]} findNums + * @param {number[]} nums + * @return {number[]} + */ +var nextGreaterElement = function(findNums, nums) { + var map = {}; + var stack = []; + nums.forEach(function(num) { + while(stack.length > 0 && stack[stack.length - 1] < num) { + map[stack.pop()] = num; + } + stack.push(num); + }); + + return findNums.map(function(num) { + return map[num] || -1; + }); +}; +``` + + +### 498. [Diagonal Traverse](https://leetcode.com/problems/diagonal-traverse/description/) +```javascript +/** + * @param {number[][]} matrix + * @return {number[]} + */ +function toggleDirection (direction) { + return direction === 'up' ? 'down' : 'up'; +} +var findDiagonalOrder = function(matrix) { + if (matrix.length === 0) return []; + var m = matrix.length; + var n = matrix[0].length; + var next = { + up: [-1, 1], + down: [1, -1] + }; + var direction = 'up'; + var x = 0, y = 0; + var ret = []; + + for (var i = 0; i < m * n; i++) { + ret.push(matrix[x][y]); + x += next[direction][0]; + y += next[direction][1]; + + if (x >= m) { + y += 2; + x -= 1; + direction = toggleDirection(direction); + } + if (y >= n) { + x += 2; + y -= 1; + direction = toggleDirection(direction); + } + if (x < 0) { + x = 0; + direction = toggleDirection(direction); + } + if (y < 0) { + y = 0; + direction = toggleDirection(direction); + } + } + + return ret; +}; + +``` + + +### 500. [Keyboard Row](https://leetcode.com/problems/keyboard-row/description/) +```javascript +/** + * @param {string[]} words + * @return {string[]} + */ +var findWords = function(words) { + return words.filter(function(str) { + return /^([qwertyuiop]*|[asdfghjkl]*|[zxcvbnm]*)$/.test(str.toLowerCase()); + }); +}; +``` + + +### 501. [Find Mode in Binary Search Tree](https://leetcode.com/problems/find-mode-in-binary-search-tree/description/) +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number[]} + */ + +var traversal = function(root, func) { + if (root === null) { + return null; + } + traversal(root.left, func); + func(root.val); + traversal(root.right, func); +}; + +var findMode = function(root) { + if (root === null) return []; + + var ret; + var maxCount = Number.MIN_VALUE; + var prevValue; + var currentCount; + + traversal(root, function(val) { + if (prevValue === undefined) { + prevValue = val; + currentCount = 1; + } else { + if (prevValue === val) { + currentCount++; + } else { + currentCount = 1; + } + } + + if(currentCount > maxCount) { + ret = []; + ret.push(val); + maxCount = currentCount; + } else if (currentCount === maxCount) { + ret.push(val); + } + prevValue = val; + }); + + return ret; +}; +``` + + +### 503. [Next Greater Element II](https://leetcode.com/problems/next-greater-element-ii/description/) +```javascript +/** + * @param {number[]} nums + * @return {number[]} + */ +var nextGreaterElements = function(nums) { + var nextGreaterMap = {}; + var stack = []; + nums.concat(nums).forEach((item) => { + while (stack.length > 0 && stack[stack.length - 1] < item) { + var key = stack.pop(); + if (nextGreaterMap[key] === undefined) { + nextGreaterMap[key] = []; + } + nextGreaterMap[key].push(item); + } + stack.push(item); + }); + + return nums.map((item) => { + if (nextGreaterMap[item] && nextGreaterMap[item].length > 0) { + return nextGreaterMap[item].shift(); + } else { + return -1; + } + }); +}; + +``` + + +### 504. [Base 7](https://leetcode.com/problems/base-7/description/) +```javascript +/** + * @param {number} num + * @return {string} + */ +var convertToBase7 = function(num) { + if (num < 0) return '-' + convertToBase7(-num); + else if (num < 7) return String(num); + else return convertToBase7(parseInt(num / 7)) + num % 7; +}; +``` + + +### 506. [Relative Ranks](https://leetcode.com/problems/relative-ranks/description/) +```javascript +/** + * @param {number[]} nums + * @return {string[]} + */ +var findRelativeRanks = function(nums) { + var rank = 4; + var map = {}; + var sortNums = [].slice.call(nums).sort((a, b) => b - a); + for (var i = 0; i < sortNums.length; i++) { + if (i === 0) { + map[sortNums[i]] = 'Gold Medal'; + } else if (i === 1) { + map[sortNums[i]] = 'Silver Medal'; + } else if (i === 2) { + map[sortNums[i]] = 'Bronze Medal'; + } else { + map[sortNums[i]] = String(rank); + rank++; + } + } + + return nums.map((score) => { + return map[score]; + }); +}; +``` + + +### 508. [Most Frequent Subtree Sum](https://leetcode.com/problems/most-frequent-subtree-sum/description/) +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number[]} + */ +function getSum (root, map) { + if (root === null) return 0; + var left = getSum(root.left, map); + var right = getSum(root.right, map); + var sum = root.val + left + right; + + map[sum] === undefined ? map[sum] = 1 : map[sum]++; + return sum; +} +var findFrequentTreeSum = function(root) { + if (root === null) return []; + var valueCountMap = {}; + var max = -1; + var ret = []; + var key; + + getSum(root, valueCountMap); + for (key in valueCountMap) { + if (valueCountMap[key] > max) { + max = valueCountMap[key]; + } + } + + for (key in valueCountMap) { + if (valueCountMap[key] === max) { + ret.push(parseInt(key)); + } + } + + return ret; +}; +``` + + +### 513. [Find Bottom Left Tree Value](https://leetcode.com/problems/find-bottom-left-tree-value/description/) +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number} + */ + +var findBottomLeftValue = function(root) { + var maxDepth = Number.MIN_SAFE_INTEGER; + var value; + + var traverse = function(root, depth) { + if (root === null) return; + if (depth > maxDepth) { + value = root.val; + maxDepth = depth; + } + traverse(root.left, depth + 1); + traverse(root.right, depth + 1); + }; + + traverse(root, 1); + return value; +}; + +``` + + +### 515. [Find Largest Value in Each Tree Row](https://leetcode.com/problems/find-largest-value-in-each-tree-row/description/) +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number[]} + */ +var largestValues = function(root) { + if (root === null) return []; + var left = largestValues(root.left); + var right = largestValues(root.right); + + var ret = []; + for(var i = 0; i < left.length || i < right.length; i++) { + if (left[i] === undefined) { + ret.push(right[i]); + } else if (right[i] === undefined) { + ret.push(left[i]); + } else { + ret.push(Math.max(right[i], left[i])); + } + } + ret.unshift(root.val); + + return ret; +}; +``` + + +### 520. [Detect Capital](https://leetcode.com/problems/detect-capital/description/) +```javascript +/** + * @param {string} word + * @return {boolean} + */ +var detectCapitalUse = function(word) { + return /^[A-Z]?([a-z]*|[A-Z]*)$/.test(word); +}; +``` + + +### 522. [Longest Uncommon Subsequence II](https://leetcode.com/problems/longest-uncommon-subsequence-ii/description/) +```javascript +function isSubStr(str, sub) { + var i = 0; + var j = 0; + while (i < str.length && j < sub.length) { + if (sub[j] === str[i]) { + j++; i++; + } else { + i++; + } + } + return j === sub.length; +} + +var findLUSlength = function(strs) { + var maxLen = strs[0].length; + var notUniqStrs = []; + var map = {}; + + for (var i = 0; i < strs.length; i++) { + if (map[strs[i]] === true) { + notUniqStrs.push(strs[i]); + } else { + map[strs[i]] = true; + } + } + + notUniqStrs.forEach((str) => { + strs = strs.filter((sub) => { + return !isSubStr(str, sub); + }); + }); + + return strs.length === 0 ? -1 : Math.max.apply(null, strs.map(i => i.length)); +} + +``` + + +### 526. [Beautiful Arrangement](https://leetcode.com/problems/beautiful-arrangement/description/) +```javascript +/** + * @param {number} N + * @return {number} + */ +var countArrangement = function(N) { + var position = []; + for (var i = 1; i <= N; i++) position.push(i); + var value = position.slice(); + + var ret = 0; + var count = function(position, value) { + if (position.length === 1 && value.length === 1) { + if (position[0] % value[0] === 0 || value[0] % position[0] === 0) { + ret++; + } + return + } + for (var i = 0; i < position.length; i++) { + if (position[i] % value[0] === 0 || value[0] % position[i] === 0) { + count(position.slice(0, i).concat(position.slice(i + 1)), value.slice(1)); + } + } + }; + count(position, value); + return ret; +}; + +``` + + +### 529. [Minesweeper](https://leetcode.com/problems/minesweeper/description/) +```javascript +function getMineCount(board, point) { + var m = board.length; + var n = board[0].length; + var x = point[0]; + var y = point[1]; + var count = 0; + for (var i = -1; i < 2; i++) { + for (var j = -1; j < 2; j++) { + var p = x + i; + var q = y + j; + if (p < 0 || p >= m || q < 0 || q >= n) continue; + if (board[p][q] === 'M' || board[p][q] === 'X') count++; + } + } + return count; +} + +function updateSquare(board, point, visited) { + var [x, y] = point; + if (visited[x][y]) return; + visited[x][y] = true; + + var m = board.length; + var n = board[0].length; + var count = getMineCount(board, point); + + if (count === 0) { + board[x][y] = 'B'; + for (var i = -1; i < 2; i++) { + for (var j = -1; j < 2; j++) { + var p = x + i; + var q = y + j; + if (p < 0 || p >= m || q < 0 || q >= n || (p === x && q === y)) continue; + + updateSquare(board, [p, q], visited); + } + } + } else { + board[x][y] = count; + } +} + +var updateBoard = function(board, click) { + var m = board.length; + var n = board[0].length; + var visited = []; + for (var k = 0; k < m; k++) visited.push(new Array(n).fill(false)); + + var [x, y] = click; + if (board[x][y] === 'M') { + board[x][y] = 'X'; + } else { + updateSquare(board, click, visited); + } + + return board; +}; + +``` + + +### 530. [Minimum Absolute Difference in BST](https://leetcode.com/problems/minimum-absolute-difference-in-bst/description/) +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number} + */ + +var getMinimumDifference = function(root) { + var prev = null; + var min = Number.MAX_VALUE; + var traversal = function(root) { + if (root === null) return; + traversal(root.left); + if (prev !== null && root.val - prev < min) { + min = root.val - prev; + } + prev = root.val; + traversal(root.right); + }; + traversal(root); + + return min; +}; +``` + + +### 532. [K-diff Pairs in an Array](https://leetcode.com/problems/k-diff-pairs-in-an-array/description/) +```javascript +/** + * @param {number[]} nums + * @param {number} k + * @return {number} + */ +var findPairs = function(nums, k) { + if (k < 0 || nums.length === 0) return 0; + var itemCountMap = {}; + var count = 0; + + nums.forEach((item, index) => { + itemCountMap[item] === undefined ? itemCountMap[item] = 1 : itemCountMap[item]++; + }); + + if (k === 0) { + for (var key in itemCountMap) { + if (itemCountMap[key] >= 2) { + count++; + } + } + } else { + for (var key in itemCountMap) { + if (itemCountMap[parseInt(key)+ k] !== undefined) { + count++; + } + } + } + + return count; +}; + +``` + + +### 538. [Convert BST to Greater Tree](https://leetcode.com/problems/convert-bst-to-greater-tree/description/) +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {TreeNode} + */ +var convertBST = function(root) { + var sum = 0; + function traverse (root) { + if (root === null) return; + traverse(root.right); + root.val = root.val + sum; + sum = root.val; + traverse(root.left); + } + + traverse(root); + return root; +}; +``` + + +### 539. [Minimum Time Difference](https://leetcode.com/problems/minimum-time-difference/description/) +```javascript +/** + * @param {string[]} timePoints + * @return {number} + */ +var findMinDifference = function(timePoints) { + var ret = Number.MAX_SAFE_INTEGER; + var hour, min; + var mins = timePoints.map(time => { + [hour, min] = time.split(':').map(i => parseInt(i)); + + return hour * 60 + min; + }); + + mins = mins.sort((a, b) => a - b); + for (var i = 0; i < mins.length - 1; i++) { + ret = Math.min(ret, mins[i + 1] - mins[i]); + } + + ret = Math.min(ret, 24 * 60 + mins[0]- mins[mins.length - 1]); + + return ret; +}; +``` + + +### 541. [Reverse String II](https://leetcode.com/problems/reverse-string-ii/description/) +```javascript +/** + * @param {string} s + * @param {number} k + * @return {string} + */ +var reverseStr = function(s, k) { + var arr = s.split(''); + var reverse = true; + var ret = ""; + + for(var i = 0; i < arr.length; i = i + k) { + if (reverse) { + for (var j = Math.min(arr.length, i + k) - 1; j >= i; j--) { + ret += arr[j]; + } + } else { + for (var j = i; j < Math.min(arr.length, i + k); j++) { + ret += arr[j]; + } + } + + reverse = !reverse; + } + + return ret; +}; +``` + + +### 542. [01 Matrix](https://leetcode.com/problems/01-matrix/description/) +```javascript +/** + * @param {number[][]} matrix + * @return {number[][]} + */ +var updateMatrix = function(matrix) { + var m = matrix.length; + var n = matrix[0].length; + var queue = []; + + for (var i = 0; i < m; i++) { + for (var j = 0; j < n; j++) { + if (matrix[i][j] === 0) { + queue.push([i, j]); + } else { + matrix[i][j] = undefined; + } + } + } + + while (queue.length > 0) { + var [x, y] = queue.shift(); + var range = [[-1,0], [1, 0], [0, -1], [0, 1]]; + range.forEach(([p, q]) => { + p += x; + q += y; + if (p < 0 || p >= m || q < 0 || q >= n) return; + if (matrix[p][q] !== undefined && matrix[p][q] < matrix[x][y] + 1) return; + matrix[p][q] = matrix[x][y] + 1; + queue.push([p, q]); + }); + } + + return matrix; +}; + +``` + + +### 543. [Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/description/) +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number} + */ + +var diameterOfBinaryTree = function(root) { + var maxDiameter = -1; + var getDepth = function(root) { + if (root === null) { + return 0; + } + var leftDepth = getDepth(root.left); + var rightDepth = getDepth(root.right); + + maxDiameter = Math.max(maxDiameter, leftDepth + rightDepth); + maxDepth = Math.max(leftDepth, rightDepth) + 1; + return maxDepth; + }; + + if (root === null) return 0; + getDepth(root); + + return maxDiameter; +}; +``` + + +### 544. [Output Contest Matches](https://leetcode.com/problems/output-contest-matches/description/) +```javascript +/** + * @param {number} n + * @return {string} + */ +function getMatch (match) { + if (match.length === 1) return match; + var ret = []; + var start = 0; + var end = match.length - 1; + var value; + + while (start < end) { + value = `(${match[start]},${match[end]})`; + ret.push(value); + start++; + end--; + } + + return getMatch(ret); +} +var findContestMatch = function(n) { + var match = []; + for (var i = 1; i <= n; i++) match.push(i); + + return getMatch(match)[0]; +}; +``` + + +### 545. [Boundary of Binary Tree](https://leetcode.com/problems/boundary-of-binary-tree/description/) +```javascript +/** +* @param {TreeNode} root +* @return {number[]} +*/ + +function rightBorder(node) { + if (node === null) return []; + var ret = []; + + ret.push(node.val); + while (node.left || node.right) { + if (node.right) { + ret.push(node.right.val); + node = node.right; + continue; + } + ret.push(node.left.val); + node = node.left; + } + return ret.reverse(); +} +function leftBorder(node) { + if (node === null) return []; + var ret = []; + + ret.push(node.val); + while (node.left || node.right) { + if (node.left) { + ret.push(node.left.val); + node = node.left; + continue; + } + ret.push(node.right.val); + node = node.right; + } + return ret; +} + +var boundaryOfBinaryTree = function(root) { + if (root === null) return []; + if (root.left === null && root.right === null) return [root.val]; + var leaf = []; + + function traversal (root) { + if (root === null) return true; + var isLeftNull = traversal(root.left); + var isRightNull = traversal(root.right); + if (isLeftNull && isRightNull) { + leaf.push(root.val); + return false; + } + } + + var up = [root.val] + var left = leftBorder(root.left); + var right = rightBorder(root.right); + traversal(root); + + var ret = up + .concat(left.slice(0, left.length - 1)) + .concat(leaf) + .concat(right.slice(1)); + + return ret; +}; +``` + + +### 546. [Remove Boxes](https://leetcode.com/problems/remove-boxes/description/) +```javascript +/** +* @param {number[]} boxes +* @return {number} +*/ + +function get(boxes, memo, i, j, k) { + if (i > j) return 0; + + if (memo[i][j][k] !== undefined) { + return memo[i][j][k]; + } + + var max = get(boxes, memo, i, j - 1, 0) + (k + 1) * (k + 1); + for (var m = i; m < j; m++) { + if (boxes[j] === boxes[m]) { + max = Math.max( + max, + get(boxes, memo, i, m, k + 1) + get(boxes, memo, m + 1, j - 1, 0) + ); + } + } + memo[i][j][k] = max; + return memo[i][j][k]; +} + +var removeBoxes = function(boxes) { + var memo = []; + for (var i = 0; i < boxes.length; i++) { + memo[i] = new Array(boxes.length); + for (var j = 0; j < boxes.length; j++) { + memo[i][j] = new Array(boxes.length); + } + } + + return get(boxes, memo, 0, boxes.length - 1, 0); +}; +``` + + +### 547. [Friend Circles](https://leetcode.com/problems/friend-circles/description/) +```javascript +/** + * @param {number[][]} M + * @return {number} + */ + +function nextUnVisitedIndex(isVisted) { + for (var i = 0; i < isVisted.length; i++) { + if (isVisted[i] === false) { + return i; + } + } + return -1; +} +var findCircleNum = function(M) { + var isVisited = new Array(M.length).fill(false); + var queue = []; + var count = 0; + var next = nextUnVisitedIndex(isVisited); + + while (next !== -1) { + queue.push(next); + count++; + while (queue.length > 0) { + var cur = queue.shift(); + isVisited[cur] = true; + + for (var i = 0; i < M.length; i++) { + if (M[cur][i] === 1 && cur !== i && isVisited[i] === false) { + queue.push(i); + } + } + } + next = nextUnVisitedIndex(isVisited); + } + + return count; +}; + + +``` + + +### 548. [Split Array with Equal Sum](https://leetcode.com/problems/split-array-with-equal-sum/description/) +```javascript +/** + * @param {number[]} nums + * @return {boolean} + */ +function getSum(memo, m, n) { + if (memo[m][n] !== undefined) { + return memo[m][n]; + } else { + memo[m][n] = getSum(memo, m, n - 1) + memo[n][n] + return memo[m][n]; + } +} +var splitArray = function(nums) { + var newArr = []; + var prev; + for (var i = 0; i < nums.length; i++) { + if (nums[i] === 0 && prev === 0) { + continue; + } + newArr.push(nums[i]); + prev = nums[i]; + } + nums = newArr; + + if (nums.length < 7) return false; + + var memo = []; + var len = nums.length; + for (var i = 0; i < len; i++) { + memo.push(new Array(len)); + memo[i][i] = nums[i]; + } + + for (var i = 1; i < len - 5; i++) { + var sum1 = getSum(memo, 0, i - 1); + + for (var j = i + 2; j < len - 3; j++) { + var sum2 = getSum(memo, i + 1, j - 1); + + if (sum1 !== sum2) continue; + for (var k = j + 2; k < len - 1; k++) { + var sum3 = getSum(memo, j + 1, k - 1); + var sum4 = getSum(memo, k + 1, len - 1); + + if (sum1 === sum3 && sum1 === sum4) { + return true; + } + } + } + } + + + return false; +}; +``` + + +### 549. [Binary Tree Longest Consecutive Sequence II](https://leetcode.com/problems/binary-tree-longest-consecutive-sequence-ii/description/) +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number} + */ + +var longestConsecutive = function(root) { + function getLongest(root) { + if (root === null) { + return null; + } + + var left = getLongest(root.left); + var right = getLongest(root.right); + + if (left === null) { + left = {}; + left.d = [root.val]; + left.i = [root.val]; + } + if (right === null) { + right = {}; + right.d = [root.val]; + right.i = [root.val]; + } + + if (left.d[left.d.length - 1] === root.val + 1 && + right.i[right.i.length - 1] === root.val - 1 + ) { + max = Math.max(max, left.d.length + right.i.length + 1); + } + if (right.d[right.d.length - 1] === root.val + 1 && + left.i[left.i.length - 1] === root.val - 1 + ) { + max = Math.max(max, right.d.length + left.i.length + 1); + } + + if (left.d[left.d.length - 1] === root.val + 1) { + left.d.push(root.val); + } else { + left.d = [root.val]; + } + if (left.i[left.i.length - 1] === root.val - 1) { + left.i.push(root.val); + } else { + left.i = [root.val]; + } + + if (right.d[right.d.length - 1] === root.val + 1) { + right.d.push(root.val); + } else { + right.d = [root.val]; + } + if (right.i[right.i.length - 1] === root.val - 1) { + right.i.push(root.val); + } else { + right.i = [root.val]; + } + max = Math.max(max, left.d.length, left.i.length, right.i.length, right.d.length); + + var ret = {}; + ret.d = left.d.length >= right.d.length ? left.d : right.d; + ret.i = left.i.length >= right.i.length ? left.i : right.i; + return ret; + } + + var max = 0; + getLongest(root); + + return max; +}; +``` + + +### 551. [Student Attendance Record I](https://leetcode.com/problems/student-attendance-record-i/description/) +```javascript +/** + * @param {string} s + * @return {boolean} + */ +var checkRecord = function(s) { + var absent = 0; + + for (var i = 0; i < s.length; i++) { + if (s[i] === 'A') absent++; + if (absent >= 2) { + return false; + } + if (i < s.length - 2 && s[i] === 'L' && s[i + 1] === 'L' && s[i + 2] === 'L') { + return false; + } + } + + return true; +}; +``` + + +### 553. [Optimal Division](https://leetcode.com/problems/optimal-division/description/) +```javascript +/** + * @param {number[]} nums + * @return {string} + */ +var optimalDivision = function(nums) { + if (nums.length === 1) { + return String(nums[0]) + } else if (nums.length === 2) { + return nums.join('/'); + } else { + return nums.join('/').replace('/','/(') + ')'; + } +}; +``` + + +### 556. [Next Greater Element III](https://leetcode.com/problems/next-greater-element-iii/description/) +```javascript +/** + * @param {number} n + * @return {number} + */ + function swap(a, i, j) { + var tmp = a[i]; + a[i] = a[j]; + a[j] = tmp; + } + var nextGreaterElement = function(n) { + if (n < 10) return -1; + var a = String(n).split(''); + var max = a[a.length - 1]; + var minIndex; + var ret; + + for (var i = a.length - 2; i >= 0; i--) { + if (a[i] < max) { + minIndex = i + 1; + for (var j = i + 2; j < a.length; j++) { + if (a[j] > a[i] && a[j] < a[minIndex]) { + minIndex = j; + } + } + swap(a, i, minIndex); + ret = a.slice(0, i + 1).concat(a.slice(i + 1).sort()).join(''); + ret = parseInt(ret); + if (ret > Math.pow(2, 31)) { + break; + } + return ret; + } + if (a[i] > max) { + max = a[i]; + } + } + + return -1; + }; +``` + + +### 560. [Subarray Sum Equals K](https://leetcode.com/problems/subarray-sum-equals-k/description/) +```javascript +/** + * @param {number[]} nums + * @param {number} k + * @return {number} + */ +var subarraySum = function(nums, k) { + var len = nums.length; + var sum = 0; + var count = 0; + + for (var i = 0; i < len; i++) { + for (var j = i; j < len; j++) { + sum += nums[j]; + if (sum === k) { + count++ + } + } + sum = 0; + } + + return count; +}; +``` + + +### 561. [Array Partition I](https://leetcode.com/problems/array-partition-i/description/) +```javascript +/** + * @param {number[]} nums + * @return {number} + */ +var arrayPairSum = function(nums) { + nums = nums.sort((a, b) => a - b); + return nums.reduce((p, c, index) => { + return index % 2 === 0 ? p + c : p; + }); +}; +``` + + +### 562. [Longest Line of Consecutive One in Matrix](https://leetcode.com/problems/longest-line-of-consecutive-one-in-matrix/description/) +```javascript +/** + * @param {number[][]} M + * @return {number} + */ +var longestLine = function(M) { + if (M.length === 0) { + return 0; + } + var m = M.length; + var n = M[0].length; + var max = 0; + var dp = []; + for (var i = 0; i < m; i++) { + var tmp = []; + for (var j = 0; j < n; j++) { + tmp.push(new Array(4).fill(0)); + } + dp.push(tmp); + } + + for (var i = 0; i < m; i++) { + for (var j = 0; j < n; j++) { + if (M[i][j] === 0) { + continue; + } + for (var k = 0; k < 4; k++) { + dp[i][j][k] = 1; + } + if (j > 0) { + dp[i][j][0] = dp[i][j - 1][0] + 1; + } + if (i > 0) { + dp[i][j][1] = dp[i - 1][j][1] + 1; + } + if (i > 0 && j > 0) { + dp[i][j][2] = dp[i - 1][j - 1][2] + 1; + } + if (i > 0 && j < n - 1) { + dp[i][j][3] = dp[i - 1][j + 1][3] + 1; + } + max = Math.max(max, dp[i][j][0], dp[i][j][1], dp[i][j][2], dp[i][j][3]) + } + } + + return max; +}; +``` + + +### 617. [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/description/) +```python +class Solution(object): + def mergeTrees(self, t1, t2): + if t1 and t2: + t1.val += t2.val + self.mergeChildTrees(t1, t2) + return t1 + elif t1: + return t1 + else: + return t2 + + def mergeChildTrees(self, t1, t2): + if t2 == None: + return + if t1.left and t2.left: + t1.left.val += t2.left.val + elif t2.left: + t1.left = t2.left + t2.left = None + + if t1.right and t2.right: + t1.right.val += t2.right.val + elif t2.right: + t1.right = t2.right + t2.right = None + + self.mergeChildTrees(t1.left, t2.left) + self.mergeChildTrees(t1.right, t2.right) +``` + + +### 646. [Maximum Length of Pair Chain](https://leetcode.com/problems/maximum-length-of-pair-chain/description/) +```javascript +/** + * @param {number[][]} pairs + * @return {number} + */ +var findLongestChain = function(pairs) { + pairs = pairs.sort((a, b) => a[0] - b[0]); + + var len = pairs.length; + var dp = Array(len).fill(1); + + for (var i = 1; i < len; i++) { + for (var j = 0; j < i; j++) { + if (pairs[i][0] > pairs[j][1]) { + dp[i] = Math.max(dp[i], dp[j] + 1); + } + } + } + + return Math.max.apply(null, dp); +}; +``` + + +### 647. [Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/description/) +```javascript +/** + * @param {string} s + * @return {number} + */ + +function isPalindromic(s) { + for (var i = 0, j = s.length - 1; i < j; i++, j--) { + if(s[i] !== s[j]) { + return false; + } + } + return true; +} +var countSubstrings = function(s) { + var count = 0; + var sub; + for(i = 0; i < s.length; i++) { + for(j = i + 1; j <= s.length; j++) { + sub = s.slice(i, j); + if (isPalindromic(sub)) { + count++; + } + } + } + return count; +}; +``` + + +### 650. [2 Keys Keyboard](https://leetcode.com/problems/2-keys-keyboard/description/) +```javascript +/** + * @param {number} n + * @return {number} + */ +var minSteps = function(n) { + if (n === 1) return 0; + var ret = 0; + for (var i = 2; i <= n; i++) { + while( n !== i) { + if (n % i === 0) { + n = n / i; + ret = ret + i; + } else { + break; + } + } + } + ret = ret + n; + return ret; +}; +``` + + +### 657. [Robot Return to Origin](https://leetcode.com/problems/robot-return-to-origin/description/) +```python +class Solution(object): + def judgeCircle(self, moves): + v = 0 + h = 0 + for i in moves: + if i == 'U': + v += 1 + elif i == 'D': + v -= 1 + elif i == 'L': + h -= 1 + elif i == 'R': + h += 1 + + return v == 0 and h == 0 +``` + + +### 681. [Next Closest Time](https://leetcode.com/problems/next-closest-time/description/) +```javascript +/** + * @param {string} time + * @return {string} + */ +var nextClosestTime = function(time) { + var cur = parseInt(time.slice(0, 2)) * 60 + parseInt(time.slice(3)); + var charMap = {}; + time.split('') + .filter(i => i != ':') + .forEach(key => charMap[key] = true); + + for (var i = 1; i <= 24 * 60; i++) { + var next = (cur + i) % (24 * 60); + var hour = parseInt(next / 60); + var min = next - hour * 60; + + var h1 = String(parseInt(hour / 10)); + var h2 = String(hour % 10); + var m1 = String(parseInt(min / 10)); + var m2 = String(parseInt(min % 10)); + + if (charMap[h1] && charMap[h2] && charMap[m1] && charMap[m2]) { + return h1 + h2 + ':' + m1 + m2; + } + } +}; +``` + + +### 686. [Repeated String Match](https://leetcode.com/problems/repeated-string-match/description/) +```python +class Solution(object): + def repeatedStringMatch(self, A, B): + count = 0 + target = '' + while (len(target) < len(B)): + target = target + A + count += 1 + if target.find(B) > -1: + return count + elif (target + A).find(B) > -1: + return count + 1 + else: + return -1 +``` + + +### 687. [Longest Univalue Path](https://leetcode.com/problems/longest-univalue-path/description/) +```python +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution(object): + def longestUnivaluePath(self, root): + """ + :type root: TreeNode + :rtype: int + """ + if root == None: + return 0 + self.maxPath = 0 + self.find(root, root.val) + return self.maxPath + + def find(self, node, val): + if node == None: + return 0 + left = self.find(node.left, node.val) + right = self.find(node.right, node.val) + self.maxPath = max(self.maxPath, right + left) + if node.val == val: + return max(right, left) + 1 + else: + return 0 +``` + + +### 712. [Minimum ASCII Delete Sum for Two Strings](https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/description/) +```javascript +/** + * @param {string} s1 + * @param {string} s2 + * @return {number} + */ +var minimumDeleteSum = function(s1, s2) { + var dp = Array(s1.length + 1).fill(0).map(() => Array(s2.length + 1).fill(0)); + for (var i = 1; i <= s1.length; i++) { + dp[i][0] = s1[i - 1].charCodeAt() + dp[i - 1][0]; + } + for (var j = 1; j <= s2.length; j++) { + dp[0][j] = s2[j - 1].charCodeAt() + dp[0][j - 1]; + } + + for (var i = 1; i <= s1.length; i++) { + for (var j = 1; j <= s2.length; j++) { + if (s1[i - 1] === s2[j - 1]) { + dp[i][j] = dp[i - 1][j - 1]; + } else { + dp[i][j] = Math.min( + dp[i - 1][j] + s1[i - 1].charCodeAt(), + dp[i][j - 1] + s2[j - 1].charCodeAt(), + ); + } + } + } + + return dp[s1.length][s2.length]; + +}; +``` + + +### 733. [Flood Fill](https://leetcode.com/problems/flood-fill/description/) +```javascript +/** + * @param {number[][]} image + * @param {number} sr + * @param {number} sc + * @param {number} newColor + * @return {number[][]} + */ +var floodFill = function(image, sr, sc, newColor) { + if (image.length === 0) { + return; + } + if (image[0].length === 0) { + return; + } + + var queue = [[sr,sc]]; + var isVisited = {}; + var oldColor = image[sr][sc]; + var option = [[1, 0], [-1, 0], [0, 1], [0, -1]]; + + while(queue.length > 0) { + var point = queue.shift(); + + isVisited[point[0] + '_' + point[1]] = true; + image[point[0]][point[1]] = newColor; + + option.forEach((delta) => { + var dest = [point[0] + delta[0], point[1] + delta[1]]; + if (isVisited[dest[0] + '_' + dest[1]] === undefined && + dest[0] >= 0 && dest[0] < image.length && dest[1] >= 0 && dest[1] < image[0].length && + image[dest[0]][dest[1]] === oldColor + ) { + queue.push(dest); + } + }) + } + return image; +}; +``` + + +### 740. [Delete and Earn](https://leetcode.com/problems/delete-and-earn/description/) +```javascript +/** + * @param {number[]} nums + * @return {number} + */ +var deleteAndEarn = function(nums) { + if(nums.length === 0) return 0; + if(nums.length === 1) return nums[0]; + + + var max = Math.max.apply(null, nums) + var len = max + 1; + + var map = {}; + for (var i = 0; i < len; i++) { + map[i] = 0; + } + for (var i = 0; i < nums.length; i++) { + map[nums[i]] += nums[i]; + } + + var dp = Array(len).fill(0); + dp[1] = map[1]; + + for (var i = 2; i < len; i++) { + dp[i] = Math.max( + map[i] + dp[i - 2], + dp[i - 1] + ) + } + + return dp[len - 1]; +}; +``` + + +### 747. [Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs/description/) +```python +class Solution(object): + def minCostClimbingStairs(self, cost): + """ + :type cost: List[int] + :rtype: int + """ + count = len(cost) + dp = [None] * count + dp[0] = cost[0] + dp[1] = cost[1] + for i in range(2, count): + dp[i] = min(dp[i - 1] + cost[i], dp[i - 2] + cost[i]) + + return min(dp[count - 1], dp[count - 2]) +``` + + +### 909. [Stone Game](https://leetcode.com/problems/stone-game/description/) +```javascript +/** + * @param {number[]} piles + * @return {boolean} + */ +var stoneGame = function(piles) { + var len = piles.length; + const dp = Array(len).fill(0).map(() => Array(len).fill(0)); + + for (var i = i; i < len; i++) dp[i][i] = piles[i]; + + for (var j = 1; j < len; j++) { + for (var i = 0; i < len - j; i++) { + dp[i][j] = Math.max( + piles[i] - dp[i - i][j], + piles[j] - dp[i][j - 1] + ) + } + } + + return dp[0][len - 1] > 0; +}; +``` + diff --git a/Algorithms/01 Matrix/README.md b/old(deprecated)/Algorithms/01 Matrix/README.md similarity index 100% rename from Algorithms/01 Matrix/README.md rename to old(deprecated)/Algorithms/01 Matrix/README.md diff --git a/Algorithms/3Sum Closest/README.md b/old(deprecated)/Algorithms/3Sum Closest/README.md similarity index 100% rename from Algorithms/3Sum Closest/README.md rename to old(deprecated)/Algorithms/3Sum Closest/README.md diff --git a/Algorithms/3Sum/README.md b/old(deprecated)/Algorithms/3Sum/README.md similarity index 100% rename from Algorithms/3Sum/README.md rename to old(deprecated)/Algorithms/3Sum/README.md diff --git a/Algorithms/Add Digits/README.md b/old(deprecated)/Algorithms/Add Digits/README.md similarity index 100% rename from Algorithms/Add Digits/README.md rename to old(deprecated)/Algorithms/Add Digits/README.md diff --git a/Algorithms/Add Strings/README.md b/old(deprecated)/Algorithms/Add Strings/README.md similarity index 100% rename from Algorithms/Add Strings/README.md rename to old(deprecated)/Algorithms/Add Strings/README.md diff --git a/Algorithms/Arithmetic Slices/README.md b/old(deprecated)/Algorithms/Arithmetic Slices/README.md similarity index 100% rename from Algorithms/Arithmetic Slices/README.md rename to old(deprecated)/Algorithms/Arithmetic Slices/README.md diff --git a/Algorithms/Arranging Coins/README.md b/old(deprecated)/Algorithms/Arranging Coins/README.md similarity index 100% rename from Algorithms/Arranging Coins/README.md rename to old(deprecated)/Algorithms/Arranging Coins/README.md diff --git a/Algorithms/Assign Cookies/README.md b/old(deprecated)/Algorithms/Assign Cookies/README.md similarity index 100% rename from Algorithms/Assign Cookies/README.md rename to old(deprecated)/Algorithms/Assign Cookies/README.md diff --git a/Algorithms/Balanced Binary Tree/README.md b/old(deprecated)/Algorithms/Balanced Binary Tree/README.md similarity index 100% rename from Algorithms/Balanced Binary Tree/README.md rename to old(deprecated)/Algorithms/Balanced Binary Tree/README.md diff --git a/Algorithms/Base 7/README.md b/old(deprecated)/Algorithms/Base 7/README.md similarity index 100% rename from Algorithms/Base 7/README.md rename to old(deprecated)/Algorithms/Base 7/README.md diff --git a/Algorithms/Beautiful Arrangement/README.md b/old(deprecated)/Algorithms/Beautiful Arrangement/README.md similarity index 100% rename from Algorithms/Beautiful Arrangement/README.md rename to old(deprecated)/Algorithms/Beautiful Arrangement/README.md diff --git a/Algorithms/Best Time to Buy and Sell Stock II/README.md b/old(deprecated)/Algorithms/Best Time to Buy and Sell Stock II/README.md similarity index 100% rename from Algorithms/Best Time to Buy and Sell Stock II/README.md rename to old(deprecated)/Algorithms/Best Time to Buy and Sell Stock II/README.md diff --git a/Algorithms/Best Time to Buy and Sell Stock/README.md b/old(deprecated)/Algorithms/Best Time to Buy and Sell Stock/README.md similarity index 100% rename from Algorithms/Best Time to Buy and Sell Stock/README.md rename to old(deprecated)/Algorithms/Best Time to Buy and Sell Stock/README.md diff --git a/Algorithms/Binary Tree Level Order Traversal II/README.md b/old(deprecated)/Algorithms/Binary Tree Level Order Traversal II/README.md similarity index 100% rename from Algorithms/Binary Tree Level Order Traversal II/README.md rename to old(deprecated)/Algorithms/Binary Tree Level Order Traversal II/README.md diff --git a/Algorithms/Binary Tree Level Order Traversal/README.md b/old(deprecated)/Algorithms/Binary Tree Level Order Traversal/README.md similarity index 100% rename from Algorithms/Binary Tree Level Order Traversal/README.md rename to old(deprecated)/Algorithms/Binary Tree Level Order Traversal/README.md diff --git a/Algorithms/Binary Tree Paths/README.md b/old(deprecated)/Algorithms/Binary Tree Paths/README.md similarity index 100% rename from Algorithms/Binary Tree Paths/README.md rename to old(deprecated)/Algorithms/Binary Tree Paths/README.md diff --git a/Algorithms/Binary Tree Preorder Traversal/README.md b/old(deprecated)/Algorithms/Binary Tree Preorder Traversal/README.md similarity index 100% rename from Algorithms/Binary Tree Preorder Traversal/README.md rename to old(deprecated)/Algorithms/Binary Tree Preorder Traversal/README.md diff --git a/Algorithms/Binary Watch/README.md b/old(deprecated)/Algorithms/Binary Watch/README.md similarity index 100% rename from Algorithms/Binary Watch/README.md rename to old(deprecated)/Algorithms/Binary Watch/README.md diff --git a/Algorithms/Bulb Switcher/README.md b/old(deprecated)/Algorithms/Bulb Switcher/README.md similarity index 100% rename from Algorithms/Bulb Switcher/README.md rename to old(deprecated)/Algorithms/Bulb Switcher/README.md diff --git a/Algorithms/Climbing Stairs/README.md b/old(deprecated)/Algorithms/Climbing Stairs/README.md similarity index 100% rename from Algorithms/Climbing Stairs/README.md rename to old(deprecated)/Algorithms/Climbing Stairs/README.md diff --git a/Algorithms/Compare Version Numbers/README.md b/old(deprecated)/Algorithms/Compare Version Numbers/README.md similarity index 100% rename from Algorithms/Compare Version Numbers/README.md rename to old(deprecated)/Algorithms/Compare Version Numbers/README.md diff --git a/Algorithms/Construct the Rectangle/README.md b/old(deprecated)/Algorithms/Construct the Rectangle/README.md similarity index 100% rename from Algorithms/Construct the Rectangle/README.md rename to old(deprecated)/Algorithms/Construct the Rectangle/README.md diff --git a/Algorithms/Contains Duplicate II/README.md b/old(deprecated)/Algorithms/Contains Duplicate II/README.md similarity index 100% rename from Algorithms/Contains Duplicate II/README.md rename to old(deprecated)/Algorithms/Contains Duplicate II/README.md diff --git a/Algorithms/Contains Duplicate/README.md b/old(deprecated)/Algorithms/Contains Duplicate/README.md similarity index 100% rename from Algorithms/Contains Duplicate/README.md rename to old(deprecated)/Algorithms/Contains Duplicate/README.md diff --git a/Algorithms/Convert BST to Greater Tree/README.md b/old(deprecated)/Algorithms/Convert BST to Greater Tree/README.md similarity index 100% rename from Algorithms/Convert BST to Greater Tree/README.md rename to old(deprecated)/Algorithms/Convert BST to Greater Tree/README.md diff --git a/Algorithms/Convert Sorted Array to Binary Search Tree/README.md b/old(deprecated)/Algorithms/Convert Sorted Array to Binary Search Tree/README.md similarity index 100% rename from Algorithms/Convert Sorted Array to Binary Search Tree/README.md rename to old(deprecated)/Algorithms/Convert Sorted Array to Binary Search Tree/README.md diff --git a/Algorithms/Copy List with Random Pointer/README.md b/old(deprecated)/Algorithms/Copy List with Random Pointer/README.md similarity index 100% rename from Algorithms/Copy List with Random Pointer/README.md rename to old(deprecated)/Algorithms/Copy List with Random Pointer/README.md diff --git a/Algorithms/Count Primes/README.md b/old(deprecated)/Algorithms/Count Primes/README.md similarity index 100% rename from Algorithms/Count Primes/README.md rename to old(deprecated)/Algorithms/Count Primes/README.md diff --git a/Algorithms/Count and Say/README.md b/old(deprecated)/Algorithms/Count and Say/README.md similarity index 100% rename from Algorithms/Count and Say/README.md rename to old(deprecated)/Algorithms/Count and Say/README.md diff --git a/Algorithms/Counting Bits/README.md b/old(deprecated)/Algorithms/Counting Bits/README.md similarity index 100% rename from Algorithms/Counting Bits/README.md rename to old(deprecated)/Algorithms/Counting Bits/README.md diff --git a/Algorithms/Delete Node in a BST/README.md b/old(deprecated)/Algorithms/Delete Node in a BST/README.md similarity index 100% rename from Algorithms/Delete Node in a BST/README.md rename to old(deprecated)/Algorithms/Delete Node in a BST/README.md diff --git a/Algorithms/Delete Node in a Linked List/README.md b/old(deprecated)/Algorithms/Delete Node in a Linked List/README.md similarity index 100% rename from Algorithms/Delete Node in a Linked List/README.md rename to old(deprecated)/Algorithms/Delete Node in a Linked List/README.md diff --git a/Algorithms/Detect Capital/README.md b/old(deprecated)/Algorithms/Detect Capital/README.md similarity index 100% rename from Algorithms/Detect Capital/README.md rename to old(deprecated)/Algorithms/Detect Capital/README.md diff --git a/Algorithms/Diameter of Binary Tree/README.md b/old(deprecated)/Algorithms/Diameter of Binary Tree/README.md similarity index 100% rename from Algorithms/Diameter of Binary Tree/README.md rename to old(deprecated)/Algorithms/Diameter of Binary Tree/README.md diff --git a/Algorithms/Divide Two Integers/README.md b/old(deprecated)/Algorithms/Divide Two Integers/README.md similarity index 100% rename from Algorithms/Divide Two Integers/README.md rename to old(deprecated)/Algorithms/Divide Two Integers/README.md diff --git a/Algorithms/Evaluate Reverse Polish Notation/README.md b/old(deprecated)/Algorithms/Evaluate Reverse Polish Notation/README.md similarity index 100% rename from Algorithms/Evaluate Reverse Polish Notation/README.md rename to old(deprecated)/Algorithms/Evaluate Reverse Polish Notation/README.md diff --git a/Algorithms/Excel Sheet Column Number/README.md b/old(deprecated)/Algorithms/Excel Sheet Column Number/README.md similarity index 100% rename from Algorithms/Excel Sheet Column Number/README.md rename to old(deprecated)/Algorithms/Excel Sheet Column Number/README.md diff --git a/Algorithms/Excel Sheet Column Title/README.md b/old(deprecated)/Algorithms/Excel Sheet Column Title/README.md similarity index 100% rename from Algorithms/Excel Sheet Column Title/README.md rename to old(deprecated)/Algorithms/Excel Sheet Column Title/README.md diff --git a/Algorithms/Factorial Trailing Zeroes/README.md b/old(deprecated)/Algorithms/Factorial Trailing Zeroes/README.md similarity index 100% rename from Algorithms/Factorial Trailing Zeroes/README.md rename to old(deprecated)/Algorithms/Factorial Trailing Zeroes/README.md diff --git a/Algorithms/Find All Duplicates in an Array/README.md b/old(deprecated)/Algorithms/Find All Duplicates in an Array/README.md similarity index 100% rename from Algorithms/Find All Duplicates in an Array/README.md rename to old(deprecated)/Algorithms/Find All Duplicates in an Array/README.md diff --git a/Algorithms/Find All Numbers Disappeared in an Array/README.md b/old(deprecated)/Algorithms/Find All Numbers Disappeared in an Array/README.md similarity index 100% rename from Algorithms/Find All Numbers Disappeared in an Array/README.md rename to old(deprecated)/Algorithms/Find All Numbers Disappeared in an Array/README.md diff --git a/Algorithms/Find Bottom Left Tree Value/README.md b/old(deprecated)/Algorithms/Find Bottom Left Tree Value/README.md similarity index 100% rename from Algorithms/Find Bottom Left Tree Value/README.md rename to old(deprecated)/Algorithms/Find Bottom Left Tree Value/README.md diff --git a/Algorithms/Find Largest Value in Each Tree Row/README.md b/old(deprecated)/Algorithms/Find Largest Value in Each Tree Row/README.md similarity index 100% rename from Algorithms/Find Largest Value in Each Tree Row/README.md rename to old(deprecated)/Algorithms/Find Largest Value in Each Tree Row/README.md diff --git a/Algorithms/Find Mode in Binary Search Tree/README.md b/old(deprecated)/Algorithms/Find Mode in Binary Search Tree/README.md similarity index 100% rename from Algorithms/Find Mode in Binary Search Tree/README.md rename to old(deprecated)/Algorithms/Find Mode in Binary Search Tree/README.md diff --git a/Algorithms/Find the Difference/README.md b/old(deprecated)/Algorithms/Find the Difference/README.md similarity index 100% rename from Algorithms/Find the Difference/README.md rename to old(deprecated)/Algorithms/Find the Difference/README.md diff --git a/Algorithms/First Bad Version/README.md b/old(deprecated)/Algorithms/First Bad Version/README.md similarity index 100% rename from Algorithms/First Bad Version/README.md rename to old(deprecated)/Algorithms/First Bad Version/README.md diff --git a/Algorithms/First Unique Character in a String/README.md b/old(deprecated)/Algorithms/First Unique Character in a String/README.md similarity index 100% rename from Algorithms/First Unique Character in a String/README.md rename to old(deprecated)/Algorithms/First Unique Character in a String/README.md diff --git a/Algorithms/Fizz Buzz/README.md b/old(deprecated)/Algorithms/Fizz Buzz/README.md similarity index 100% rename from Algorithms/Fizz Buzz/README.md rename to old(deprecated)/Algorithms/Fizz Buzz/README.md diff --git a/Algorithms/Generate Parentheses/README.md b/old(deprecated)/Algorithms/Generate Parentheses/README.md similarity index 100% rename from Algorithms/Generate Parentheses/README.md rename to old(deprecated)/Algorithms/Generate Parentheses/README.md diff --git a/Algorithms/Guess Number Higher or Lower/README.md b/old(deprecated)/Algorithms/Guess Number Higher or Lower/README.md similarity index 100% rename from Algorithms/Guess Number Higher or Lower/README.md rename to old(deprecated)/Algorithms/Guess Number Higher or Lower/README.md diff --git a/Algorithms/Hamming Distance/README.md b/old(deprecated)/Algorithms/Hamming Distance/README.md similarity index 100% rename from Algorithms/Hamming Distance/README.md rename to old(deprecated)/Algorithms/Hamming Distance/README.md diff --git a/Algorithms/Happy Number/README.md b/old(deprecated)/Algorithms/Happy Number/README.md similarity index 100% rename from Algorithms/Happy Number/README.md rename to old(deprecated)/Algorithms/Happy Number/README.md diff --git a/Algorithms/House Robber/README.md b/old(deprecated)/Algorithms/House Robber/README.md similarity index 100% rename from Algorithms/House Robber/README.md rename to old(deprecated)/Algorithms/House Robber/README.md diff --git a/Algorithms/Implement Queue using Stacks/README.md b/old(deprecated)/Algorithms/Implement Queue using Stacks/README.md similarity index 100% rename from Algorithms/Implement Queue using Stacks/README.md rename to old(deprecated)/Algorithms/Implement Queue using Stacks/README.md diff --git a/Algorithms/Implement strStr()/README.md b/old(deprecated)/Algorithms/Implement strStr()/README.md similarity index 100% rename from Algorithms/Implement strStr()/README.md rename to old(deprecated)/Algorithms/Implement strStr()/README.md diff --git a/Algorithms/Integer Break/README.md b/old(deprecated)/Algorithms/Integer Break/README.md similarity index 100% rename from Algorithms/Integer Break/README.md rename to old(deprecated)/Algorithms/Integer Break/README.md diff --git a/Algorithms/Integer to Roman/README.md b/old(deprecated)/Algorithms/Integer to Roman/README.md similarity index 100% rename from Algorithms/Integer to Roman/README.md rename to old(deprecated)/Algorithms/Integer to Roman/README.md diff --git a/Algorithms/Intersection of Two Arrays II/README.md b/old(deprecated)/Algorithms/Intersection of Two Arrays II/README.md similarity index 100% rename from Algorithms/Intersection of Two Arrays II/README.md rename to old(deprecated)/Algorithms/Intersection of Two Arrays II/README.md diff --git a/Algorithms/Intersection of Two Arrays/README.md b/old(deprecated)/Algorithms/Intersection of Two Arrays/README.md similarity index 100% rename from Algorithms/Intersection of Two Arrays/README.md rename to old(deprecated)/Algorithms/Intersection of Two Arrays/README.md diff --git a/Algorithms/Intersection of Two Linked Lists/README.md b/old(deprecated)/Algorithms/Intersection of Two Linked Lists/README.md similarity index 100% rename from Algorithms/Intersection of Two Linked Lists/README.md rename to old(deprecated)/Algorithms/Intersection of Two Linked Lists/README.md diff --git a/Algorithms/Invert Binary Tree/README.md b/old(deprecated)/Algorithms/Invert Binary Tree/README.md similarity index 100% rename from Algorithms/Invert Binary Tree/README.md rename to old(deprecated)/Algorithms/Invert Binary Tree/README.md diff --git a/Algorithms/Island Perimeter/README.md b/old(deprecated)/Algorithms/Island Perimeter/README.md similarity index 100% rename from Algorithms/Island Perimeter/README.md rename to old(deprecated)/Algorithms/Island Perimeter/README.md diff --git a/Algorithms/Isomorphic Strings/README.md b/old(deprecated)/Algorithms/Isomorphic Strings/README.md similarity index 100% rename from Algorithms/Isomorphic Strings/README.md rename to old(deprecated)/Algorithms/Isomorphic Strings/README.md diff --git a/Algorithms/K-diff Pairs in an Array/README.md b/old(deprecated)/Algorithms/K-diff Pairs in an Array/README.md similarity index 100% rename from Algorithms/K-diff Pairs in an Array/README.md rename to old(deprecated)/Algorithms/K-diff Pairs in an Array/README.md diff --git a/Algorithms/Keyboard Row/README.md b/old(deprecated)/Algorithms/Keyboard Row/README.md similarity index 100% rename from Algorithms/Keyboard Row/README.md rename to old(deprecated)/Algorithms/Keyboard Row/README.md diff --git a/Algorithms/Largest Number/README.md b/old(deprecated)/Algorithms/Largest Number/README.md similarity index 100% rename from Algorithms/Largest Number/README.md rename to old(deprecated)/Algorithms/Largest Number/README.md diff --git a/Algorithms/Length of Last Word/README.md b/old(deprecated)/Algorithms/Length of Last Word/README.md similarity index 100% rename from Algorithms/Length of Last Word/README.md rename to old(deprecated)/Algorithms/Length of Last Word/README.md diff --git a/Algorithms/Letter Combinations of a Phone Number/README.md b/old(deprecated)/Algorithms/Letter Combinations of a Phone Number/README.md similarity index 100% rename from Algorithms/Letter Combinations of a Phone Number/README.md rename to old(deprecated)/Algorithms/Letter Combinations of a Phone Number/README.md diff --git a/Algorithms/License Key Formatting/README.md b/old(deprecated)/Algorithms/License Key Formatting/README.md similarity index 100% rename from Algorithms/License Key Formatting/README.md rename to old(deprecated)/Algorithms/License Key Formatting/README.md diff --git a/Algorithms/Linked List Cycle/README.md b/old(deprecated)/Algorithms/Linked List Cycle/README.md similarity index 100% rename from Algorithms/Linked List Cycle/README.md rename to old(deprecated)/Algorithms/Linked List Cycle/README.md diff --git a/Algorithms/Linked List Random Node/README.md b/old(deprecated)/Algorithms/Linked List Random Node/README.md similarity index 100% rename from Algorithms/Linked List Random Node/README.md rename to old(deprecated)/Algorithms/Linked List Random Node/README.md diff --git a/Algorithms/Longest Common Prefix/README.md b/old(deprecated)/Algorithms/Longest Common Prefix/README.md similarity index 100% rename from Algorithms/Longest Common Prefix/README.md rename to old(deprecated)/Algorithms/Longest Common Prefix/README.md diff --git a/Algorithms/Longest Palindrome/README.md b/old(deprecated)/Algorithms/Longest Palindrome/README.md similarity index 100% rename from Algorithms/Longest Palindrome/README.md rename to old(deprecated)/Algorithms/Longest Palindrome/README.md diff --git a/Algorithms/Lowest Common Ancestor of a Binary Search Tree/README.md b/old(deprecated)/Algorithms/Lowest Common Ancestor of a Binary Search Tree/README.md similarity index 100% rename from Algorithms/Lowest Common Ancestor of a Binary Search Tree/README.md rename to old(deprecated)/Algorithms/Lowest Common Ancestor of a Binary Search Tree/README.md diff --git a/Algorithms/Majority Element/README.md b/old(deprecated)/Algorithms/Majority Element/README.md similarity index 100% rename from Algorithms/Majority Element/README.md rename to old(deprecated)/Algorithms/Majority Element/README.md diff --git a/Algorithms/Max Consecutive Ones/README.md b/old(deprecated)/Algorithms/Max Consecutive Ones/README.md similarity index 100% rename from Algorithms/Max Consecutive Ones/README.md rename to old(deprecated)/Algorithms/Max Consecutive Ones/README.md diff --git a/Algorithms/Max Points on a Line/README.md b/old(deprecated)/Algorithms/Max Points on a Line/README.md similarity index 100% rename from Algorithms/Max Points on a Line/README.md rename to old(deprecated)/Algorithms/Max Points on a Line/README.md diff --git a/Algorithms/Maximum Depth of Binary Tree/README.md b/old(deprecated)/Algorithms/Maximum Depth of Binary Tree/README.md similarity index 100% rename from Algorithms/Maximum Depth of Binary Tree/README.md rename to old(deprecated)/Algorithms/Maximum Depth of Binary Tree/README.md diff --git a/Algorithms/Maximum Subarray/README.md b/old(deprecated)/Algorithms/Maximum Subarray/README.md similarity index 100% rename from Algorithms/Maximum Subarray/README.md rename to old(deprecated)/Algorithms/Maximum Subarray/README.md diff --git a/Algorithms/Merge Sorted Array/README.md b/old(deprecated)/Algorithms/Merge Sorted Array/README.md similarity index 100% rename from Algorithms/Merge Sorted Array/README.md rename to old(deprecated)/Algorithms/Merge Sorted Array/README.md diff --git a/Algorithms/Merge Two Sorted Lists/README.md b/old(deprecated)/Algorithms/Merge Two Sorted Lists/README.md similarity index 100% rename from Algorithms/Merge Two Sorted Lists/README.md rename to old(deprecated)/Algorithms/Merge Two Sorted Lists/README.md diff --git a/Algorithms/Min Stack/README.md b/old(deprecated)/Algorithms/Min Stack/README.md similarity index 100% rename from Algorithms/Min Stack/README.md rename to old(deprecated)/Algorithms/Min Stack/README.md diff --git a/Algorithms/Minesweeper/README.md b/old(deprecated)/Algorithms/Minesweeper/README.md similarity index 100% rename from Algorithms/Minesweeper/README.md rename to old(deprecated)/Algorithms/Minesweeper/README.md diff --git a/Algorithms/Minimum Absolute Difference in BST/README.md b/old(deprecated)/Algorithms/Minimum Absolute Difference in BST/README.md similarity index 100% rename from Algorithms/Minimum Absolute Difference in BST/README.md rename to old(deprecated)/Algorithms/Minimum Absolute Difference in BST/README.md diff --git a/Algorithms/Minimum Depth of Binary Tree/README.md b/old(deprecated)/Algorithms/Minimum Depth of Binary Tree/README.md similarity index 100% rename from Algorithms/Minimum Depth of Binary Tree/README.md rename to old(deprecated)/Algorithms/Minimum Depth of Binary Tree/README.md diff --git a/Algorithms/Minimum Moves to Equal Array Elements/README.md b/old(deprecated)/Algorithms/Minimum Moves to Equal Array Elements/README.md similarity index 100% rename from Algorithms/Minimum Moves to Equal Array Elements/README.md rename to old(deprecated)/Algorithms/Minimum Moves to Equal Array Elements/README.md diff --git a/Algorithms/Missing Number/README.md b/old(deprecated)/Algorithms/Missing Number/README.md similarity index 100% rename from Algorithms/Missing Number/README.md rename to old(deprecated)/Algorithms/Missing Number/README.md diff --git a/Algorithms/Most Frequent Subtree Sum/README.md b/old(deprecated)/Algorithms/Most Frequent Subtree Sum/README.md similarity index 100% rename from Algorithms/Most Frequent Subtree Sum/README.md rename to old(deprecated)/Algorithms/Most Frequent Subtree Sum/README.md diff --git a/Algorithms/Move Zeroes/README.md b/old(deprecated)/Algorithms/Move Zeroes/README.md similarity index 100% rename from Algorithms/Move Zeroes/README.md rename to old(deprecated)/Algorithms/Move Zeroes/README.md diff --git a/Algorithms/Next Greater Element I/README.md b/old(deprecated)/Algorithms/Next Greater Element I/README.md similarity index 100% rename from Algorithms/Next Greater Element I/README.md rename to old(deprecated)/Algorithms/Next Greater Element I/README.md diff --git a/Algorithms/Next Greater Element II/README.md b/old(deprecated)/Algorithms/Next Greater Element II/README.md similarity index 100% rename from Algorithms/Next Greater Element II/README.md rename to old(deprecated)/Algorithms/Next Greater Element II/README.md diff --git a/Algorithms/Next Permutation/README.md b/old(deprecated)/Algorithms/Next Permutation/README.md similarity index 100% rename from Algorithms/Next Permutation/README.md rename to old(deprecated)/Algorithms/Next Permutation/README.md diff --git a/Algorithms/Nim Game/README.md b/old(deprecated)/Algorithms/Nim Game/README.md similarity index 100% rename from Algorithms/Nim Game/README.md rename to old(deprecated)/Algorithms/Nim Game/README.md diff --git a/Algorithms/Nth Digit/README.md b/old(deprecated)/Algorithms/Nth Digit/README.md similarity index 100% rename from Algorithms/Nth Digit/README.md rename to old(deprecated)/Algorithms/Nth Digit/README.md diff --git a/Algorithms/Number Complement/README.md b/old(deprecated)/Algorithms/Number Complement/README.md similarity index 100% rename from Algorithms/Number Complement/README.md rename to old(deprecated)/Algorithms/Number Complement/README.md diff --git a/Algorithms/Number of 1 Bits/README.md b/old(deprecated)/Algorithms/Number of 1 Bits/README.md similarity index 100% rename from Algorithms/Number of 1 Bits/README.md rename to old(deprecated)/Algorithms/Number of 1 Bits/README.md diff --git a/Algorithms/Number of Boomerangs/README.md b/old(deprecated)/Algorithms/Number of Boomerangs/README.md similarity index 100% rename from Algorithms/Number of Boomerangs/README.md rename to old(deprecated)/Algorithms/Number of Boomerangs/README.md diff --git a/Algorithms/Number of Segments in a String/README.md b/old(deprecated)/Algorithms/Number of Segments in a String/README.md similarity index 100% rename from Algorithms/Number of Segments in a String/README.md rename to old(deprecated)/Algorithms/Number of Segments in a String/README.md diff --git a/Algorithms/Odd Even Linked List/README.md b/old(deprecated)/Algorithms/Odd Even Linked List/README.md similarity index 100% rename from Algorithms/Odd Even Linked List/README.md rename to old(deprecated)/Algorithms/Odd Even Linked List/README.md diff --git a/Algorithms/Output Contest Matches/README.md b/old(deprecated)/Algorithms/Output Contest Matches/README.md similarity index 100% rename from Algorithms/Output Contest Matches/README.md rename to old(deprecated)/Algorithms/Output Contest Matches/README.md diff --git a/Algorithms/Palindrome Linked List/README.md b/old(deprecated)/Algorithms/Palindrome Linked List/README.md similarity index 100% rename from Algorithms/Palindrome Linked List/README.md rename to old(deprecated)/Algorithms/Palindrome Linked List/README.md diff --git a/Algorithms/Palindrome Number/README.md b/old(deprecated)/Algorithms/Palindrome Number/README.md similarity index 100% rename from Algorithms/Palindrome Number/README.md rename to old(deprecated)/Algorithms/Palindrome Number/README.md diff --git a/Algorithms/Pascal's Triangle II/README.md b/old(deprecated)/Algorithms/Pascal's Triangle II/README.md similarity index 100% rename from Algorithms/Pascal's Triangle II/README.md rename to old(deprecated)/Algorithms/Pascal's Triangle II/README.md diff --git a/Algorithms/Pascal's Triangle/README.md b/old(deprecated)/Algorithms/Pascal's Triangle/README.md similarity index 100% rename from Algorithms/Pascal's Triangle/README.md rename to old(deprecated)/Algorithms/Pascal's Triangle/README.md diff --git a/Algorithms/Path Sum III/README.md b/old(deprecated)/Algorithms/Path Sum III/README.md similarity index 100% rename from Algorithms/Path Sum III/README.md rename to old(deprecated)/Algorithms/Path Sum III/README.md diff --git a/Algorithms/Path Sum/README.md b/old(deprecated)/Algorithms/Path Sum/README.md similarity index 100% rename from Algorithms/Path Sum/README.md rename to old(deprecated)/Algorithms/Path Sum/README.md diff --git a/Algorithms/Permutations/README.md b/old(deprecated)/Algorithms/Permutations/README.md similarity index 100% rename from Algorithms/Permutations/README.md rename to old(deprecated)/Algorithms/Permutations/README.md diff --git a/Algorithms/Plus One/README.md b/old(deprecated)/Algorithms/Plus One/README.md similarity index 100% rename from Algorithms/Plus One/README.md rename to old(deprecated)/Algorithms/Plus One/README.md diff --git a/Algorithms/Power of Four/README.md b/old(deprecated)/Algorithms/Power of Four/README.md similarity index 100% rename from Algorithms/Power of Four/README.md rename to old(deprecated)/Algorithms/Power of Four/README.md diff --git a/Algorithms/Power of Three/README.md b/old(deprecated)/Algorithms/Power of Three/README.md similarity index 100% rename from Algorithms/Power of Three/README.md rename to old(deprecated)/Algorithms/Power of Three/README.md diff --git a/Algorithms/Power of Two/README.md b/old(deprecated)/Algorithms/Power of Two/README.md similarity index 100% rename from Algorithms/Power of Two/README.md rename to old(deprecated)/Algorithms/Power of Two/README.md diff --git a/Algorithms/Product of Array Except Self/README.md b/old(deprecated)/Algorithms/Product of Array Except Self/README.md similarity index 100% rename from Algorithms/Product of Array Except Self/README.md rename to old(deprecated)/Algorithms/Product of Array Except Self/README.md diff --git a/Algorithms/Queue Reconstruction by Height/README.md b/old(deprecated)/Algorithms/Queue Reconstruction by Height/README.md similarity index 100% rename from Algorithms/Queue Reconstruction by Height/README.md rename to old(deprecated)/Algorithms/Queue Reconstruction by Height/README.md diff --git a/Algorithms/Ransom Note/README.md b/old(deprecated)/Algorithms/Ransom Note/README.md similarity index 100% rename from Algorithms/Ransom Note/README.md rename to old(deprecated)/Algorithms/Ransom Note/README.md diff --git a/Algorithms/Rectangle Area/README.md b/old(deprecated)/Algorithms/Rectangle Area/README.md similarity index 100% rename from Algorithms/Rectangle Area/README.md rename to old(deprecated)/Algorithms/Rectangle Area/README.md diff --git a/Algorithms/Relative Ranks/README.md b/old(deprecated)/Algorithms/Relative Ranks/README.md similarity index 100% rename from Algorithms/Relative Ranks/README.md rename to old(deprecated)/Algorithms/Relative Ranks/README.md diff --git a/Algorithms/Remove Duplicates from Sorted Array/README.md b/old(deprecated)/Algorithms/Remove Duplicates from Sorted Array/README.md similarity index 100% rename from Algorithms/Remove Duplicates from Sorted Array/README.md rename to old(deprecated)/Algorithms/Remove Duplicates from Sorted Array/README.md diff --git a/Algorithms/Remove Duplicates from Sorted List/README.md b/old(deprecated)/Algorithms/Remove Duplicates from Sorted List/README.md similarity index 100% rename from Algorithms/Remove Duplicates from Sorted List/README.md rename to old(deprecated)/Algorithms/Remove Duplicates from Sorted List/README.md diff --git a/Algorithms/Remove Element/README.md b/old(deprecated)/Algorithms/Remove Element/README.md similarity index 100% rename from Algorithms/Remove Element/README.md rename to old(deprecated)/Algorithms/Remove Element/README.md diff --git a/Algorithms/Remove Linked List Elements/README.md b/old(deprecated)/Algorithms/Remove Linked List Elements/README.md similarity index 100% rename from Algorithms/Remove Linked List Elements/README.md rename to old(deprecated)/Algorithms/Remove Linked List Elements/README.md diff --git a/Algorithms/Remove Nth Node From End of List/README.md b/old(deprecated)/Algorithms/Remove Nth Node From End of List/README.md similarity index 100% rename from Algorithms/Remove Nth Node From End of List/README.md rename to old(deprecated)/Algorithms/Remove Nth Node From End of List/README.md diff --git a/Algorithms/Repeated Substring Pattern/README.md b/old(deprecated)/Algorithms/Repeated Substring Pattern/README.md similarity index 100% rename from Algorithms/Repeated Substring Pattern/README.md rename to old(deprecated)/Algorithms/Repeated Substring Pattern/README.md diff --git a/Algorithms/Reverse Integer/README.md b/old(deprecated)/Algorithms/Reverse Integer/README.md similarity index 100% rename from Algorithms/Reverse Integer/README.md rename to old(deprecated)/Algorithms/Reverse Integer/README.md diff --git a/Algorithms/Reverse Linked List II/README.md b/old(deprecated)/Algorithms/Reverse Linked List II/README.md similarity index 100% rename from Algorithms/Reverse Linked List II/README.md rename to old(deprecated)/Algorithms/Reverse Linked List II/README.md diff --git a/Algorithms/Reverse Linked List/README.md b/old(deprecated)/Algorithms/Reverse Linked List/README.md similarity index 100% rename from Algorithms/Reverse Linked List/README.md rename to old(deprecated)/Algorithms/Reverse Linked List/README.md diff --git a/Algorithms/Reverse String/README.md b/old(deprecated)/Algorithms/Reverse String/README.md similarity index 100% rename from Algorithms/Reverse String/README.md rename to old(deprecated)/Algorithms/Reverse String/README.md diff --git a/Algorithms/Reverse Vowels of a String/README.md b/old(deprecated)/Algorithms/Reverse Vowels of a String/README.md similarity index 100% rename from Algorithms/Reverse Vowels of a String/README.md rename to old(deprecated)/Algorithms/Reverse Vowels of a String/README.md diff --git a/Algorithms/Reverse Words in a String/README.md b/old(deprecated)/Algorithms/Reverse Words in a String/README.md similarity index 100% rename from Algorithms/Reverse Words in a String/README.md rename to old(deprecated)/Algorithms/Reverse Words in a String/README.md diff --git a/Algorithms/Roman to Integer/README.md b/old(deprecated)/Algorithms/Roman to Integer/README.md similarity index 100% rename from Algorithms/Roman to Integer/README.md rename to old(deprecated)/Algorithms/Roman to Integer/README.md diff --git a/Algorithms/Rotate Array/README.md b/old(deprecated)/Algorithms/Rotate Array/README.md similarity index 100% rename from Algorithms/Rotate Array/README.md rename to old(deprecated)/Algorithms/Rotate Array/README.md diff --git a/Algorithms/Same Tree/README.md b/old(deprecated)/Algorithms/Same Tree/README.md similarity index 100% rename from Algorithms/Same Tree/README.md rename to old(deprecated)/Algorithms/Same Tree/README.md diff --git a/Algorithms/Search Insert Position/README.md b/old(deprecated)/Algorithms/Search Insert Position/README.md similarity index 100% rename from Algorithms/Search Insert Position/README.md rename to old(deprecated)/Algorithms/Search Insert Position/README.md diff --git a/Algorithms/Search in Rotated Sorted Array/README.md b/old(deprecated)/Algorithms/Search in Rotated Sorted Array/README.md similarity index 100% rename from Algorithms/Search in Rotated Sorted Array/README.md rename to old(deprecated)/Algorithms/Search in Rotated Sorted Array/README.md diff --git a/Algorithms/Set Matrix Zeroes/README.md b/old(deprecated)/Algorithms/Set Matrix Zeroes/README.md similarity index 100% rename from Algorithms/Set Matrix Zeroes/README.md rename to old(deprecated)/Algorithms/Set Matrix Zeroes/README.md diff --git a/Algorithms/Single Number/README.md b/old(deprecated)/Algorithms/Single Number/README.md similarity index 100% rename from Algorithms/Single Number/README.md rename to old(deprecated)/Algorithms/Single Number/README.md diff --git a/Algorithms/Sort List/README.md b/old(deprecated)/Algorithms/Sort List/README.md similarity index 100% rename from Algorithms/Sort List/README.md rename to old(deprecated)/Algorithms/Sort List/README.md diff --git a/Algorithms/Spiral Matrix/README.md b/old(deprecated)/Algorithms/Spiral Matrix/README.md similarity index 100% rename from Algorithms/Spiral Matrix/README.md rename to old(deprecated)/Algorithms/Spiral Matrix/README.md diff --git a/Algorithms/Sqrt(x)/README.md b/old(deprecated)/Algorithms/Sqrt(x)/README.md similarity index 100% rename from Algorithms/Sqrt(x)/README.md rename to old(deprecated)/Algorithms/Sqrt(x)/README.md diff --git a/Algorithms/String to Integer (atoi)/README.md b/old(deprecated)/Algorithms/String to Integer (atoi)/README.md similarity index 100% rename from Algorithms/String to Integer (atoi)/README.md rename to old(deprecated)/Algorithms/String to Integer (atoi)/README.md diff --git a/Algorithms/Sum of Left Leaves/README.md b/old(deprecated)/Algorithms/Sum of Left Leaves/README.md similarity index 100% rename from Algorithms/Sum of Left Leaves/README.md rename to old(deprecated)/Algorithms/Sum of Left Leaves/README.md diff --git a/Algorithms/Summary Ranges/README.md b/old(deprecated)/Algorithms/Summary Ranges/README.md similarity index 100% rename from Algorithms/Summary Ranges/README.md rename to old(deprecated)/Algorithms/Summary Ranges/README.md diff --git a/Algorithms/Swap Nodes in Pairs/README.md b/old(deprecated)/Algorithms/Swap Nodes in Pairs/README.md similarity index 100% rename from Algorithms/Swap Nodes in Pairs/README.md rename to old(deprecated)/Algorithms/Swap Nodes in Pairs/README.md diff --git a/Algorithms/Symmetric Tree/README.md b/old(deprecated)/Algorithms/Symmetric Tree/README.md similarity index 100% rename from Algorithms/Symmetric Tree/README.md rename to old(deprecated)/Algorithms/Symmetric Tree/README.md diff --git a/Algorithms/Teemo Attacking/README.md b/old(deprecated)/Algorithms/Teemo Attacking/README.md similarity index 100% rename from Algorithms/Teemo Attacking/README.md rename to old(deprecated)/Algorithms/Teemo Attacking/README.md diff --git a/Algorithms/Third Maximum Number/README.md b/old(deprecated)/Algorithms/Third Maximum Number/README.md similarity index 100% rename from Algorithms/Third Maximum Number/README.md rename to old(deprecated)/Algorithms/Third Maximum Number/README.md diff --git a/Algorithms/Top K Frequent Elements/README.md b/old(deprecated)/Algorithms/Top K Frequent Elements/README.md similarity index 100% rename from Algorithms/Top K Frequent Elements/README.md rename to old(deprecated)/Algorithms/Top K Frequent Elements/README.md diff --git a/Algorithms/Two Sum/README.md b/old(deprecated)/Algorithms/Two Sum/README.md similarity index 100% rename from Algorithms/Two Sum/README.md rename to old(deprecated)/Algorithms/Two Sum/README.md diff --git a/Algorithms/Ugly Number/README.md b/old(deprecated)/Algorithms/Ugly Number/README.md similarity index 100% rename from Algorithms/Ugly Number/README.md rename to old(deprecated)/Algorithms/Ugly Number/README.md diff --git a/Algorithms/Valid Anagram/README.md b/old(deprecated)/Algorithms/Valid Anagram/README.md similarity index 100% rename from Algorithms/Valid Anagram/README.md rename to old(deprecated)/Algorithms/Valid Anagram/README.md diff --git a/Algorithms/Valid Palindrome/README.md b/old(deprecated)/Algorithms/Valid Palindrome/README.md similarity index 100% rename from Algorithms/Valid Palindrome/README.md rename to old(deprecated)/Algorithms/Valid Palindrome/README.md diff --git a/Algorithms/Valid Parentheses/README.md b/old(deprecated)/Algorithms/Valid Parentheses/README.md similarity index 100% rename from Algorithms/Valid Parentheses/README.md rename to old(deprecated)/Algorithms/Valid Parentheses/README.md diff --git a/Algorithms/Valid Perfect Square/README.md b/old(deprecated)/Algorithms/Valid Perfect Square/README.md similarity index 100% rename from Algorithms/Valid Perfect Square/README.md rename to old(deprecated)/Algorithms/Valid Perfect Square/README.md diff --git a/Algorithms/Valid Sudoku/README.md b/old(deprecated)/Algorithms/Valid Sudoku/README.md similarity index 100% rename from Algorithms/Valid Sudoku/README.md rename to old(deprecated)/Algorithms/Valid Sudoku/README.md diff --git a/Algorithms/Validate Binary Search Tree/README.md b/old(deprecated)/Algorithms/Validate Binary Search Tree/README.md similarity index 100% rename from Algorithms/Validate Binary Search Tree/README.md rename to old(deprecated)/Algorithms/Validate Binary Search Tree/README.md diff --git a/Algorithms/ZigZag Conversion/README.md b/old(deprecated)/Algorithms/ZigZag Conversion/README.md similarity index 100% rename from Algorithms/ZigZag Conversion/README.md rename to old(deprecated)/Algorithms/ZigZag Conversion/README.md diff --git a/old(deprecated)/README.md b/old(deprecated)/README.md new file mode 100644 index 0000000..64c367a --- /dev/null +++ b/old(deprecated)/README.md @@ -0,0 +1,161 @@ +leetcode +======== +#### Total solved: 153 (Easy: 111 Medium: 41 Hard: 1 ) +My JavaScript Solution of [leetcode](http://oj.leetcode.com/problems/) + + +| No | Title | Source Code | Difficulty | +|----| ----- | -------- | ---------- | +|544|Output Contest Matches|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Output%20Contest%20Matches)|Medium| +|543|Diameter of Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Diameter%20of%20Binary%20Tree)|Easy| +|542|01 Matrix|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/01%20Matrix)|Medium| +|538|Convert BST to Greater Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Convert%20BST%20to%20Greater%20Tree)|Medium| +|532|K-diff Pairs in an Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/K-diff%20Pairs%20in%20an%20Array)|Easy| +|530|Minimum Absolute Difference in BST|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Minimum%20Absolute%20Difference%20in%20BST)|Easy| +|529|Minesweeper|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Minesweeper)|Medium| +|526|Beautiful Arrangement|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Beautiful%20Arrangement)|Medium| +|520|Detect Capital|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Detect%20Capital)|Easy| +|515|Find Largest Value in Each Tree Row|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Find%20Largest%20Value%20in%20Each%20Tree%20Row)|Medium| +|513|Find Bottom Left Tree Value|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Find%20Bottom%20Left%20Tree%20Value)|Medium| +|508|Most Frequent Subtree Sum|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Most%20Frequent%20Subtree%20Sum)|Medium| +|506|Relative Ranks|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Relative%20Ranks)|Easy| +|504|Base 7|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Base%207)|Easy| +|503|Next Greater Element II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Next%20Greater%20Element%20II)|Medium| +|501|Find Mode in Binary Search Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Find%20Mode%20in%20Binary%20Search%20Tree)|Easy| +|500|Keyboard Row|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Keyboard%20Row)|Easy| +|496|Next Greater Element I|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Next%20Greater%20Element%20I)|Easy| +|492|Construct the Rectangle|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Construct%20the%20Rectangle)|Easy| +|485|Max Consecutive Ones|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Max%20Consecutive%20Ones)|Easy| +|482|License Key Formatting|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/License%20Key%20Formatting)|Medium| +|476|Number Complement|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Number%20Complement)|Easy| +|463|Add Digits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Add%20Digits)|Easy| +|461|Hamming Distance|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Hamming%20Distance)|Easy| +|459|Repeated Substring Pattern|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Repeated%20Substring%20Pattern)|Easy| +|455|Assign Cookies|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Assign%20Cookies)|Easy| +|453|Minimum Moves to Equal Array Elements|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Minimum%20Moves%20to%20Equal%20Array%20Elements)|Easy| +|450|Delete Node in a BST|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Delete%20Node%20in%20a%20BST)|Medium| +|448|Find All Numbers Disappeared in an Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Find%20All%20Numbers%20Disappeared%20in%20an%20Array)|Easy| +|447|Number of Boomerangs|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Number%20of%20Boomerangs)|Easy| +|442|Find All Duplicates in an Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Find%20All%20Duplicates%20in%20an%20Array)|Medium| +|441|Arranging Coins|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Arranging%20Coins)|Easy| +|437|Path Sum III|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Path%20Sum%20III)|Easy| +|434|Number of Segments in a String|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Number%20of%20Segments%20in%20a%20String)|Easy| +|415|Add Strings|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Add%20Strings)|Easy| +|414|Third Maximum Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Third%20Maximum%20Number)|Easy| +|413|Arithmetic Slices|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Arithmetic%20Slices)|Medium| +|412|Fizz Buzz|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Fizz%20Buzz)|Easy| +|409|Longest Palindrome|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Longest%20Palindrome)|Easy| +|406|Queue Reconstruction by Height|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Queue%20Reconstruction%20by%20Height)|Medium| +|404|Sum of Left Leaves|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Sum%20of%20Left%20Leaves)|Easy| +|401|Binary Watch|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Binary%20Watch)|Easy| +|400|Nth Digit|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Nth%20Digit)|Easy| +|389|Find the Difference|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Find%20the%20Difference)|Easy| +|387|First Unique Character in a String|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/First%20Unique%20Character%20in%20a%20String)|Easy| +|383|Ransom Note|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Ransom%20Note)|Easy| +|374|Guess Number Higher or Lower|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Guess%20Number%20Higher%20or%20Lower)|Easy| +|367|Valid Perfect Square|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Perfect%20Square)|Easy| +|350|Intersection of Two Arrays II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Intersection%20of%20Two%20Arrays%20II)|Easy| +|349|Intersection of Two Arrays|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Intersection%20of%20Two%20Arrays)|Easy| +|347|Top K Frequent Elements|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Top%20K%20Frequent%20Elements)|Medium| +|345|Reverse Vowels of a String|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Vowels%20of%20a%20String)|Easy| +|344|Reverse String|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20String)|Easy| +|343|Integer Break|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Integer%20Break)|Medium| +|342|Power of Four|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Power%20of%20Four)|Easy| +|338|Counting Bits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Counting%20Bits)|Medium| +|328|Odd Even Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Odd%20Even%20Linked%20List)|Medium| +|326|Power of Three|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Power%20of%20Three)|Easy| +|319|Add Digits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Add%20Digits)|Medium| +|292|Nim Game|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Nim%20Game)|Easy| +|283|Move Zeroes|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Move%20Zeroes)|Easy| +|278|First Bad Version|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/First%20Bad%20Version)|Easy| +|268|Missing Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Missing%20Number)|Medium| +|263|Ugly Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Ugly%20Number)|Easy| +|258|Add Digits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Add%20Digits)|Easy| +|257|Binary Tree Paths|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Binary%20Tree%20Paths)|Easy| +|242|Valid Anagram|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Anagram)|Easy| +|238|Product of Array Except Self|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Product%20of%20Array%20Except%20Self)|Medium| +|237|Delete Node in a Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Delete%20Node%20in%20a%20Linked%20List)|Easy| +|235|Lowest Common Ancestor of a Binary Search Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree)|Easy| +|234|Palindrome Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Palindrome%20Linked%20List)|Easy| +|232|Implement Queue using Stacks|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Implement%20Queue%20using%20Stacks)|Easy| +|231|Power of Two|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Power%20of%20Two)|Easy| +|228|Summary Ranges|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Summary%20Ranges)|Easy| +|226|Invert Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Invert%20Binary%20Tree)|Easy| +|223|Rectangle Area|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Rectangle%20Area)|Easy| +|219|Contains Duplicate II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Contains%20Duplicate%20II)|Easy| +|217|Contains Duplicate|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Contains%20Duplicate)|Easy| +|206|Reverse Linked List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Linked%20List)|Easy| +|205|Isomorphic Strings|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Isomorphic%20Strings)|Easy| +|204|Count Primes|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Count%20Primes)|Easy| +|203|Remove Linked List Elements|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Linked%20List%20Elements)|Easy| +|202|Happy Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Happy%20Number)|Easy| +|198|House Robber|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/House%20Robber)|Easy| +|191|Number of 1 Bits|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Number%20of%201%20Bits)|Easy| +|189|Rotate Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Rotate%20Array)|Easy| +|179|Largest Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Largest%20Number)|Medium| +|172|Factorial Trailing Zeroes|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Factorial%20Trailing%20Zeroes)|Easy| +|171|Excel Sheet Column Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Excel%20Sheet%20Column%20Number)|Easy| +|169|Majority Element|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Majority%20Element)|Easy| +|168|Excel Sheet Column Title|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Excel%20Sheet%20Column%20Title)|Easy| +|165|Compare Version Numbers|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Compare%20Version%20Numbers)|Easy| +|160|Intersection of Two Linked Lists|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Intersection%20of%20Two%20Linked%20Lists)|Easy| +|155|Min Stack|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Min%20Stack)|Easy| +|151|Reverse Words in a String|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Words%20in%20a%20String)|Easy| +|150|Evaluate Reverse Polish Notation|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Evaluate%20Reverse%20Polish%20Notation)|Medium| +|149|Max Points on a Line|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Max%20Points%20on%20a%20Line)|Hard| +|148|Sort List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Sort%20List)|Medium| +|144|Binary Tree Preorder Traversal|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Binary%20Tree%20Preorder%20Traversal)|Medium| +|141|Linked List Cycle|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Linked%20List%20Cycle)|Easy| +|138|Copy List with Random Pointer|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Copy%20List%20with%20Random%20Pointer)|Medium| +|136|Single Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Single%20Number)|Medium| +|125|Valid Palindrome|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Palindrome)|Easy| +|122|Best Time to Buy and Sell Stock II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Best%20Time%20to%20Buy%20and%20Sell%20Stock%20II)|Medium| +|121|Best Time to Buy and Sell Stock|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Best%20Time%20to%20Buy%20and%20Sell%20Stock)|Easy| +|119|Pascal's Triangle II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Pascal's%20Triangle%20II)|Easy| +|118|Pascal's Triangle|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Pascal's%20Triangle)|Easy| +|112|Path Sum|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Path%20Sum)|Easy| +|111|Minimum Depth of Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Minimum%20Depth%20of%20Binary%20Tree)|Easy| +|110|Balanced Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Balanced%20Binary%20Tree)|Easy| +|108|Convert Sorted Array to Binary Search Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Convert%20Sorted%20Array%20to%20Binary%20Search%20Tree)|Easy| +|107|Binary Tree Level Order Traversal II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Binary%20Tree%20Level%20Order%20Traversal%20II)|Easy| +|104|Maximum Depth of Binary Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Maximum%20Depth%20of%20Binary%20Tree)|Easy| +|102|Binary Tree Level Order Traversal|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Binary%20Tree%20Level%20Order%20Traversal)|Easy| +|101|Symmetric Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Symmetric%20Tree)|Easy| +|100|Same Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Same%20Tree)|Easy| +|98|Validate Binary Search Tree|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Validate%20Binary%20Search%20Tree)|Medium| +|92|Reverse Linked List II|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Linked%20List%20II)|Medium| +|88|Merge Sorted Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Merge%20Sorted%20Array)|Easy| +|83|Remove Duplicates from Sorted List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Duplicates%20from%20Sorted%20List)|Easy| +|73|Set Matrix Zeroes|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Set%20Matrix%20Zeroes)|Medium| +|70|Climbing Stairs|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Climbing%20Stairs)|Easy| +|69|Sqrt(x)|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Sqrt(x))|Easy| +|66|Plus One|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Plus%20One)|Easy| +|58|Length of Last Word|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Length%20of%20Last%20Word)|Easy| +|54|Spiral Matrix|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Spiral%20Matrix)|Medium| +|53|Maximum Subarray|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Maximum%20Subarray)|Easy| +|46|Permutations|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Permutations)|Medium| +|38|Count and Say|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Count%20and%20Say)|Easy| +|36|Valid Sudoku|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Sudoku)|Easy| +|35|Search Insert Position|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Search%20Insert%20Position)|Easy| +|33|Search in Rotated Sorted Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Search%20in%20Rotated%20Sorted%20Array)|Medium| +|31|Next Permutation|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Next%20Permutation)|Medium| +|29|Divide Two Integers|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Divide%20Two%20Integers)|Medium| +|28|Implement strStr()|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Implement%20strStr())|Easy| +|27|Remove Element|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Element)|Easy| +|26|Remove Duplicates from Sorted Array|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Duplicates%20from%20Sorted%20Array)|Easy| +|24|Swap Nodes in Pairs|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Swap%20Nodes%20in%20Pairs)|Easy| +|22|Generate Parentheses|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Generate%20Parentheses)|Medium| +|21|Merge Two Sorted Lists|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Merge%20Two%20Sorted%20Lists)|Easy| +|20|Valid Parentheses|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Valid%20Parentheses)|Easy| +|19|Remove Nth Node From End of List|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Remove%20Nth%20Node%20From%20End%20of%20List)|Easy| +|17|Letter Combinations of a Phone Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Letter%20Combinations%20of%20a%20Phone%20Number)|Medium| +|16|3Sum Closest|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/3Sum%20Closest)|Medium| +|15|3Sum|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/3Sum)|Medium| +|14|Longest Common Prefix|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Longest%20Common%20Prefix)|Easy| +|13|Roman to Integer|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Roman%20to%20Integer)|Easy| +|12|Integer to Roman|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Integer%20to%20Roman)|Medium| +|9|Palindrome Number|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Palindrome%20Number)|Easy| +|8|String to Integer (atoi)|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/String%20to%20Integer%20(atoi))|Easy| +|7|Reverse Integer|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Reverse%20Integer)|Easy| +|6|ZigZag Conversion|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/ZigZag%20Conversion)|Easy| +|1|Two Sum|[JavaScript](https://github.com/duteng/leetcode/tree/master/Algorithms/Two%20Sum)|Easy| diff --git a/generate-md.js b/old(deprecated)/generate-md.js similarity index 100% rename from generate-md.js rename to old(deprecated)/generate-md.js diff --git a/package.json b/old(deprecated)/package.json similarity index 100% rename from package.json rename to old(deprecated)/package.json diff --git a/request-problem.js b/old(deprecated)/request-problem.js similarity index 100% rename from request-problem.js rename to old(deprecated)/request-problem.js