├── .gitignore ├── 001-Two-Sum.js ├── 002-Add-Two-Numbers.js ├── 003-Longest-Substring-Without-Repeating-Characters.js ├── 004-Median-of-Two-Sorted-Arrays.js ├── 005-Longest-Palindromic-Substring.js ├── 006-ZigZag-Conversion.js ├── 007-Reverse-Integer.js ├── 009-Palindrome-Number.js ├── 010-Regular-Expression-Matching.js ├── 011-Container-With-Most-Water.js ├── 013-Roman-to-Integer.js ├── 014-Longest-Common-Prefix.js ├── 015-3Sum.js ├── 016-3Sum-Closest.js ├── 017-Letter-Combinations-of-a-Phone-Number.js ├── 018-4Sum.js ├── 019-Remove-Nth-Node-From-End-of-List.js ├── 020-Valid-Parentheses.js ├── 021-Merge-Two-Sorted-Lists.js ├── 022-Generate-Parentheses.js ├── 023-Merge-k-Sorted-Lists.js ├── 024-Swap-Nodes-in-Pairs.js ├── 025-Reverse-Nodes-in-k-Group.js ├── 026-Remove-Duplicates-from-Sorted-Array.js ├── 027-Remove-Element.js ├── 030-Substring-with-Concatenation-of-All-Words.js ├── 031-Next-Permutation.js ├── 032-Longest-Valid-Parentheses.js ├── 033-Search-in-Rotated-Sorted-Array.js ├── 034-Search-for-a-Range.js ├── 035-Search-Insert-Position.js ├── 036-Valid-Sudoku.js ├── 037-Sudoku-Solver.js ├── 038-Count-and-Say.js ├── 039-Combination-Sum.js ├── 040-Combination-Sum-II.js ├── 041-First-Missing-Positive.js ├── 043-Multiply-Strings.js ├── 046-Permutations.js ├── 047-Permutations-II.js ├── 051-N-Queens.js ├── 053-Maximum-Subarray.js ├── 062-Unique-Paths.js ├── 063-Unique-Paths-II.js ├── 064-Minimum-Path-Sum.js ├── 067-Add-Binary.js ├── 070-Climbing-Stairs.js ├── 083-Remove-Duplicates-from-Sorted-List.js ├── 085-Maximal-Rectangle.js ├── 091-Decode-Ways.js ├── 095-Unique-Binary-Search-Trees-II.js ├── 096-Unique-Binary-Search-Trees.js ├── 100-Same-Tree.js ├── 101-Symmetric-Tree.js ├── 111-Minimum-Depth-of-Binary-Tree.js ├── 120-Triangle.js ├── 121-Best-Time-to-Buy-and-Sell-Stock.js ├── 136-Single-Number.js ├── 138-Copy-List-with-Random-Pointer.js ├── 139-Word-Break.js ├── 151-Reverse-Words-in-a-String.js ├── 152-Maximum-Product-Subarray.js ├── 162-Find-Peak-Element.js ├── 167-Two-Sum-II-Input-array-is-sorted.js ├── 171-Excel-Sheet-Column-Number.js ├── 190-Reverse-Bits.js ├── 191-Number-of-1-Bits.js ├── 198-House-Robber.js ├── 202-Happy-Number.js ├── 204-Count-Primes.js ├── 217-Contains-Duplicate.js ├── 226-Invert-Binary-Tree.js ├── 227-Basic-Calculator-II.js ├── 258-Add-Digits.js ├── 263-Ugly-Number.js ├── 268-Missing-Number.js ├── 283-Move-Zeroes.js ├── 300-Longest-Increasing-Subsequence.js ├── 347-Top-K-Frequent-Elements.js ├── 349-Intersection-of-Two-Arrays.js ├── 383-Ransom-Note.js ├── 387-First-Unique-Character-in-a-String.js ├── 392-Is-Subsequence.js ├── 404-Sum-of-Left-Leaves.js ├── 413-Arithmetic-Slices.js ├── 415-Add-Strings.js ├── 416-Partition-Equal-Subset-Sum.js ├── 447-Number-of-Boomerangs.js ├── 448-Find-All-Numbers-Disappeared-in-an-Array.js ├── 453-Minimum-Moves-to-Equal-Array-Elements.js ├── 454-4Sum-II.js ├── 455-Assign-Cookies.js ├── 461-Hamming-Distance.js ├── 476-Number-Complement.js ├── 477-Total-Hamming-Distance.js ├── 492-Construct-the-Rectangle.js ├── 500-Keyboard-Row.js ├── 504-Base-7.js ├── 506-Relative-Ranks.js ├── 520-Detect-Capital.js ├── 523-Continuous-Subarray-Sum.js ├── 557-Reverse-Words-in-a-String-III.js ├── 561-Array-Partition-I.js ├── 617-Merge-Two-Binary-Trees.js ├── 646-Maximum-Length-of-Pair-Chain.js ├── 647-Palindromic-Substrings.js ├── 654-Maximum-Binary-Tree.js ├── 657-Judge-Route-Circle.js ├── 673-Number-of-Longest-Increasing-Subsequence.js ├── 693-Binary-Number-with-Alternating-Bits.js ├── 695-Max-Area-of-Island.js ├── 718-Maximum-Length-of-Repeated-Subarray.js ├── 728-Self-Dividing-Numbers.js ├── README.md └── pack ├── 01.js ├── 02.js └── 03.js /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .DS_Store 3 | \** 4 | file.js -------------------------------------------------------------------------------- /001-Two-Sum.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/two-sum/description/ 3 | * Difficulty:Easy 4 | * 5 | * Given an array of integers, return indices of the two numbers such that they add up to a specific target. 6 | * You may assume that each input would have exactly one solution, and you may not use the same element twice. 7 | * Example: 8 | * Given nums = [2, 7, 11, 15], target = 9, 9 | * Because nums[0] + nums[1] = 2 + 7 = 9, 10 | * return [0, 1]. 11 | */ 12 | 13 | /** 14 | * @param {number[]} numbers 15 | * @param {number} target 16 | * @return {number[]} 17 | */ 18 | var twoSum = function (numbers, target) { 19 | 20 | for (var i = 0; i < numbers.length - 1; i++) { 21 | for (var j = i + 1; j < numbers.length; j++) { 22 | if (numbers[i] + numbers[j] === target) return [i, j]; 23 | } 24 | } 25 | }; 26 | 27 | var twoSum2 = function (numbers, target) { 28 | var map = {}; 29 | for (var i = 0; i < numbers.length; i++) { 30 | var n = numbers[i]; 31 | if (map[target - n] !== undefined) { 32 | return [map[target - n], i]; 33 | } else { 34 | map[n] = i; 35 | } 36 | } 37 | }; 38 | 39 | console.log(twoSum([2, 11, 15, 7], 9)); 40 | console.log(twoSum2([2, 7, 11, 15], 9)); 41 | console.log(twoSum2([2, 7, 11, 15], 26)); 42 | console.log(twoSum2([2, 7, 11, 15], 26)); 43 | 44 | -------------------------------------------------------------------------------- /002-Add-Two-Numbers.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/add-two-numbers/description/ 3 | * Difficulty:Medium 4 | * 5 | * You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. 6 | * You may assume the two numbers do not contain any leading zero, except the number 0 itself. 7 | * Example 8 | * Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) 9 | * Output: 7 -> 0 -> 8 10 | * Explanation: 342 + 465 = 807. 11 | */ 12 | 13 | // Definition for singly-linked list. 14 | function ListNode(val) { 15 | this.val = val; 16 | this.next = null; 17 | } 18 | 19 | /** 20 | * @param {ListNode} l1 21 | * @param {ListNode} l2 22 | * @return {ListNode} 23 | */ 24 | var addTwoNumbers = function (l1, l2) { 25 | 26 | var c = 0; 27 | var ret = new ListNode(0); 28 | var curr = ret; 29 | 30 | while (l1 || l2) { 31 | var a = l1 ? l1.val : 0; 32 | var b = l2 ? l2.val : 0; 33 | var sum = a + b + c; 34 | c = Math.floor(sum / 10); 35 | curr.next = new ListNode(sum % 10); 36 | if (l1) { 37 | l1 = l1.next; 38 | } 39 | if (l2) { 40 | l2 = l2.next; 41 | } 42 | curr = curr.next; 43 | } 44 | if (c) { 45 | curr.next = new ListNode(c); 46 | } 47 | 48 | return ret.next; 49 | }; 50 | -------------------------------------------------------------------------------- /003-Longest-Substring-Without-Repeating-Characters.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/longest-substring-without-repeating-characters/description/ 3 | * Difficulty:Medium 4 | * 5 | * Given a string, find the length of the longest substring without repeating characters. 6 | * Examples: 7 | * Given "abcabcbb", the answer is "abc", which the length is 3. 8 | * Given "bbbbb", the answer is "b", with the length of 1. 9 | * Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring. 10 | * 11 | */ 12 | 13 | /** 14 | * @param {string} s 15 | * @return {number} 16 | */ 17 | var lengthOfLongestSubstring = function (s) { 18 | 19 | var max = 0; 20 | var i = 0; 21 | var j = 0; 22 | var n = s.length; 23 | var map = {}; 24 | 25 | while (i < n && j < n) { 26 | if (map[s[j]] === undefined) { 27 | map[s[j]] = 1; 28 | j++; 29 | max = Math.max(max, j - i); 30 | } else { 31 | delete map[s[i]]; 32 | i++; 33 | } 34 | 35 | } 36 | 37 | return max; 38 | }; 39 | 40 | console.log(lengthOfLongestSubstring('c'), 1); 41 | console.log(lengthOfLongestSubstring(''), 0); 42 | console.log(lengthOfLongestSubstring('abcabcbb'), 3); 43 | console.log(lengthOfLongestSubstring('bbbbb'), 1); 44 | console.log(lengthOfLongestSubstring('pwwkew'), 3); 45 | console.log(lengthOfLongestSubstring('xhhyccrcbdczkvzeeubynglxfdedshtpobqsdhufkzgwuhaabdzrlkosnuxibrxssnkxuhcggkecshdvkcmymdqbxolbfjtzyfw'), 14); -------------------------------------------------------------------------------- /004-Median-of-Two-Sorted-Arrays.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/median-of-two-sorted-arrays/description/ 3 | * Difficulty:Hard 4 | * 5 | * There are two sorted arrays nums1 and nums2 of size m and n respectively. 6 | * Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 7 | * 8 | * Example 1: 9 | * nums1 = [1, 3] 10 | * nums2 = [2] 11 | * The median is 2.0 12 | * 13 | * Example 2: 14 | * nums1 = [1, 2] 15 | * nums2 = [3, 4] 16 | * The median is (2 + 3)/2 = 2.5 17 | * * 18 | */ 19 | 20 | 21 | function kth(arr1, s1, n1, arr2, s2, n2, k) { 22 | // console.log(arr1, s1, n1, arr2, s2, n2, k); 23 | // console.log('-----------'); 24 | if (k < 1 || k > n1 + n2) return -1; 25 | 26 | if (n1 > n2) { 27 | return kth(arr2, s2, n2, arr1, s1, n1, k); 28 | } 29 | 30 | if (n1 === 0) { 31 | return arr2[s2 + k - 1]; 32 | } 33 | 34 | if (k === 1) { 35 | return arr1[s1] < arr2[s2] ? arr1[s1] : arr2[s2]; 36 | } 37 | 38 | var newK = k >> 1; 39 | 40 | if (n1 < newK) { 41 | newK = n1; 42 | } 43 | 44 | if (arr1[s1 + newK - 1] < arr2[s2 + newK - 1]) { 45 | return kth(arr1, s1 + newK, n1 - newK, arr2, s2, n2, k - newK); 46 | } else { 47 | return kth(arr1, s1, n1, arr2, s2 + newK, n2 - newK, k - newK); 48 | } 49 | 50 | } 51 | 52 | // var arr1 = [2, 3, 6, 7, 9]; 53 | // var arr2 = [1, 4, 8, 10]; 54 | // console.log([...arr1, ...arr2].sort(function (a, b) { 55 | // if (a > b) return 1; 56 | // if (a < b) return -1; 57 | // return 0; 58 | // })); 59 | // 60 | // console.log('======='); 61 | // console.log(kth(arr1, 0, 5, arr2, 0, 4, 1), 1); 62 | // console.log(kth(arr1, 0, 5, arr2, 0, 4, 2), 2); 63 | // console.log(kth(arr1, 0, 5, arr2, 0, 4, 3), 3); 64 | // console.log(kth(arr1, 0, 5, arr2, 0, 4, 4), 4); 65 | // console.log(kth(arr1, 0, 5, arr2, 0, 4, 5), 6); 66 | // console.log(kth(arr1, 0, 5, arr2, 0, 4, 6), 7); 67 | // console.log(kth(arr1, 0, 5, arr2, 0, 4, 7), 8); 68 | // console.log(kth(arr1, 0, 5, arr2, 0, 4, 8), 9); 69 | // console.log(kth(arr1, 0, 5, arr2, 0, 4, 9), 10); 70 | 71 | /** 72 | * @param {number[]} nums1 73 | * @param {number[]} nums2 74 | * @return {number} 75 | */ 76 | var findMedianSortedArrays = function (nums1, nums2) { 77 | 78 | var n1 = nums1.length; 79 | var n2 = nums2.length; 80 | 81 | var mid = Math.floor((n1 + n2) / 2); 82 | if ((n1 + n2) % 2 === 0) { 83 | return (kth(nums1, 0, n1, nums2, 0, n2, mid) + kth(nums1, 0, n1, nums2, 0, n2, mid + 1)) / 2; 84 | } else { 85 | return kth(nums1, 0, n1, nums2, 0, n2, mid + 1); 86 | } 87 | }; 88 | 89 | console.log(findMedianSortedArrays([1, 3, 4], [2, 5])); 90 | console.log(findMedianSortedArrays([1, 3, 4], [2, 5, 6])); -------------------------------------------------------------------------------- /005-Longest-Palindromic-Substring.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/longest-palindromic-substring/description/ 3 | * Difficulty:Medium 4 | * 5 | * Given a string s, find the longest palindromic substring in s. 6 | * You may assume that the maximum length of s is 1000. 7 | * 8 | * Example: 9 | * Input: "babad" 10 | * Output: "bab" 11 | * Note: "aba" is also a valid answer. 12 | * 13 | * Example: 14 | * Input: "cbbd" 15 | * Output: "bb" 16 | */ 17 | /** 18 | * @param {string} s 19 | * @return {string} 20 | */ 21 | var longestPalindrome = function (s) { 22 | var a = new Date(); 23 | var n = s.length; 24 | var res = ''; 25 | var dp = []; 26 | while (dp.push(new Array(n).fill(-1)) < n); 27 | // console.log(dp); 28 | 29 | for (var i = n - 1; i >= 0; i--) { 30 | for (var j = i; j < n; j++) { 31 | dp[i][j] = s[i] === s[j] && ((j - i < 3) || dp[i + 1][j - 1]); 32 | if (dp[i][j] === undefined) { 33 | console.log(i, j, s[i], s[j], dp[i + 1][j - 1]) 34 | } 35 | if (dp[i][j]) { 36 | var tmp = s.substring(i, j + 1); 37 | if (tmp.length > res.length) res = tmp; 38 | } 39 | 40 | } 41 | } 42 | // console.log(dp); 43 | console.log(new Date() - a); 44 | 45 | return res; 46 | }; 47 | 48 | // console.log(isPalindrome(s, 1, 3)); 49 | // console.log(longestPalindrome('babad')); 50 | // console.log(longestPalindrome('')); 51 | // console.log(longestPalindrome('a')); 52 | // console.log(longestPalindrome('aabbbbbb')); 53 | console.log(longestPalindrome("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabcaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); -------------------------------------------------------------------------------- /006-ZigZag-Conversion.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * https://leetcode.com/problems/zigzag-conversion/description/ 4 | * Difficulty:Medium 5 | * 6 | * The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: 7 | * (you may want to display this pattern in a fixed font for better legibility) 8 | * 9 | * P A H N 10 | * A P L S I I G 11 | * Y I R 12 | * And then read line by line: "PAHNAPLSIIGYIR" 13 | * Write the code that will take a string and make this conversion given a number of rows: 14 | * string convert(string text, int nRows); 15 | * convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR". 16 | * 17 | */ 18 | 19 | /** 20 | * @param {string} s 21 | * @param {number} numRows 22 | * @return {string} 23 | */ 24 | var convert = function (s, numRows) { 25 | 26 | var arr = []; 27 | for (var i = 0; i < numRows; i++) { 28 | arr[i] = []; 29 | } 30 | 31 | var cnt = 0; 32 | var len = s.length; 33 | 34 | while (cnt < len) { 35 | for (var i = 0; i < arr.length && cnt < len; i++) { 36 | arr[i].push(s[cnt++]); 37 | } 38 | for (var i = numRows - 2; i >= 1 && cnt < len; i--) { 39 | arr[i].push(s[cnt++]); 40 | } 41 | } 42 | // console.log(arr); 43 | 44 | return arr.map(arr => arr.join('')).join('') 45 | }; 46 | 47 | console.log(convert('PAYPALISHIRING', 3)); -------------------------------------------------------------------------------- /007-Reverse-Integer.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/reverse-integer/description/ 3 | * Difficulty:Easy 4 | * 5 | * Given a 32-bit signed integer, reverse digits of an integer. 6 | * 7 | * Example 1: 8 | * Input: 123 9 | * Output: 321 10 | * Example 2: 11 | * Input: -123 12 | * Output: -321 13 | * Example 3: 14 | * Input: 120 15 | * Output: 21 16 | * Note: 17 | * Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. 18 | * For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. 19 | */ 20 | 21 | /** 22 | * @param {number} x 23 | * @return {number} 24 | */ 25 | var reverse = function (x) { 26 | 27 | var sign = x >= 0 ? -1 : 1; 28 | x = Math.abs(x); 29 | 30 | var sum = 0; 31 | while (x) { 32 | sum = sum * 10 + x % 10; 33 | x = Math.floor(x / 10); 34 | } 35 | var ret = sign * -1 * sum; 36 | var max = Math.pow(2, 31) - 1; 37 | var min = -Math.pow(2, 31); 38 | if (ret > max) return 0; 39 | if (ret < min) return 0; 40 | return ret; 41 | }; 42 | 43 | console.log(reverse(123) === 321); 44 | console.log(reverse(-123) === -321); 45 | console.log(reverse(120) === 21); 46 | console.log(reverse(1534236469) === 0); 47 | console.log(reverse(-2147483412), reverse(-2147483412) === -2143847412); 48 | console.log(reverse(-2147483648), reverse(-2147483648) === 0); 49 | 50 | -------------------------------------------------------------------------------- /009-Palindrome-Number.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/palindrome-number/description/ 3 | * Difficulty:Easy 4 | * 5 | * Determine whether an integer is a palindrome. Do this without extra space. 6 | */ 7 | 8 | /** 9 | * @param {number} x 10 | * @return {boolean} 11 | */ 12 | var isPalindrome = function (x) { 13 | if (x < 0) return false; 14 | var t = x; 15 | x = Math.abs(x); 16 | var p = 0; 17 | while (x) { 18 | p = p * 10 + x % 10; 19 | x = Math.floor(x / 10); 20 | } 21 | // console.log(x, p); 22 | return t === p; 23 | }; 24 | 25 | console.log(isPalindrome(-1) === false); 26 | console.log(isPalindrome(0) === true); 27 | console.log(isPalindrome(123) === false); 28 | console.log(isPalindrome(12321) === true); 29 | console.log(isPalindrome(1221) === true); 30 | console.log(isPalindrome(2222) === true); 31 | 32 | -------------------------------------------------------------------------------- /010-Regular-Expression-Matching.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/regular-expression-matching/description/ 3 | * Difficulty:Hard 4 | * 5 | * implement regular expression matching with support for '.' and '*'. 6 | * 7 | * '.' Matches any single character. 8 | * '*' Matches zero or more of the preceding element. 9 | * The matching should cover the entire input string (not partial). 10 | * 11 | * The function prototype should be: 12 | * bool isMatch(const char *s, const char *p) 13 | * 14 | * Some examples: 15 | * isMatch("aa","a") → false 16 | * isMatch("aa","aa") → true 17 | * isMatch("aaa","aa") → false 18 | * isMatch("aa", "a*") → true 19 | * isMatch("aa", ".*") → true 20 | * isMatch("ab", ".*") → true 21 | * isMatch("aab", "c*a*b") → true 22 | */ 23 | 24 | /** 25 | * 26 | * 解题思路 27 | * 28 | * 动态规划 29 | * 注意体感要求, isMatch 为全匹配 30 | * 例子中的 isMatch("aab", "c*a*b") -> true 31 | * 是因为匹配 0个c, 2个a, 一个b 32 | * 33 | * @param {string} s 34 | * @param {string} p 35 | * @return {boolean} 36 | */ 37 | var isMatch = function (s, p) { 38 | 39 | var dp = []; 40 | var m = s.length; 41 | var n = p.length; 42 | 43 | for (var i = 0; i <= m; i++) { 44 | dp.push(new Array(n + 1).fill(false)); 45 | } 46 | dp[0][0] = true; 47 | 48 | for (var i = 0; i <= m; i++) { 49 | for (var j = 1; j <= n; j++) { 50 | if (p[j - 1] === '*') { 51 | // isMatch('a', 'a.*') 52 | // 如果j-1是*, 那么j-2可以出现0次; 53 | // 所以可以直接看 dp[i][j-2] 54 | dp[i][j] = dp[i][j - 2] || 55 | // isMatch('aa', 'aa*') 56 | (i > 0 && (s[i - 1] === p[j - 2] || p[j - 2] === '.') && dp[i - 1][j]); 57 | } else { 58 | dp[i][j] = i > 0 && dp[i - 1][j - 1] && 59 | (s[i - 1] === p[j - 1] || p[j - 1] === '.'); 60 | } 61 | } 62 | } 63 | 64 | console.log(dp.map(a => a.map(a => a ? 1 : 0))); 65 | return dp[m][n]; 66 | }; 67 | 68 | // console.log(isMatch("aa", "a"), '→', false) 69 | // console.log(isMatch("aa", "aa"), '→', true) 70 | // console.log(isMatch("aaa", "aa"), '→', false) 71 | // console.log(isMatch("aa", "a*"), '→', true) 72 | // console.log(isMatch("aa", ".*"), '→', true) 73 | // console.log(isMatch("ab", ".*"), '→', true) 74 | // console.log(isMatch("aab", "c*a*b"), '→', true) 75 | 76 | console.log(isMatch("a", "a.*")); 77 | // console.log(isMatch("aasdfasdfasdfasdfas", "aasdf.*asdf.*asdf.*asdf.*s")); 78 | -------------------------------------------------------------------------------- /013-Roman-to-Integer.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/roman-to-integer/description/ 3 | * Difficulty:Easy 4 | * 5 | * Given a roman numeral, convert it to an integer. 6 | * Input is guaranteed to be within the range from 1 to 3999. 7 | */ 8 | 9 | /** 10 | * @see https://baike.baidu.com/item/%E7%BD%97%E9%A9%AC%E6%95%B0%E5%AD%97 11 | * 12 | * 基本字符 13 | * I V X L C D M 14 | * 1 5 10 50 100 500 1000 15 | * 相应的阿拉伯数字表示 16 | * 17 | * 计数方法 18 | * 相同的数字连写、所表示的数等于这些数字相加得到的数、如:Ⅲ=3; 19 | * 小的数字在大的数字的右边、所表示的数等于这些数字相加得到的数、 如:Ⅷ=8、Ⅻ=12; 20 | * 小的数字(限于 I、X 和 C)在大的数字的左边、所表示的数等于大数减小数得到的数、如:Ⅳ=4、Ⅸ=9; 21 | * 正常使用时、连写的数字重复不得超过三次; 22 | * 23 | * @param {string} s 24 | * @return {number} 25 | */ 26 | var romanToInt = function (s) { 27 | 28 | if (!s) return 0; 29 | 30 | var map = { 31 | 'I': 1, 32 | 'V': 5, 33 | 'X': 10, 34 | 'L': 50, 35 | 'C': 100, 36 | 'D': 500, 37 | 'M': 1000 38 | }; 39 | 40 | var sum = map[s[s.length - 1]]; 41 | for (var i = s.length - 2; i >= 0; i--) { 42 | if (map[s[i]] < map[s[i + 1]]) sum -= map[s[i]]; 43 | else sum += map[s[i]]; 44 | } 45 | return sum; 46 | }; 47 | 48 | console.log(romanToInt('III'), 3); 49 | console.log(romanToInt('VI'), 6); 50 | console.log(romanToInt('IV'), 4); -------------------------------------------------------------------------------- /014-Longest-Common-Prefix.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/longest-common-prefix/ 3 | * Difficulty:Easy 4 | * 5 | * Write a function to find the longest common prefix string amongst an array of strings. 6 | */ 7 | 8 | /** 9 | * @param {string[]} strs 10 | * @return {string} 11 | */ 12 | var longestCommonPrefix = function (strs) { 13 | 14 | var m = strs.length; 15 | if (!m) return ''; 16 | 17 | var min = Infinity; 18 | var minIndex = -1; 19 | for (var i = 0; i < strs.length; i++) { 20 | if (strs[i].length < min) { 21 | min = strs[i].length; 22 | minIndex = i; 23 | } 24 | } 25 | 26 | var s = strs[minIndex]; 27 | 28 | for (i = 0; i < s.length; i++) { 29 | var ch = strs[0][i]; 30 | var same = true; 31 | for (var j = 1; j < m; j++) { 32 | if (strs[j][i] !== ch) { 33 | same = false; 34 | break; 35 | } 36 | 37 | } 38 | if (!same) break; 39 | 40 | } 41 | 42 | return s.substr(0, i); 43 | 44 | }; 45 | 46 | console.log(longestCommonPrefix([ 47 | 'abdc', 48 | 'abc123', 49 | 'abc234' 50 | ])); -------------------------------------------------------------------------------- /015-3Sum.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/3sum/ 3 | * Difficulty:Medium 4 | * 5 | * Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? 6 | * Find all unique triplets in the array which gives the sum of zero. 7 | * Note: The solution set must not contain duplicate triplets. 8 | * 9 | * For example, given array S = [-1, 0, 1, 2, -1, -4], 10 | * A solution set is: 11 | * [ 12 | * [-1, 0, 1], 13 | * [-1, -1, 2] 14 | * ] 15 | 16 | */ 17 | /** 18 | * @param {number[]} nums 19 | * @return {number[][]} 20 | */ 21 | var threeSum = function (nums) { 22 | 23 | nums.sort(function (a, b) { 24 | if (a > b) return 1; 25 | if (a === b) return 0; 26 | if (a < b) return -1; 27 | }); 28 | // console.log(nums); 29 | var ret = []; 30 | for (var i = 0; i < nums.length - 2; i++) { 31 | var a = nums[i]; 32 | if (i === 0 || (i > 0 && nums[i] !== nums[i - 1])) { 33 | 34 | var j = i + 1; 35 | var k = nums.length - 1; 36 | while (j < k) { 37 | var b = nums[j]; 38 | var c = nums[k]; 39 | 40 | var sum = a + b + c; 41 | 42 | // console.log(a, b, c, '=', sum); 43 | if (sum > 0) k--; 44 | else if (sum === 0) { 45 | ret.push([a, b, c]); 46 | while (j < k && nums[j] === nums[++j]); 47 | while (j < k && nums[k] === nums[--k]); 48 | // j++; 49 | // k--; 50 | 51 | } 52 | else j++; 53 | } 54 | } 55 | 56 | } 57 | return ret; 58 | 59 | }; 60 | 61 | console.log(threeSum([-2, 0, 0, 2, 2])); 62 | console.log(threeSum([-1, 0, 1, 2, -1, -4])); 63 | // console.log(threeSum([0, 0, 0, 0])); 64 | // console.log(threeSum([1, -1, -1, 0])); 65 | -------------------------------------------------------------------------------- /016-3Sum-Closest.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/3sum-closest/description/ 3 | * Difficulty:Medium 4 | * 5 | * Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. 6 | * Return the sum of the three integers. You may assume that each input would have exactly one solution. 7 | * For example, given array S = {-1 2 1 -4}, and target = 1. 8 | * The sum that is closest to the target is 2. (-1 + 2 + 1 = 2). 9 | * 10 | */ 11 | 12 | /** 13 | * @param {number[]} nums 14 | * @param {number} target 15 | * @return {number} 16 | */ 17 | var threeSumClosest = function (nums, target) { 18 | 19 | var ans = nums[0] + nums[1] + nums[2]; 20 | var len = nums.length; 21 | 22 | nums.sort((a, b) => a < b ? -1 : (a > b) ? 1 : 0); 23 | 24 | for (var i = 0; i < len - 2; i++) { 25 | var j = i + 1; 26 | var k = len - 1; 27 | 28 | while (j < k) { 29 | var sum = nums[i] + nums[j] + nums[k]; 30 | if (sum === target) return sum; 31 | if (sum > target) k--; 32 | if (sum < target) j++; 33 | if (Math.abs(target - sum) < Math.abs(target - ans)) { 34 | ans = sum; 35 | } 36 | } 37 | } 38 | return ans; 39 | 40 | }; 41 | 42 | console.log(threeSumClosest([-1, 2, 1, -4], 1)); -------------------------------------------------------------------------------- /017-Letter-Combinations-of-a-Phone-Number.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/ 4 | * Difficulty:Medium 5 | * 6 | * Given a digit string, return all possible letter combinations that the number could represent. 7 | * 8 | * A mapping of digit to letters (just like on the telephone buttons) is given below. 9 | * 10 | * Input:Digit string "23" 11 | * Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]. 12 | * 13 | * Note: 14 | * Although the above answer is in lexicographical order, your answer could be in any order you want. 15 | * 16 | */ 17 | 18 | /** 19 | * @param {string} digits 20 | * @return {string[]} 21 | */ 22 | var letterCombinations = function (digits) { 23 | 24 | if (!digits.length) return []; 25 | 26 | var ans = ['']; 27 | var map = ['0', '1', 'abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz']; 28 | 29 | for (var i = 0; i < digits.length; i++) { 30 | var str = map[digits[i]]; 31 | var tmp = []; 32 | for (var j = 0; j < ans.length; j++) { 33 | var t = ans[j]; 34 | for (var k = 0; k < str.length; k++) { 35 | tmp.push(t + str[k]); 36 | } 37 | } 38 | ans = tmp; 39 | } 40 | 41 | return ans; 42 | }; 43 | 44 | console.log(letterCombinations('23')); -------------------------------------------------------------------------------- /018-4Sum.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/4sum/description/ 3 | * Difficulty:Medium 4 | * 5 | * Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. 6 | * Note: The solution set must not contain duplicate quadruplets. 7 | * For example, given array S = [1, 0, -1, 0, -2, 2], and target = 0. 8 | * A solution set is: 9 | * [ 10 | * [-1, 0, 0, 1], 11 | * [-2, -1, 1, 2], 12 | * [-2, 0, 0, 2] 13 | ] 14 | */ 15 | 16 | /** 17 | * @param {number[]} nums 18 | * @param {number} target 19 | * @return {number[][]} 20 | */ 21 | var fourSum = function (nums, target) { 22 | var len = nums.length; 23 | if (len < 4) return []; 24 | nums.sort((a, b) => a < b ? -1 : a > b ? 1 : 0); 25 | // console.log(nums); 26 | var ans = []; 27 | for (var i = 0; i < len - 3; i++) { 28 | 29 | for (var j = i + 1; j < len - 2; j++) { 30 | var k = j + 1; 31 | var l = len - 1; 32 | 33 | while (k < l) { 34 | var sum = nums[i] + nums[j] + nums[k] + nums[l]; 35 | if (sum === target) { 36 | ans.push([nums[i], nums[j], nums[k], nums[l]]); 37 | while (nums[l--] === nums[l] && nums[k++] === nums[k] && k < l); 38 | } 39 | else if (sum < target) while (nums[k++] === nums[k] && k < l); 40 | else while (nums[l--] === nums[l] && k < l); 41 | } 42 | while (nums[j] === nums[j + 1]) j++; 43 | } 44 | while (nums[i] === nums[i + 1]) i++; 45 | 46 | } 47 | return ans; 48 | 49 | }; 50 | 51 | console.log(fourSum([-5, -4, -2, -2, -2, -1, 0, 0, 1], -9)); 52 | -------------------------------------------------------------------------------- /019-Remove-Nth-Node-From-End-of-List.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/ 3 | * Difficulty:Medium 4 | * 5 | * Given a linked list, remove the nth node from the end of list and return its head. 6 | * 7 | * For example, 8 | * Given linked list: 1->2->3->4->5, and n = 2. 9 | * After removing the second node from the end, the linked list becomes 1->2->3->5. 10 | * 11 | * Note: 12 | * Given n will always be valid. 13 | * Try to do this in one pass. 14 | * 15 | */ 16 | 17 | 18 | // Definition for singly-linked list. 19 | function ListNode(val) { 20 | this.val = val; 21 | this.next = null; 22 | } 23 | 24 | /** 25 | * @param {ListNode} head 26 | * @param {number} n 27 | * @return {ListNode} 28 | */ 29 | var removeNthFromEnd = function (head, n) { 30 | if (!head) return head; 31 | var len = 0; 32 | var tail = head; 33 | while (tail) { 34 | tail = tail.next; 35 | len++; 36 | } 37 | if (len === n) { 38 | return head.next; 39 | } 40 | 41 | len = len - n - 1; 42 | tail = head; 43 | while (len) { 44 | tail = tail.next; 45 | len--; 46 | } 47 | tail.next = tail.next.next; 48 | return head; 49 | }; 50 | 51 | var a = new ListNode(1); 52 | var b = new ListNode(2); 53 | var c = new ListNode(3); 54 | var d = new ListNode(4); 55 | // var e = new ListNode(5); 56 | a.next = b; 57 | b.next = c; 58 | c.next = d; 59 | // d.next = e; 60 | 61 | console.log(removeNthFromEnd(a, 2)); 62 | -------------------------------------------------------------------------------- /020-Valid-Parentheses.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/valid-parentheses/ 3 | * Difficulty:Easy 4 | * 5 | * Given a string containing just the characters '(', ')', '{', '}', '[' and ']', 6 | * determine if the input string is valid. 7 | * The brackets must close in the correct order, 8 | * "()" and "()[]{}" are all valid but "(]" and "([)]" are not. 9 | */ 10 | 11 | /** 12 | * @param {string} s 13 | * @return {boolean} 14 | */ 15 | var isValid = function (s) { 16 | 17 | var stack = []; 18 | for (var i = 0; i < s.length; i++) { 19 | var c = s[i]; 20 | switch (c) { 21 | case '(': 22 | stack.push(')'); 23 | break; 24 | case '[': 25 | stack.push(']'); 26 | break; 27 | case '{': 28 | stack.push('}'); 29 | break; 30 | default: 31 | 32 | if (!stack.length || stack.pop() !== c) { 33 | // console.log(stack); 34 | return false; 35 | } 36 | 37 | } 38 | } 39 | 40 | return stack.length === 0; 41 | }; 42 | 43 | console.log(isValid('()[]{}')); 44 | console.log(isValid('[()][]{}')); 45 | console.log(isValid('(])')); -------------------------------------------------------------------------------- /021-Merge-Two-Sorted-Lists.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/merge-two-sorted-lists/description/ 3 | * Difficulty:Easy 4 | * 5 | * Merge two sorted linked lists and return it as a new list. 6 | * The new list should be made by splicing together the nodes of the first two lists. 7 | * 8 | * Example: 9 | * Input: 1->2->4, 1->3->4 10 | * Output: 1->1->2->3->4->4 11 | */ 12 | 13 | 14 | // Definition for singly-linked list. 15 | function ListNode(val) { 16 | this.val = val; 17 | this.next = null; 18 | } 19 | 20 | /** 21 | * 22 | * non-recursion 23 | * 24 | * @param {ListNode} l1 25 | * @param {ListNode} l2 26 | * @return {ListNode} 27 | */ 28 | var mergeTwoLists = function (l1, l2) { 29 | if (!l1) return l2; 30 | if (!l2) return l1; 31 | 32 | var n = new ListNode(0); 33 | var t = n; 34 | 35 | while (l1 && l2) { 36 | if (l1.val <= l2.val) { 37 | n.next = l1; 38 | l1 = l1.next; 39 | } else { 40 | n.next = l2; 41 | l2 = l2.next; 42 | } 43 | n = n.next; 44 | } 45 | if (l1) n.next = l1; 46 | if (l2) n.next = l2; 47 | 48 | return t.next; 49 | }; 50 | 51 | /** 52 | * 53 | * recursion 54 | * 55 | * @param {ListNode} l1 56 | * @param {ListNode} l2 57 | * @return {ListNode} 58 | */ 59 | var mergeTwoLists = function (l1, l2) { 60 | if (!l1) return l2; 61 | if (!l2) return l1; 62 | 63 | if (l1.val <= l2.val) { 64 | l1.next = mergeTwoLists(l1.next, l2); 65 | return l1; 66 | } else { 67 | l2.next = mergeTwoLists(l1, l2.next); 68 | return l2; 69 | } 70 | }; 71 | 72 | -------------------------------------------------------------------------------- /022-Generate-Parentheses.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/generate-parentheses/description/ 3 | * Difficulty:Medium 4 | * 5 | * Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. 6 | * For example, given n = 3, a solution set is: 7 | * [ 8 | * "((()))", 9 | * "(()())", 10 | * "(())()", 11 | * "()(())", 12 | * "()()()" 13 | * ] 14 | */ 15 | 16 | /** 17 | * @param {number} n 18 | * @return {string[]} 19 | */ 20 | var generateParenthesis = function (n) { 21 | 22 | var ans = []; 23 | helper(ans, '', 0, 0, n); 24 | return ans; 25 | }; 26 | 27 | function helper(ans, str, left, right, n) { 28 | if (right === n) ans.push(str); 29 | if (left < n) { 30 | helper(ans, str + '(', left + 1, right, n); 31 | } 32 | if (right < left) { 33 | helper(ans, str + ')', left, right + 1, n); 34 | } 35 | } 36 | 37 | console.log(generateParenthesis(3)); -------------------------------------------------------------------------------- /023-Merge-k-Sorted-Lists.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/merge-k-sorted-lists/description/ 3 | * Difficulty:Hard 4 | * 5 | * Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 6 | */ 7 | 8 | 9 | //Definition for singly-linked list. 10 | function ListNode(val) { 11 | this.val = val; 12 | this.next = null; 13 | } 14 | 15 | /** 16 | * @param {ListNode[]} lists 17 | * @return {ListNode} 18 | */ 19 | var mergeKLists = function (lists) { 20 | return lists.reduce((a, b) => merge2lists(a, b), null) 21 | }; 22 | 23 | function merge2lists(a, b) { 24 | if (!a && !b) return null; 25 | if (!a) return b; 26 | if (!b) return a; 27 | var h; 28 | if (a.val < b.val) { 29 | h = a; 30 | a = a.next; 31 | } else { 32 | h = b; 33 | b = b.next; 34 | } 35 | var t = h; 36 | 37 | while (a && b) { 38 | if (a.val < b.val) { 39 | t.next = a; 40 | t = t.next; 41 | a = a.next; 42 | } else { 43 | t.next = b; 44 | t = t.next; 45 | b = b.next; 46 | } 47 | } 48 | if (a) t.next = a; 49 | if (b) t.next = b; 50 | return h; 51 | 52 | } 53 | 54 | var a = { 55 | val: 1, 56 | next: { 57 | val: 4, 58 | next: { 59 | val: 7, 60 | next: null 61 | } 62 | } 63 | } 64 | var b = { 65 | val: 2, 66 | next: { 67 | val: 8, 68 | next: { 69 | val: 9, 70 | next: null 71 | } 72 | } 73 | } 74 | 75 | var c = { 76 | val: 3, 77 | next: { 78 | val: 10, 79 | next: null 80 | } 81 | } 82 | 83 | // console.log(merge2lists(a, b)); 84 | console.log(mergeKLists([a, b, c])) -------------------------------------------------------------------------------- /024-Swap-Nodes-in-Pairs.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/swap-nodes-in-pairs/description/ 3 | * Difficulty:Medium 4 | * 5 | * Given a linked list, swap every two adjacent nodes and return its head. 6 | * 7 | * For example, 8 | * Given 1->2->3->4, you should return the list as 2->1->4->3. 9 | * Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed. 10 | * 11 | */ 12 | 13 | // Definition for singly-linked list. 14 | function ListNode(val) { 15 | this.val = val; 16 | this.next = null; 17 | } 18 | 19 | /** 20 | * @param {ListNode} head 21 | * @return {ListNode} 22 | */ 23 | var swapPairs = function (head) { 24 | var t = new ListNode(0); 25 | t.next = head; 26 | 27 | var a = t; 28 | 29 | while (true) { 30 | if (!a) break; 31 | var b = a.next; 32 | if (!b) break; 33 | var c = b.next; 34 | if (!c) break; 35 | 36 | b.next = c.next; 37 | c.next = b; 38 | a.next = c; 39 | a = b; 40 | 41 | } 42 | return t.next; 43 | }; 44 | 45 | console.log(swapPairs({ 46 | val: 1, 47 | next: { 48 | val: 2, 49 | next: { 50 | val: 3, 51 | next: { 52 | val: 4 53 | } 54 | } 55 | } 56 | })) -------------------------------------------------------------------------------- /025-Reverse-Nodes-in-k-Group.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/reverse-nodes-in-k-group/description/ 3 | * Difficulty:Hard 4 | * 5 | * Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. 6 | * k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. 7 | * You may not alter the values in the nodes, only nodes itself may be changed. 8 | * Only constant memory is allowed. 9 | * For example, 10 | * Given this linked list: 1->2->3->4->5 11 | * For k = 2, you should return: 2->1->4->3->5 12 | * For k = 3, you should return: 3->2->1->4->5 13 | * 14 | */ 15 | 16 | // Definition for singly-linked list. 17 | function ListNode(val) { 18 | this.val = val; 19 | this.next = null; 20 | } 21 | 22 | /** 23 | * @param {ListNode} head 24 | * @param {number} k 25 | * @return {ListNode} 26 | */ 27 | var reverseKGroup = function (head, k) { 28 | // if (k === 1) 29 | // return head; 30 | var t = new ListNode(0); 31 | t.next = head; 32 | var s = t; 33 | 34 | while (true) { 35 | var cnt = 0; 36 | var f = t; 37 | while (cnt++ < k && f) { 38 | f = f.next; 39 | } 40 | // console.log(p(t), p(f)); 41 | 42 | if (!f || cnt !== k + 1) break; 43 | cnt = 0; 44 | var a = t.next; 45 | 46 | while (++cnt < k) { 47 | var b = a.next; 48 | a.next = b.next; 49 | b.next = t.next; 50 | t.next = b; 51 | // console.log(p(t), p(a), p(b)); 52 | } 53 | t = a; 54 | } 55 | 56 | return s.next; 57 | }; 58 | 59 | function p(n) { 60 | var t = n; 61 | var s = ''; 62 | while (t) { 63 | s = s + t.val + '->'; 64 | t = t.next; 65 | } 66 | s += 'null'; 67 | return s; 68 | } 69 | // 70 | console.log(p(reverseKGroup({ 71 | val: 1, 72 | next: { 73 | val: 2, 74 | next: { 75 | val: 3, 76 | next: { 77 | val: 4 78 | } 79 | } 80 | } 81 | }, 2))) 82 | 83 | console.log(p(reverseKGroup({val: 1}, 2))); 84 | 85 | console.log(p(reverseKGroup({ 86 | val: 1, 87 | next: { 88 | val: 2 89 | } 90 | }, 2))) 91 | 92 | console.log(p(reverseKGroup({ 93 | val: 1, 94 | next: { 95 | val: 2, 96 | next: { 97 | val: 3, 98 | next: { 99 | val: 4, 100 | next: { 101 | val: 5, 102 | next: { 103 | val: 6, 104 | next: { 105 | val: 7 106 | } 107 | } 108 | } 109 | } 110 | } 111 | } 112 | }, 3))) 113 | // 114 | 115 | console.log(p(reverseKGroup({ 116 | val: 1, 117 | next: { 118 | val: 2, 119 | next: { 120 | val: 3, 121 | next: { 122 | val: 4, 123 | next: null 124 | } 125 | } 126 | } 127 | }, 2))); 128 | 129 | -------------------------------------------------------------------------------- /026-Remove-Duplicates-from-Sorted-Array.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/remove-duplicates-from-sorted-array/ 3 | * Difficulty:Easy 4 | * 5 | * Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. 6 | * Do not allocate extra space for another array, you must do this in place with constant memory. 7 | * 8 | * For example, 9 | * Given input array nums = [1,1,2], 10 | * Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. 11 | * It doesn't matter what you leave beyond the new length. 12 | */ 13 | 14 | /** 15 | * @param {number[]} nums 16 | * @return {number} 17 | */ 18 | var removeDuplicates = function (nums) { 19 | var n = nums.length; 20 | if (!n) return 0; 21 | var last = nums[0]; 22 | var cnt = 1; 23 | 24 | for (var i = 1; i < nums.length; i++) { 25 | if (nums[i] !== last) { 26 | last = nums[i]; 27 | cnt++; 28 | } 29 | else { 30 | nums.splice(i, 1); 31 | i--; 32 | } 33 | } 34 | return cnt; 35 | 36 | }; 37 | 38 | /** 39 | * @param {number[]} nums 40 | * @return {number} 41 | */ 42 | var removeDuplicates = function (nums) { 43 | for (var i = 0; i < nums.length - 1; i++) 44 | if (nums[i] === nums[i + 1]) nums.splice(i--, 1); 45 | return nums.length; 46 | }; 47 | 48 | var arr = [1, 2, 2]; 49 | console.log(removeDuplicates(arr)); 50 | console.log(arr); -------------------------------------------------------------------------------- /027-Remove-Element.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/remove-element/description/ 3 | * Difficulty:Easy 4 | * 5 | * Given an array and a value, remove all instances of that value in-place and return the new length. 6 | * Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. 7 | * The order of elements can be changed. It doesn't matter what you leave beyond the new length. 8 | * Example: 9 | * Given nums = [3,2,2,3], val = 3, 10 | * Your function should return length = 2, with the first two elements of nums being 2. 11 | */ 12 | 13 | /** 14 | * @param {number[]} nums 15 | * @param {number} val 16 | * @return {number} 17 | */ 18 | var removeElement = function (nums, val) { 19 | 20 | var len = nums.length; 21 | for (var i = 0; i < len; i++) { 22 | if (nums[i] === val) { 23 | nums.splice(i, 1); 24 | i--; 25 | len--; 26 | } 27 | } 28 | return len; 29 | }; 30 | 31 | console.log(removeElement([3, 2, 2, 3], 3)); -------------------------------------------------------------------------------- /030-Substring-with-Concatenation-of-All-Words.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * https://leetcode.com/problems/substring-with-concatenation-of-all-words/description/ 4 | * Difficulty:Hard 5 | * 6 | * You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) 7 | * in s that is a concatenation of each word in words exactly once and without any intervening characters. 8 | * For example, given: 9 | * s: "barfoothefoobarman" 10 | * words: ["foo", "bar"] 11 | * You should return the indices: [0,9]. 12 | * (order does not matter). 13 | * 14 | */ 15 | /** 16 | * @param {string} s 17 | * @param {string[]} words 18 | * @return {number[]} 19 | */ 20 | var findSubstring = function (s, words) { 21 | if (!s.length || !words.length) return []; 22 | var ans = []; 23 | var toFind = {}; 24 | 25 | var m = words.length; 26 | var n = words[0].length; 27 | 28 | for (var i = 0; i < m; i++) { 29 | toFind[words[i]] = (toFind[words[i]] || 0) + 1; 30 | } 31 | 32 | for (i = 0; i <= s.length - m * n; i++) { 33 | var found = {}; 34 | 35 | for (var j = 0; j < m; j++) { 36 | var k = i + n * j; 37 | var w = s.substr(k, n); 38 | if (!toFind[w]) break; 39 | found[w] = (found[w] || 0) + 1; 40 | if (found[w] > toFind[w]) break; 41 | } 42 | if (j === m) ans.push(i); 43 | } 44 | 45 | return ans; 46 | 47 | }; 48 | 49 | console.log(findSubstring('barfoothefoobarman', ['foo', 'bar'])); -------------------------------------------------------------------------------- /031-Next-Permutation.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/next-permutation/description/ 3 | * Difficulty:Medium 4 | * 5 | * Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. 6 | * If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). 7 | * The replacement must be in-place, do not allocate extra memory. 8 | * Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column. 9 | * 1,2,3 → 1,3,2 10 | * 3,2,1 → 1,2,3 11 | * 1,1,5 → 1,5,1 12 | */ 13 | /** 14 | * @param {number[]} nums 15 | * @return {void} Do not return anything, modify nums in-place instead. 16 | */ 17 | var nextPermutation = function (nums) { 18 | 19 | if (nums.length < 2) return; 20 | var peak = nums.length - 1; 21 | for (var i = peak - 1; nums[i] >= nums[peak]; peak = i--); 22 | 23 | if (peak !== 0) { 24 | var swapIndex = findSwap(nums, peak, nums.length - 1, peak - 1); 25 | if (swapIndex !== -1) { 26 | swap(nums, peak - 1, swapIndex); 27 | } 28 | } 29 | 30 | reverse(nums, peak, nums.length - 1); 31 | 32 | }; 33 | 34 | function findSwap(nums, s, e, target) { 35 | for (var i = e; i >= s; i--) { 36 | if (nums[i] > nums[target]) return i; 37 | } 38 | return -1; 39 | } 40 | 41 | function swap(nums, s, e) { 42 | var t = nums[s]; 43 | nums[s] = nums[e]; 44 | nums[e] = t; 45 | } 46 | function reverse(nums, s, e) { 47 | // var len = e - s; 48 | for (var i = 0; i < Math.ceil((e - s ) / 2); i++) { 49 | 50 | swap(nums, s + i, e - i); 51 | } 52 | // return nums; 53 | } 54 | 55 | // console.log(reverse([1, 2, 3, 4, 5], 0, 4)); 56 | // console.log(reverse([1, 2, 3, 4, 5], 3, 4)); 57 | // console.log(reverse([1, 2, 3, 4, 5], 2, 3)); 58 | // console.log(reverse([1, 2, 3, 4, 5], 1, 1)); 59 | // console.log(reverse([1, 2, 3, 4, 5], 1, 4)); 60 | 61 | // var nums = [1, 2, 5, 4, 3]; 62 | // console.log(nums); 63 | // nextPermutation(nums); 64 | // console.log(nums); 65 | // 66 | console.log('===='); 67 | 68 | var nums = [2, 3, 1]; 69 | console.log(nums); 70 | nextPermutation(nums); 71 | console.log(nums); 72 | 73 | console.log('===='); 74 | 75 | var nums = [1, 1]; 76 | console.log(nums); 77 | nextPermutation(nums); 78 | console.log(nums); 79 | 80 | console.log('===='); 81 | 82 | var nums = [3, 2, 1]; 83 | console.log(nums); 84 | nextPermutation(nums); 85 | console.log(nums); 86 | 87 | 88 | -------------------------------------------------------------------------------- /032-Longest-Valid-Parentheses.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/longest-valid-parentheses/description/ 3 | * Difficulty:Hard 4 | * 5 | * Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. 6 | * For "(()", the longest valid parentheses substring is "()", which has length = 2. 7 | * Another example is ")()())", where the longest valid parentheses substring is "()()", which has length = 4. 8 | */ 9 | 10 | /** 11 | * 使用栈解决 12 | * @param {string} s 13 | * @return {number} 14 | */ 15 | var longestValidParentheses = function (s) { 16 | var stack = []; 17 | for (var i = 0; i < s.length; i++) { 18 | if (s[i] === '(') stack.push(i); 19 | else { 20 | if (stack.length && s[stack[stack.length - 1]] === '(') stack.length--; 21 | else stack.push(i); 22 | } 23 | } 24 | 25 | if (!stack.length) return s.length; 26 | var longest = 0; 27 | var end = s.length; 28 | var start = 0; 29 | while (stack.length) { 30 | start = stack[stack.length - 1]; 31 | stack.length--; 32 | longest = Math.max(longest, end - start - 1); 33 | end = start; 34 | } 35 | longest = Math.max(longest, end); 36 | return longest; 37 | }; 38 | 39 | 40 | console.log(longestValidParentheses('()'), 2); 41 | console.log(longestValidParentheses('())'), 2); 42 | console.log(longestValidParentheses('(()'), 2); 43 | console.log(longestValidParentheses('))()())((())))'), 6); 44 | console.log(longestValidParentheses('()'), 2); 45 | console.log(longestValidParentheses('('), 0); 46 | console.log(longestValidParentheses(')()()))()()())'), 6); 47 | console.log(longestValidParentheses('()(()'), 2); 48 | console.log(longestValidParentheses('()(()'), 2); 49 | console.log(longestValidParentheses('(()'), 2); 50 | 51 | -------------------------------------------------------------------------------- /033-Search-in-Rotated-Sorted-Array.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/search-in-rotated-sorted-array/description/ 3 | * Difficulty:Medium 4 | * 5 | * Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. 6 | * (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). 7 | * You are given a target value to search. If found in the array return its index, otherwise return -1. 8 | * You may assume no duplicate exists in the array. 9 | */ 10 | /** 11 | * @param {number[]} nums 12 | * @param {number} target 13 | * @return {number} 14 | */ 15 | var search = function (nums, target) { 16 | 17 | 18 | var lo = 0; 19 | var hi = nums.length - 1; 20 | while (lo < hi) { 21 | var mid = Math.floor((lo + hi) / 2); 22 | if (nums[mid] < nums[hi]) hi = mid; 23 | else lo = mid + 1; 24 | } 25 | var i = lo; 26 | 27 | lo = target < nums[0] ? i : 0; 28 | hi = target <= nums[nums.length - 1] ? nums.length - 1 : i; 29 | 30 | // console.log(nums, lo, hi); 31 | while (lo <= hi) { 32 | mid = Math.floor((lo + hi) / 2); 33 | // console.log(lo, mid, hi) 34 | if (nums[mid] < target) lo = mid + 1; 35 | else if (nums[mid] === target) return mid; 36 | else hi = mid - 1; 37 | } 38 | 39 | return -1; 40 | 41 | }; 42 | 43 | console.log(search([], 5)) 44 | console.log(search([1], 0)) 45 | console.log(search([4, 5, 6, 7, 0, 1, 2], 2)) 46 | console.log(search([3, 1], 1)) -------------------------------------------------------------------------------- /034-Search-for-a-Range.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * https://leetcode.com/problems/search-for-a-range/description/ 4 | * Difficulty:Medium 5 | * 6 | * Given an array of integers sorted in ascending order, find the starting and ending position of a given target value. 7 | * Your algorithm's runtime complexity must be in the order of O(log n). 8 | * If the target is not found in the array, return [-1, -1]. 9 | * For example, 10 | * Given [5, 7, 7, 8, 8, 10] and target value 8, 11 | * return [3, 4]. 12 | * 13 | */ 14 | /** 15 | * @param {number[]} nums 16 | * @param {number} target 17 | * @return {number[]} 18 | */ 19 | var searchRange = function (nums, target) { 20 | 21 | var i = 0; 22 | var j = nums.length - 1; 23 | var ret = [-1, -1]; 24 | while (i < j) { 25 | var mid = Math.floor((i + j) / 2); 26 | // console.log(i, mid, j); 27 | if (nums[mid] < target) i = mid + 1; 28 | else j = mid; 29 | } 30 | if (nums[i] !== target) return ret; 31 | ret[0] = i; 32 | j = nums.length - 1; 33 | while (i < j) { 34 | mid = Math.ceil((i + j) / 2); 35 | // console.log(i, mid, j); 36 | if (nums[mid] > target) j = mid - 1; 37 | else i = mid; 38 | } 39 | ret[1] = j; 40 | return ret; 41 | }; 42 | 43 | console.log(searchRange([5, 7, 7, 8, 8, 10], 8)); -------------------------------------------------------------------------------- /035-Search-Insert-Position.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/search-insert-position/description/ 3 | * Difficulty:Easy 4 | * 5 | * Given a sorted array and a target value, return the index if the target is found. 6 | * If not, return the index where it would be if it were inserted in order. 7 | * 8 | * You may assume no duplicates in the array. 9 | * 10 | * Here are few examples. 11 | * [1,3,5,6], 5 → 2 12 | * [1,3,5,6], 2 → 1 13 | * [1,3,5,6], 7 → 4 14 | * [1,3,5,6], 0 → 0 15 | */ 16 | 17 | /** 18 | * 解题思路 19 | * 20 | * 找到不比目标元素大的索引即可 21 | * 22 | * @param {number[]} nums 23 | * @param {number} target 24 | * @return {number} 25 | */ 26 | var searchInsert = function (nums, target) { 27 | 28 | for (var i = 0; i < nums.length; i++) { 29 | if (target <= nums[i]) return i; 30 | } 31 | return i; 32 | 33 | }; 34 | 35 | console.log(searchInsert([1, 3, 5, 6], 5) == 2); 36 | console.log(searchInsert([1, 3, 5, 6], 2) == 1); 37 | console.log(searchInsert([1, 3, 5, 6], 7) == 4); 38 | console.log(searchInsert([1, 3, 5, 6], 0) == 0); 39 | console.log(searchInsert([1, 3, 5, 6], 100) == 4); -------------------------------------------------------------------------------- /036-Valid-Sudoku.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/valid-sudoku/description/ 3 | * Difficulty:Medium 4 | * 5 | * Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. 6 | * The Sudoku board could be partially filled, where empty cells are filled with the character '.'. 7 | * A partially filled sudoku which is valid. 8 | * Note: 9 | * A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated. 10 | * 11 | */ 12 | /** 13 | * @param {character[][]} board 14 | * @return {boolean} 15 | */ 16 | var isValidSudoku = function (board) { 17 | for (var i = 0; i < 9; i++) { 18 | var rowNums = []; 19 | var colNums = []; 20 | var cubeNums = []; 21 | 22 | for (var j = 0; j < 9; j++) { 23 | var ch = board[i][j]; 24 | if (ch !== '.') { 25 | if (rowNums.indexOf(ch) > -1) return false; 26 | rowNums.push(ch); 27 | } 28 | 29 | ch = board[j][i]; 30 | if (ch !== '.') { 31 | if (colNums.indexOf(ch) > -1) return false; 32 | colNums.push(ch); 33 | } 34 | 35 | var row = Math.floor(i / 3) * 3 + Math.floor(j / 3); 36 | var col = i % 3 * 3 + j % 3; 37 | // console.log(i, j, row, col); 38 | ch = board[row][col]; 39 | if (ch !== '.') { 40 | if (cubeNums.indexOf(ch) > -1) return false; 41 | cubeNums.push(ch); 42 | } 43 | } 44 | } 45 | return true; 46 | }; 47 | 48 | // console.log(isValidSudoku([ 49 | // [".", "8", "7", "6", "5", "4", "3", "2", "1"], 50 | // ["2", ".", ".", ".", ".", ".", ".", ".", "."], 51 | // ["3", ".", ".", ".", ".", ".", ".", ".", "."], 52 | // ["4", ".", ".", ".", ".", ".", ".", ".", "."], 53 | // ["5", ".", ".", ".", ".", ".", ".", ".", "."], 54 | // ["6", ".", ".", ".", ".", ".", ".", ".", "."], 55 | // ["7", ".", ".", ".", ".", ".", ".", ".", "."], 56 | // ["8", ".", ".", ".", ".", ".", ".", ".", "."], 57 | // ["9", ".", ".", ".", ".", ".", ".", ".", "."]])); 58 | console.log(isValidSudoku([ 59 | [".", ".", "4", ".", ".", ".", "6", "3", "."], 60 | [".", ".", ".", ".", ".", ".", ".", ".", "."], 61 | ["5", ".", ".", ".", ".", ".", ".", "9", "."], 62 | [".", ".", ".", "5", "6", ".", ".", ".", "."], 63 | ["4", ".", "3", ".", ".", ".", ".", ".", "1"], 64 | [".", ".", ".", "7", ".", ".", ".", ".", "."], 65 | [".", ".", ".", "5", ".", ".", ".", ".", "."], 66 | [".", ".", ".", ".", ".", ".", ".", ".", "."], 67 | [".", ".", ".", ".", ".", ".", ".", ".", "."]])) 68 | -------------------------------------------------------------------------------- /037-Sudoku-Solver.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/sudoku-solver/description/ 3 | * Difficulty:Hard 4 | * 5 | * Write a program to solve a Sudoku puzzle by filling the empty cells. 6 | * Empty cells are indicated by the character '.'. 7 | * You may assume that there will be only one unique solution. 8 | */ 9 | 10 | /** 11 | * @param {character[][]} board 12 | * @return {void} Do not return anything, modify board in-place instead. 13 | */ 14 | var solveSudoku = function (board) { 15 | solve(board); 16 | console.log(board); 17 | }; 18 | 19 | function solve(board) { 20 | for (var i = 0; i < 9; i++) { 21 | for (var j = 0; j < 9; j++) { 22 | var ch = board[i][j]; 23 | if (ch === '.') { 24 | for (var k = 1; k <= 9; k++) { 25 | 26 | if (isValid(i, j, board, '' + k)) { 27 | 28 | board[i][j] = '' + k; 29 | // console.log(board); 30 | // console.log('-------------'); 31 | if (solve(board)) { 32 | // console.log(board); 33 | // console.log('-------------'); 34 | return true; 35 | } else { 36 | board[i][j] = '.'; 37 | } 38 | } 39 | } 40 | return false; 41 | } 42 | } 43 | } 44 | return true; 45 | } 46 | 47 | function isValid(row, col, board, t) { 48 | 49 | for (var i = 0; i < 9; i++) { 50 | var ch = board[row][i]; 51 | if (ch === t) return false; 52 | 53 | ch = board[i][col]; 54 | if (ch === t) return false; 55 | 56 | ch = board[Math.floor(row / 3) * 3 + Math.floor(i / 3)][Math.floor(col / 3) * 3 + i % 3]; 57 | // if (row === 0 && col === 8) { 58 | // console.log('~ ', Math.floor(row / 3) * 3 + Math.floor(i / 3), Math.floor(row / 3) * 3 + i % 3, ch); 59 | // } 60 | if (ch === t) return false; 61 | } 62 | return true; 63 | 64 | } 65 | 66 | console.log(solveSudoku([ 67 | [".", ".", "9", "7", "4", "8", ".", ".", "."], 68 | ["7", ".", ".", ".", ".", ".", ".", ".", "."], 69 | [".", "2", ".", "1", ".", "9", ".", ".", "."], 70 | [".", ".", "7", ".", ".", ".", "2", "4", "."], 71 | [".", "6", "4", ".", "1", ".", "5", "9", "."], 72 | [".", "9", "8", ".", ".", ".", "3", ".", "."], 73 | [".", ".", ".", "8", ".", "3", ".", "2", "."], 74 | [".", ".", ".", ".", ".", ".", ".", ".", "6"], 75 | [".", ".", ".", "2", "7", "5", "9", ".", "."] 76 | ])); 77 | 78 | console.log([ 79 | ["5", "1", "9", "7", "4", "8", "6", "3", "2"], 80 | ["7", "8", "3", "6", "5", "2", "4", "1", "9"], 81 | ["4", "2", "6", "1", "3", "9", "8", "7", "5"], 82 | ["3", "5", "7", "9", "8", "6", "2", "4", "1"], 83 | ["2", "6", "4", "3", "1", "7", "5", "9", "8"], 84 | ["1", "9", "8", "5", "2", "4", "3", "6", "7"], 85 | ["9", "7", "5", "8", "6", "3", "1", "2", "4"], 86 | ["8", "3", "2", "4", "9", "1", "7", "5", "6"], 87 | ["6", "4", "1", "2", "7", "5", "9", "8", "3"] 88 | ]) 89 | -------------------------------------------------------------------------------- /038-Count-and-Say.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/count-and-say/description/ 3 | * Difficulty:Easy 4 | * 5 | * The count-and-say sequence is the sequence of integers with the first five terms as following: 6 | * 1. 1 7 | * 2. 11 8 | * 3. 21 9 | * 4. 1211 10 | * 5. 111221 11 | * 12 | * 1 is read off as "one 1" or 11. 13 | * 14 | * 11 is read off as "two 1s" or 21. 15 | * 16 | * 21 is read off as "one 2, then one 1" or 1211. 17 | * 18 | * Given an integer n, generate the nth term of the count-and-say sequence. 19 | * 20 | * Note: Each term of the sequence of integers will be represented as a string. 21 | * 22 | * Example 1: 23 | * Input: 1 24 | * Output: "1" 25 | * 26 | * Example 2: 27 | * Input: 4 28 | * Output: "1211" 29 | */ 30 | 31 | /** 32 | * @param {number} n 33 | * @return {string} 34 | */ 35 | var countAndSay = function (n) { 36 | var ans = '1'; 37 | for (var i = 1; i < n; i++) { 38 | var tmp = ''; 39 | var cnt = 1; 40 | for (var j = 1; j < ans.length; j++) { 41 | if (ans[j] === ans[j - 1]) cnt++; 42 | else { 43 | tmp += (cnt + ans[j - 1]); 44 | cnt = 1; 45 | } 46 | } 47 | ans = tmp + cnt + ans[j - 1]; 48 | } 49 | 50 | return ans; 51 | }; 52 | 53 | console.log(countAndSay(1)); 54 | console.log(countAndSay(2)); 55 | console.log(countAndSay(3)); 56 | console.log(countAndSay(4)); 57 | console.log(countAndSay(5)); 58 | console.log(countAndSay(6)); -------------------------------------------------------------------------------- /039-Combination-Sum.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/combination-sum/description/ 3 | * Difficulty:Medium 4 | * 5 | * Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. 6 | * The same repeated number may be chosen from C unlimited number of times. 7 | * Note: 8 | * All numbers (including target) will be positive integers. 9 | * The solution set must not contain duplicate combinations. 10 | * For example, given candidate set [2, 3, 6, 7] and target 7, 11 | * A solution set is: 12 | * [ 13 | * [7], 14 | * [2, 2, 3] 15 | * ] 16 | */ 17 | 18 | /** 19 | * @param {number[]} candidates 20 | * @param {number} target 21 | * @return {number[][]} 22 | */ 23 | var combinationSum = function (candidates, target) { 24 | 25 | var res = []; 26 | var temp = []; 27 | helper(res, temp, candidates, target, 0); 28 | return res; 29 | 30 | }; 31 | 32 | function helper(res, temp, candidates, target, start) { 33 | 34 | if (target === 0) { 35 | res.push([...temp]); 36 | return; 37 | } 38 | 39 | for (var i = start; i < candidates.length; i++) { 40 | if (candidates[i] <= target) { 41 | temp.push(candidates[i]); 42 | helper(res, temp, candidates, target - candidates[i], i); 43 | temp.length -= 1; 44 | } 45 | 46 | } 47 | } 48 | 49 | console.log(combinationSum([1, 2, 3, 5, 6, 7], 7)); 50 | console.log(combinationSum([7, 2, 3, 5, 6, 1], 7)); -------------------------------------------------------------------------------- /040-Combination-Sum-II.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. 3 | * Each number in C may only be used once in the combination. 4 | * Note: 5 | * All numbers (including target) will be positive integers. 6 | * The solution set must not contain duplicate combinations. 7 | * For example, given candidate set [10, 1, 2, 7, 6, 1, 5] and target 8, 8 | * A solution set is: 9 | * [ 10 | * [1, 7], 11 | * [1, 2, 5], 12 | * [2, 6], 13 | * [1, 1, 6] 14 | * ] 15 | */ 16 | 17 | /** 18 | * @param {number[]} candidates 19 | * @param {number} target 20 | * @return {number[][]} 21 | */ 22 | var combinationSum2 = function (candidates, target) { 23 | 24 | var res = []; 25 | var temp = []; 26 | candidates.sort((b, a) => b - a); 27 | helper(res, temp, candidates, target, 0); 28 | return res; 29 | }; 30 | 31 | function helper(res, temp, candidates, target, start) { 32 | if (target === 0) { 33 | return res.push([...temp]); 34 | } 35 | 36 | for (var i = start; i < candidates.length && candidates[i] <= target; i++) { 37 | if (i === start || candidates[i] !== candidates[i - 1]) { 38 | temp.push(candidates[i]); 39 | helper(res, temp, candidates, target - candidates[i], i + 1); 40 | temp.length -= 1; 41 | } 42 | } 43 | } 44 | 45 | console.log(combinationSum2([10, 1, 2, 7, 6, 1, 5], 8)); -------------------------------------------------------------------------------- /041-First-Missing-Positive.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/first-missing-positive/ 3 | * Difficulty:Hard 4 | * 5 | * Given an unsorted integer array, find the first missing positive integer. 6 | * For example, 7 | * Given [1,2,0] return 3, 8 | * and [3,4,-1,1] return 2. 9 | * Your algorithm should run in O(n) time and uses constant space. 10 | */ 11 | 12 | /** 13 | * 思路介绍 14 | * 1. 将第 i 个元素放到第 i 个坑 15 | * 2. 遍历所有的坑, 找到数字不对应的坑 16 | * 17 | * 例如 18 | * [ 3, 4, -1, 1 ] 19 | * --> [ -1, 4, 3, 1 ] 20 | * --> [ -1, 1, 3, 4 ] 21 | * --> [ 1, -1, 3, 4 ] 22 | * --> 2 23 | * 24 | * @param {number[]} nums 25 | * @return {number} 26 | */ 27 | var firstMissingPositive = function (nums) { 28 | var n = nums.length; 29 | var i = 0; 30 | // console.log(nums); 31 | while (i < n) { 32 | var c = nums[i]; 33 | if (c > 0 && n < n + 1 && c !== i + 1 && nums[c - 1] !== nums[i]) { 34 | swap(nums, c - 1, i); 35 | // console.log('-->', nums); 36 | } else { 37 | i++; 38 | } 39 | 40 | } 41 | 42 | // console.log(nums); 43 | for (var i = 0; i < n; i++) { 44 | if (nums[i] !== i + 1) return i + 1; 45 | } 46 | return n + 1; 47 | }; 48 | 49 | function swap(nums, i, j) { 50 | var tmp = nums[i]; 51 | nums[i] = nums[j]; 52 | nums[j] = tmp; 53 | } 54 | 55 | // console.log(firstMissingPositive([1, 2, 0])); 56 | console.log(firstMissingPositive([3, 4, -1, 1])); 57 | // console.log(firstMissingPositive([1, 1])); 58 | // console.log(firstMissingPositive([2, 2])); 59 | -------------------------------------------------------------------------------- /043-Multiply-Strings.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/multiply-strings/description/ 3 | * Difficulty:Medium 4 | * 5 | * Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2. 6 | * Note: 7 | * The length of both num1 and num2 is < 110. 8 | * Both num1 and num2 contains only digits 0-9. 9 | * Both num1 and num2 does not contain any leading zero. 10 | * You must not use any built-in BigInteger library or convert the inputs to integer directly. 11 | */ 12 | 13 | /** 14 | * @param {string} num1 15 | * @param {string} num2 16 | * @return {string} 17 | */ 18 | var multiply = function (num1, num2) { 19 | var m = num1.length; 20 | var n = num2.length; 21 | var arr = new Array(m + n).fill(0); 22 | for (var i = m - 1; i >= 0; i--) { 23 | for (var j = n - 1; j >= 0; j--) { 24 | var mul = (num1[i] - '0') * (num2[j] - '0'); 25 | 26 | var sum = mul + arr[i + j + 1]; 27 | 28 | arr[i + j] += Math.floor(sum / 10); 29 | arr[i + j + 1] = sum % 10; 30 | } 31 | } 32 | 33 | var str = arr.reduce((a, b) => { 34 | if (a === '' && b === 0) return a; 35 | return a + b; 36 | }, ''); 37 | 38 | return str ? str : '0'; 39 | 40 | }; 41 | 42 | console.log(multiply('89', '45')); 43 | console.log(multiply('123', '123')); 44 | console.log(multiply('123', '0')); 45 | 46 | 47 | -------------------------------------------------------------------------------- /046-Permutations.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/permutations/description/ 3 | * Difficulty:Medium 4 | * 5 | * Given a collection of distinct numbers, return all possible permutations. 6 | * For example, 7 | * [1,2,3] have the following permutations: 8 | * [ 9 | * [1,2,3], 10 | * [1,3,2], 11 | * [2,1,3], 12 | * [2,3,1], 13 | * [3,1,2], 14 | * [3,2,1] 15 | * ] 16 | */ 17 | 18 | /** 19 | * @param {number[]} nums 20 | * @return {number[][]} 21 | */ 22 | var permute = function (nums) { 23 | if (!nums.length) return []; 24 | var res = [[]]; 25 | for (var i = 0; i < nums.length; i++) { 26 | var len = res.length; 27 | for (var j = 0; j < len; j++) { 28 | var oldArr = res.shift(); 29 | for (var k = 0; k <= oldArr.length; k++) { 30 | var newArr = oldArr.slice(); 31 | newArr.splice(k, 0, nums[i]); 32 | res.push(newArr); 33 | } 34 | } 35 | } 36 | return res; 37 | }; 38 | console.log(permute([1, 2, 3])); -------------------------------------------------------------------------------- /047-Permutations-II.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/permutations-ii/description/ 3 | * Difficulty:Medium 4 | * 5 | * Given a collection of numbers that might contain duplicates, return all possible unique permutations. 6 | * For example, 7 | * [1,1,2] have the following unique permutations: 8 | * [ 9 | * [1,1,2], 10 | * [1,2,1], 11 | * [2,1,1] 12 | * ] 13 | */ 14 | 15 | /** 16 | * @param {number[]} nums 17 | * @return {number[][]} 18 | */ 19 | var permuteUnique = function (nums) { 20 | if (!nums.length) return []; 21 | nums.sort((a, b) => a - b); 22 | var res = [[]]; 23 | for (var i = 0; i < nums.length; i++) { 24 | var len = res.length; 25 | for (var j = 0; j < len; j++) { 26 | var oldArr = res.shift(); 27 | if (i > 0 && nums[i] === nums[i - 1]) { 28 | var k = oldArr.lastIndexOf(nums[i]); 29 | } else { 30 | k = 0; 31 | } 32 | for (; k <= oldArr.length; k++) { 33 | 34 | if (k === oldArr.length || nums[i] !== oldArr[k]) { 35 | var newArr = oldArr.slice(); 36 | newArr.splice(k, 0, nums[i]); 37 | // console.log(oldArr, newArr); 38 | res.push(newArr); 39 | } 40 | 41 | } 42 | } 43 | } 44 | return res; 45 | }; 46 | 47 | console.log(permuteUnique([1, 2, 2, 1])); -------------------------------------------------------------------------------- /051-N-Queens.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/n-queens/description/ 3 | * Difficulty:Hard 4 | * 5 | * The n-queens puzzle is the problem of placing n queens on an n×n chessboard 6 | * such that no two queens attack each other. 7 | * 8 | * Given an integer n, return all distinct solutions to the n-queens puzzle. 9 | * Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space respectively. 10 | * 11 | * For example, 12 | * There exist two distinct solutions to the 4-queens puzzle: 13 | * [ 14 | * [".Q..", // Solution 1 15 | * "...Q", 16 | * "Q...", 17 | * "..Q."], 18 | * 19 | * ["..Q.", // Solution 2 20 | * "Q...", 21 | * "...Q", 22 | * ".Q.."] 23 | * ] 24 | * 2,1 3,2 25 | */ 26 | 27 | /** 28 | * @param {number} n 29 | * @return {string[][]} 30 | */ 31 | var solveNQueens = function (n) { 32 | var ret = []; 33 | var board = []; 34 | for (var i = 0; i < n; i++) { 35 | board.push(new Array(n).fill('.')); 36 | } 37 | helper(board, 0, ret); 38 | return ret; 39 | }; 40 | 41 | function helper(board, col, ret) { 42 | if (col === board.length) { 43 | ret.push(construct(board)); 44 | } else { 45 | for (var i = 0; i < board.length; i++) { 46 | if (check(board, i, col)) { 47 | board[i][col] = 'Q'; 48 | helper(board, col + 1, ret); 49 | board[i][col] = '.'; 50 | } 51 | } 52 | } 53 | } 54 | 55 | function check(board, x, y) { 56 | for (var i = 0; i < board.length; i++) { 57 | for (var j = 0; j < y; j++) { 58 | if (board[i][j] === 'Q' && 59 | (i === x || i + j === x + y || i + y === j + x)) return false; 60 | } 61 | } 62 | return true; 63 | } 64 | function construct(board) { 65 | return board.map(arr => arr.join('')); 66 | } 67 | console.log(solveNQueens(4)); -------------------------------------------------------------------------------- /053-Maximum-Subarray.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/maximum-subarray/description/ 3 | * Difficulty:Easy 4 | * 5 | * Find the contiguous subarray within an array (containing at least one number) which has the largest sum. 6 | * 7 | * For example, given the array [-2,1,-3,4,-1,2,1,-5,4], 8 | * the contiguous subarray [4,-1,2,1] has the largest sum = 6. 9 | */ 10 | 11 | /** 12 | * 13 | * https://discuss.leetcode.com/topic/6413/dp-solution-some-thoughts 14 | * 15 | * @param {number[]} nums 16 | * @return {number} 17 | */ 18 | var maxSubArray = function (nums) { 19 | 20 | var dp = []; 21 | var max = dp[0] = nums[0]; 22 | 23 | for (var i = 1; i < nums.length; i++) { 24 | dp[i] = nums[i] + (dp[i - 1] > 0 ? dp[i - 1] : 0); 25 | max = Math.max(dp[i], max); 26 | } 27 | 28 | return max; 29 | 30 | }; 31 | 32 | /** 33 | * https://discuss.leetcode.com/topic/5000/accepted-o-n-solution-in-java/11 34 | * @param nums 35 | * @returns {*} 36 | */ 37 | var maxSubArray = function (nums) { 38 | 39 | var max = nums[0]; 40 | var sum = nums[0]; 41 | 42 | for (var i = 1; i < nums.length; i++) { 43 | sum = sum > 0 ? (sum + nums[i]) : nums[i]; 44 | max = Math.max(sum, max); 45 | } 46 | return max; 47 | 48 | }; 49 | 50 | console.log(maxSubArray([1, 1, 1]) == 3); 51 | console.log(maxSubArray([-1, -1, -1]) == -1); 52 | console.log(maxSubArray([-2, 1, -3, 4, -1, 2, 1, -5, 4]) == 6); 53 | console.log(maxSubArray([-2, -1]) == -1); 54 | console.log(maxSubArray([-1]) == -1); 55 | console.log(maxSubArray([-1, 0]) == 0); 56 | 57 | -------------------------------------------------------------------------------- /062-Unique-Paths.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/unique-paths/ 3 | * Difficulty:Medium 4 | * 5 | * A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). 6 | * The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below). 7 | * How many possible unique paths are there? 8 | * Note: m and n will be at most 100. 9 | * 10 | */ 11 | 12 | /** 13 | * @param {number} m 14 | * @param {number} n 15 | * @return {number} 16 | */ 17 | var uniquePaths = function (m, n) { 18 | 19 | var arr = []; 20 | for (var i = 0; i < m; i++) { 21 | for (var j = 0; j < n; j++) { 22 | if (i === 0 && j === 0) arr[0] = 1; 23 | else { 24 | var left = j - 1 < 0 ? 0 : arr[i * n + j - 1]; 25 | var top = i - 1 < 0 ? 0 : arr[(i - 1) * n + j]; 26 | arr[i * n + j] = left + top; 27 | } 28 | 29 | } 30 | } 31 | return arr[arr.length - 1]; 32 | }; 33 | 34 | console.log(uniquePaths(2, 2)); -------------------------------------------------------------------------------- /063-Unique-Paths-II.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/unique-paths-ii/description/ 3 | * Difficulty:Medium 4 | * 5 | * Follow up for "Unique Paths": 6 | * Now consider if some obstacles are added to the grids. How many unique paths would there be? 7 | * An obstacle and empty space is marked as 1 and 0 respectively in the grid. 8 | * For example, 9 | * There is one obstacle in the middle of a 3x3 grid as illustrated below. 10 | * [ 11 | * [0,0,0], 12 | * [0,1,0], 13 | * [0,0,0] 14 | * ] 15 | * The total number of unique paths is 2. 16 | * Note: m and n will be at most 100. 17 | */ 18 | 19 | /** 20 | * @param {number[][]} obstacleGrid 21 | * @return {number} 22 | */ 23 | var uniquePathsWithObstacles = function (obstacleGrid) { 24 | var m = obstacleGrid.length; 25 | var n = obstacleGrid[0].length; 26 | var dp = []; 27 | while (dp.push(new Array(n + 1).fill(0)) <= m); 28 | 29 | dp[0][1] = 1; 30 | for (var i = 1; i <= m; i++) { 31 | for (var j = 1; j <= n; j++) { 32 | if (!obstacleGrid[i - 1][j - 1]) 33 | dp[i][j] = dp[i][j - 1] + dp[i - 1][j]; 34 | } 35 | } 36 | return dp[m][n]; 37 | }; 38 | 39 | console.log(uniquePathsWithObstacles([[0, 0]])); -------------------------------------------------------------------------------- /064-Minimum-Path-Sum.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/minimum-path-sum/description/ 3 | * Difficulty:Medium 4 | * 5 | * Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. 6 | * Note: You can only move either down or right at any point in time. 7 | * Example 1: 8 | * [ 9 | * [1,3,1], 10 | * [1,5,1], 11 | * [4,2,1] 12 | * ] 13 | * Given the above grid map, return 7. Because the path 1→3→1→1→1 minimizes the sum. 14 | */ 15 | 16 | /** 17 | * @param {number[][]} grid 18 | * @return {number} 19 | */ 20 | var minPathSum = function (grid) { 21 | 22 | var m = grid.length; 23 | var n = grid[0].length; 24 | var dp = []; 25 | while (dp.push(new Array(n + 1).fill(Number.MAX_VALUE)) <= m); 26 | dp[0][1] = 0; 27 | dp[1][0] = 0; 28 | 29 | for (var i = 1; i <= m; i++) { 30 | for (var j = 1; j <= n; j++) { 31 | dp[i][j] = grid[i - 1][j - 1] + Math.min(dp[i][j - 1], dp[i - 1][j]); 32 | } 33 | } 34 | 35 | return dp[m][n]; 36 | 37 | }; 38 | 39 | console.log(minPathSum([[1, 2], [1, 1]])); -------------------------------------------------------------------------------- /067-Add-Binary.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/add-binary/description/ 3 | * Difficulty:Easy 4 | * Given two binary strings, return their sum (also a binary string). 5 | * For example, 6 | * a = "11" 7 | * b = "1" 8 | * Return "100". 9 | */ 10 | 11 | /** 12 | * @param {string} a 13 | * @param {string} b 14 | * @return {string} 15 | */ 16 | var addBinary = function (a, b) { 17 | var c = 0; 18 | var alen = a.length; 19 | var blen = b.length; 20 | var ans = ''; 21 | for (var i = 0; i < Math.max(alen, blen); i++) { 22 | var ai = i < alen ? parseInt(a[alen - i - 1]) : 0; 23 | var bi = i < blen ? parseInt(b[blen - i - 1]) : 0; 24 | 25 | var sum = ai + bi + c; 26 | // console.log(ai, bi, c); 27 | if (sum < 2) { 28 | ans = sum + ans; 29 | c = 0; 30 | } 31 | else { 32 | ans = (sum - 2) + ans; 33 | c = 1; 34 | } 35 | } 36 | if (c) { 37 | ans = 1 + ans; 38 | } 39 | 40 | return ans; 41 | 42 | }; 43 | 44 | console.log(addBinary('111', '1')); -------------------------------------------------------------------------------- /070-Climbing-Stairs.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/climbing-stairs/description 3 | * Difficulty:Easy 4 | * 5 | * You are climbing a stair case. It takes n steps to reach to the top. 6 | * Each time you can either climb 1 or 2 steps. 7 | * In how many distinct ways can you climb to the top? 8 | * 9 | * Note: Given n will be a positive integer. 10 | * 11 | * 答案: 12 | * 13 | * https://leetcode.com/articles/climbing-stairs/ 14 | */ 15 | 16 | /** 17 | * @param {number} n 18 | * @return {number} 19 | */ 20 | var climbStairs = function (n) { 21 | if (n < 2) return 1; 22 | return climbStairs(n - 1) + climbStairs(n - 2); 23 | }; 24 | 25 | /** 26 | * @param {number} n 27 | * @return {number} 28 | */ 29 | var climbStairs2 = function (n) { 30 | var ways = [1, 1]; 31 | for (var i = 2; i <= n; i++) { 32 | ways[i] = ways[i - 1] + ways[i - 2]; 33 | } 34 | return ways[n]; 35 | }; 36 | 37 | console.log(climbStairs(1)); 38 | console.log(climbStairs(2)); 39 | console.log(climbStairs(3)); 40 | console.log(climbStairs(4)); 41 | 42 | console.log('=============='); 43 | 44 | console.log(climbStairs2(1)); 45 | console.log(climbStairs2(2)); 46 | console.log(climbStairs2(3)); 47 | console.log(climbStairs2(4)); -------------------------------------------------------------------------------- /083-Remove-Duplicates-from-Sorted-List.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/ 3 | * Difficulty:Easy 4 | * 5 | * Given a sorted linked list, delete all duplicates such that each element appear only once. 6 | * 7 | * For example, 8 | * Given 1->1->2, return 1->2. 9 | * Given 1->1->2->3->3, return 1->2->3. 10 | * 11 | */ 12 | 13 | 14 | /** 15 | * Definition for singly-linked list. 16 | * function ListNode(val) { 17 | * this.val = val; 18 | * this.next = null; 19 | * } 20 | */ 21 | 22 | /** 23 | * @param {ListNode} head 24 | * @return {ListNode} 25 | */ 26 | var deleteDuplicates = function (head) { 27 | 28 | var p = head; 29 | if (!p) return p; 30 | var all = [p.val]; 31 | var t = p.next; 32 | 33 | while (t) { 34 | 35 | if (all.indexOf(t.val) == -1) { 36 | all.push(t.val); 37 | p = t; 38 | t = p.next; 39 | } else { 40 | t = t.next; 41 | p.next = t; 42 | } 43 | } 44 | return head; 45 | 46 | }; 47 | 48 | /** 49 | * 改进方案 50 | * 51 | * @param {ListNode} head 52 | * @return {ListNode} 53 | */ 54 | var deleteDuplicates2 = function (head) { 55 | 56 | var p = head; 57 | while (p && p.next) { 58 | if (p.val == p.next.val) { 59 | p.next = p.next.next; 60 | } else { 61 | p = p.next; 62 | } 63 | } 64 | return head; 65 | 66 | }; 67 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /085-Maximal-Rectangle.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/maximal-rectangle/description/ 3 | * Difficulty:Hard 4 | * Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area. 5 | * For example, given the following matrix: 6 | * 7 | * 1 0 1 0 0 8 | * 1 0 1 1 1 9 | * 1 1 1 1 1 10 | * 1 0 0 1 0 11 | * 12 | * Return 6. 13 | * 14 | */ 15 | 16 | /** 17 | * @param {character[][]} matrix 18 | * @return {number} 19 | */ 20 | var maximalRectangle = function (matrix) { 21 | var m = matrix.length; 22 | if (!m) return 0; 23 | var n = matrix[0].length; 24 | if (!n) return 0; 25 | 26 | var dp = []; 27 | while (dp.push(new Array(n + 1).fill([0, 0])) <= m) ; 28 | var max = 0; 29 | for (var i = 1; i <= m; i++) { 30 | for (var j = 1; j <= n; j++) { 31 | if (matrix[i - 1][j - 1] != 0) { 32 | dp[i][j] = [1, 1]; 33 | var top = dp[i - 1][j]; 34 | var left = dp[i][j - 1]; 35 | if (top[0] * top[1] === 0) { 36 | dp[i][j] = [1, left[1] + 1]; 37 | } else if (left[0] * left[1] === 0) { 38 | dp[i][j] = [top[0] + 1, 1]; 39 | } else { 40 | var x1 = (top[0] + 1); 41 | var y1 = Math.min(top[1], left[1] + 1); 42 | var p1 = x1 * y1; 43 | 44 | var x2 = Math.min(left[0], top[0] + 1); 45 | var y2 = (left[1] + 1); 46 | var p2 = x2 * y2; 47 | 48 | if (p1 >= p2) { 49 | dp[i][j] = [x1, y1]; 50 | } else { 51 | dp[i][j] = [x2, y2]; 52 | } 53 | 54 | } 55 | 56 | max = Math.max(max, dp[i][j][0] * dp[i][j][1]); 57 | } 58 | 59 | } 60 | } 61 | 62 | // for (var i = 0; i < matrix.length; i++) { 63 | // console.log(matrix[i].split('')); 64 | // } 65 | // console.log('======================'); 66 | // 67 | // for (var i = 1; i < dp.length; i++) { 68 | // console.log(dp[i].slice(1).join(' | ')); 69 | // } 70 | 71 | return max; 72 | }; 73 | 74 | // console.log(maximalRectangle([ 75 | // [1, 0, 1, 0, 0], 76 | // [1, 0, 1, 1, 1], 77 | // [1, 1, 1, 1, 1], 78 | // [1, 0, 0, 1, 0], 79 | // ])); 80 | 81 | // console.log(maximalRectangle( 82 | // ["10100", "10111", "11111", "10010"] 83 | // )); 84 | 85 | console.log(maximalRectangle(["010", "110", "011", "010"])); -------------------------------------------------------------------------------- /091-Decode-Ways.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/decode-ways/description/ 3 | * Difficulty:Medium 4 | * 5 | * A message containing letters from A-Z is being encoded to numbers using the following mapping: 6 | * 7 | * 'A' -> 1 8 | * 'B' -> 2 9 | * ... 10 | * 'Z' -> 26 11 | * 12 | * Given an encoded message containing digits, determine the total number of ways to decode it. 13 | * For example, 14 | * Given encoded message "12", it could be decoded as "AB" (1 2) or "L" (12). 15 | * The number of ways decoding "12" is 2. 16 | * 17 | */ 18 | 19 | 20 | /** 21 | * @param {string} s 22 | * @return {number} 23 | */ 24 | var numDecodings = function (s) { 25 | if (!s.length) return 0; 26 | var dp = []; 27 | var n = s.length; 28 | dp[0] = 1; 29 | 30 | for (var i = 1; i <= n; i++) { 31 | dp[i] = 0; 32 | var sum1 = parseInt(s[i - 1]); 33 | var sum2 = parseInt(s[i - 2] + s[i - 1]); 34 | 35 | if (sum1 > 0 && sum1 < 10) { 36 | dp[i] += dp[i - 1]; 37 | } 38 | if (sum2 > 9 && sum2 < 27) { 39 | dp[i] += dp[i - 2]; 40 | } 41 | } 42 | // console.log(dp); 43 | return dp[n]; 44 | }; 45 | 46 | console.log(numDecodings("0")); 47 | console.log(numDecodings("12")); 48 | console.log(numDecodings("10")); 49 | console.log(numDecodings("01")); 50 | console.log(numDecodings("100")); -------------------------------------------------------------------------------- /095-Unique-Binary-Search-Trees-II.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/unique-binary-search-trees-ii/description/ 3 | * Difficulty:Medium 4 | * 5 | * Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1...n. 6 | * 7 | * For example, 8 | * Given n = 3, your program should return all 5 unique BST's shown below. 9 | * 10 | * 1 3 3 2 1 11 | * \ / / / \ \ 12 | * 3 2 1 1 3 2 13 | * / / \ \ 14 | * 2 1 2 3 15 | */ 16 | 17 | 18 | // Definition for a binary tree node. 19 | function TreeNode(val) { 20 | this.val = val; 21 | this.left = this.right = null; 22 | } 23 | 24 | /** 25 | * @param {number} n 26 | * @return {TreeNode[]} 27 | */ 28 | var generateTrees = function (n) { 29 | if (n === 0) return []; 30 | return _genTrees(1, n); 31 | }; 32 | 33 | function _genTrees(start, end) { 34 | if (start > end) return [null]; 35 | // if (start === end) return [new TreeNode(start)]; 36 | var list = []; 37 | for (var i = start; i <= end; i++) { 38 | 39 | var leftTrees = _genTrees(start, i - 1); 40 | var rightTrees = _genTrees(i + 1, end); 41 | // console.log(leftTrees, rightTrees); 42 | 43 | leftTrees.forEach(left => { 44 | rightTrees.forEach(right => { 45 | 46 | var root = new TreeNode(i); 47 | root.left = left; 48 | root.right = right; 49 | list.push(root); 50 | }) 51 | }) 52 | } 53 | return list; 54 | } 55 | 56 | console.log(generateTrees(0)); -------------------------------------------------------------------------------- /096-Unique-Binary-Search-Trees.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/unique-binary-search-trees/description/ 3 | * Difficulty:Medium 4 | * 5 | * Given n, how many structurally unique BST's (binary search trees) that store values 1...n? 6 | * For example, 7 | * Given n = 3, there are a total of 5 unique BST's. 8 | * 9 | * 1 3 3 2 1 10 | * \ / / / \ \ 11 | * 3 2 1 1 3 2 12 | * / / \ \ 13 | * 2 1 2 3 14 | * 15 | */ 16 | 17 | 18 | /** 19 | * G(n): the number of unique BST for a sequence of length n. 20 | * F(i, n), 1 <= i <= n: the number of unique BST, where the number i is the root of BST, and the sequence ranges from 1 to n. 21 | * 22 | * G(n) = F(1, n) + F(2, n) + ... + F(n, n). 23 | * G(0)=1, G(1)=1. 24 | * 25 | * F(i, n) = G(i-1) * G(n-i) 1 <= i <= n 26 | * G(n) = G(0) * G(n-1) + G(1) * G(n-2) + … + G(n-1) * G(0) 27 | * 28 | * 29 | * 30 | */ 31 | 32 | /** 33 | * @param {number} n 34 | * @return {number} 35 | */ 36 | var numTrees = function (n) { 37 | var dp = [1, 1]; 38 | 39 | for (var i = 2; i <= n; i++) { 40 | dp[i] = 0; 41 | for (var j = 0; j < i; j++) { 42 | dp[i] += dp[j] * dp[i - j - 1]; 43 | } 44 | } 45 | return dp[n]; 46 | }; 47 | 48 | console.log(numTrees(1)); 49 | console.log(numTrees(2)); 50 | console.log(numTrees(3)); -------------------------------------------------------------------------------- /100-Same-Tree.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/same-tree/description/ 3 | * Difficulty:Easy 4 | * 5 | * Given two binary trees, write a function to check if they are equal or not. 6 | * Two binary trees are considered equal if they are structurally identical and the nodes have the same value. 7 | */ 8 | 9 | /** 10 | * Definition for a binary tree node. 11 | * function TreeNode(val) { 12 | * this.val = val; 13 | * this.left = this.right = null; 14 | * } 15 | */ 16 | /** 17 | * @param {TreeNode} p 18 | * @param {TreeNode} q 19 | * @return {boolean} 20 | */ 21 | var isSameTree = function (p, q) { 22 | 23 | if (!p && !q) return true; 24 | if (!p || !q) return false; 25 | if (p.val !== q.val) return false; 26 | 27 | return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); 28 | 29 | }; 30 | 31 | 32 | -------------------------------------------------------------------------------- /101-Symmetric-Tree.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/symmetric-tree/description/ 3 | * Difficulty:Easy 4 | * 5 | * Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). 6 | * For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 7 | * 1 8 | * / \ 9 | * 2 2 10 | * / \ / \ 11 | * 3 4 4 3 12 | * But the following [1,2,2,null,3,null,3] is not: 13 | * 1 14 | * / \ 15 | * 2 2 16 | * \ \ 17 | * 3 3 18 | * Note: 19 | * Bonus points if you could solve it both recursively and iteratively. 20 | */ 21 | 22 | /** 23 | * Definition for a binary tree node. 24 | * function TreeNode(val) { 25 | * this.val = val; 26 | * this.left = this.right = null; 27 | * } 28 | */ 29 | /** 30 | * @param {TreeNode} root 31 | * @return {boolean} 32 | */ 33 | var isSymmetric = function (root) { 34 | if (!root) return true; 35 | return helper(root.left, root.right) 36 | }; 37 | 38 | function helper(a, b) { 39 | if (!a && !b) return true; 40 | if (!a) return false; 41 | if (!b) return false; 42 | if (a.val !== b.val) return false; 43 | return helper(a.left, b.right) && helper(a.right, b.left); 44 | } 45 | 46 | console.log(isSymmetric({ 47 | val: 1, 48 | left: { 49 | val: 2, 50 | left: { 51 | val: 3, 52 | }, 53 | right: { 54 | val: 4 55 | } 56 | }, 57 | right: { 58 | val: 2, 59 | left: { 60 | val: 4, 61 | }, 62 | right: { 63 | val: 3 64 | } 65 | } 66 | })) -------------------------------------------------------------------------------- /111-Minimum-Depth-of-Binary-Tree.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/minimum-depth-of-binary-tree/description/ 3 | * Difficulty:Easy 4 | * 5 | * Given a binary tree, find its minimum depth. 6 | * The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 7 | * 8 | */ 9 | 10 | /** 11 | * Definition for a binary tree node. 12 | * function TreeNode(val) { 13 | * this.val = val; 14 | * this.left = this.right = null; 15 | * } 16 | */ 17 | /** 18 | * @param {TreeNode} root 19 | * @return {number} 20 | */ 21 | var minDepth = function (root) { 22 | 23 | if (!root) return 0; 24 | 25 | var left = minDepth(root.left); 26 | var right = minDepth(root.right); 27 | 28 | return (left == 0 || right == 0) ? left + right + 1 : Math.min(left, right) + 1; 29 | 30 | }; -------------------------------------------------------------------------------- /121-Best-Time-to-Buy-and-Sell-Stock.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ 3 | * Difficulty:Easy 4 | * 5 | * Say you have an array for which the ith element is the price of a given stock on day i. 6 | * If you were only permitted to complete at most one transaction 7 | * (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. 8 | * 9 | * Example 1: 10 | * Input: [7, 1, 5, 3, 6, 4] 11 | * Output: 5 12 | * max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price) 13 | * 14 | * Example 2: 15 | * Input: [7, 6, 4, 3, 1] 16 | * Output: 0 17 | * In this case, no transaction is done, i.e. max profit = 0. 18 | */ 19 | 20 | /** 21 | * @param {number[]} prices 22 | * @return {number} 23 | */ 24 | var maxProfit = function (prices) { 25 | if (!prices.length) return 0; 26 | var min = prices[0]; 27 | var max = 0; 28 | for (var i = 1; i < prices.length; i++) { 29 | var tmp = prices[i]; 30 | if (tmp < min) min = tmp; 31 | else if (tmp - min > max) max = tmp - min; 32 | } 33 | return max; 34 | 35 | }; 36 | 37 | console.log(maxProfit([7, 1, 5, 3, 6, 4])); 38 | console.log(maxProfit([7, 6, 4, 3, 1])); 39 | -------------------------------------------------------------------------------- /136-Single-Number.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * https://leetcode.com/problems/single-number/description 4 | * Difficulty:Easy 5 | * 6 | * Given an array of integers, every element appears twice except for one. Find that single one. 7 | * 8 | * Note: 9 | * Your algorithm should have a linear runtime complexity. 10 | * 11 | * Could you implement it without using extra memory? 12 | * 13 | * 14 | * https://discuss.leetcode.com/topic/1916/my-o-n-solution-using-xor/1 15 | * Hint: known that A XOR A = 0 and the XOR operator is commutative 16 | */ 17 | 18 | /** 19 | * @param {number[]} nums 20 | * @return {number} 21 | */ 22 | var singleNumber = function (nums) { 23 | 24 | return nums.reduce((ret, n) => ret ^= n, 0); 25 | 26 | }; 27 | 28 | console.log(singleNumber([1, 2, 2]) == 1); 29 | console.log(singleNumber([1, 2, 1]) == 2); -------------------------------------------------------------------------------- /138-Copy-List-with-Random-Pointer.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/copy-list-with-random-pointer/description/ 3 | * Difficulty:Medium 4 | * 5 | * A linked list is given such that each node contains an additional random pointer 6 | * which could point to any node in the list or null. 7 | * 8 | * Return a deep copy of the list. 9 | * 10 | */ 11 | 12 | 13 | // Definition for singly-linked list with a random pointer. 14 | function RandomListNode(label) { 15 | this.label = label; 16 | this.next = this.random = null; 17 | } 18 | 19 | /** 20 | * @param {RandomListNode} head 21 | * @return {RandomListNode} 22 | */ 23 | var copyRandomList = function (head) { 24 | 25 | print(head); 26 | 27 | var p = head; 28 | // A->B->C 29 | // A->A'->B->B'->C->C' 30 | while (p) { 31 | var copy = new RandomListNode(p.label + "'"); 32 | copy.next = p.next; 33 | p.next = copy; 34 | p = copy.next; 35 | } 36 | 37 | print(head); 38 | 39 | // 构造 A' B' C'.random 40 | p = head; 41 | while (p) { 42 | p.next.random = p.random ? p.random.next : null; 43 | p = p.next.next; 44 | } 45 | 46 | print(head); 47 | // 构造 copy 48 | var pp = new RandomListNode(0); 49 | var copy = pp; 50 | p = head; 51 | while (p) { 52 | pp.next = p.next; 53 | pp = pp.next; 54 | 55 | p.next = pp.next; 56 | p = p.next; 57 | 58 | } 59 | 60 | print(head); 61 | print(copy.next); 62 | 63 | return copy.next; 64 | }; 65 | 66 | var a = new RandomListNode('A'); 67 | var b = new RandomListNode('B'); 68 | var c = new RandomListNode('C'); 69 | 70 | a.next = b; 71 | b.next = c; 72 | a.random = c; 73 | b.random = c; 74 | c.random = a; 75 | 76 | function print(h) { 77 | var ls = []; 78 | var rs = []; 79 | while (h) { 80 | ls.push(h.label); 81 | rs.push(h.random ? h.random.label : 'nil'); 82 | h = h.next; 83 | } 84 | console.log(ls.join('->')); 85 | console.log(rs.join(' ')); 86 | console.log('-------------'); 87 | } 88 | 89 | console.log(copyRandomList(a)); -------------------------------------------------------------------------------- /139-Word-Break.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/word-break/description/ 3 | * Difficulty:Medium 4 | * 5 | * Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words. You may assume the dictionary does not contain duplicate words. 6 | * 7 | * For example, given 8 | * s = "leetcode", 9 | * dict = ["leet", "code"]. 10 | * Return true because "leetcode" can be segmented as "leet code". 11 | */ 12 | 13 | /** 14 | * @param {string} s 15 | * @param {string[]} wordDict 16 | * @return {boolean} 17 | */ 18 | var wordBreak = function (s, wordDict) { 19 | var n = s.length; 20 | 21 | var dp = [true]; 22 | for (var i = 1; i < n + 1; i++) { 23 | 24 | for (var j = 0; j < i; j++) { 25 | 26 | if (dp[j] && wordDict.indexOf(s.substring(j, i)) > -1) { 27 | dp[i] = true; 28 | break; 29 | } else { 30 | dp[i] = false; 31 | } 32 | 33 | } 34 | 35 | } 36 | // console.log(dp); 37 | return dp[n]; 38 | 39 | }; 40 | 41 | console.log(wordBreak("leetcode", ["lee", "leet", "cod", "code"])); 42 | console.log(wordBreak("", ["lee", "leet", "cod", "code"])); 43 | console.log(wordBreak("lee", ["l", "leet", "cod", "code", "e"])); 44 | console.log(wordBreak("lee", ["l", "leet", "cod", "code", "leetco"])); -------------------------------------------------------------------------------- /151-Reverse-Words-in-a-String.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/reverse-words-in-a-string/description/ 3 | * Difficulty:Medium 4 | * 5 | * Given an input string, reverse the string word by word. 6 | * 7 | * For example, 8 | * Given s = "the sky is blue", 9 | * return "blue is sky the". 10 | */ 11 | 12 | /** 13 | * @param {string} str 14 | * @returns {string} 15 | */ 16 | var reverseWords = function (str) { 17 | return str.split(' ') 18 | .filter(w => w) 19 | .reverse() 20 | .join(' '); 21 | }; -------------------------------------------------------------------------------- /152-Maximum-Product-Subarray.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/maximum-product-subarray/description/ 3 | * Difficulty:Medium 4 | * 5 | * Find the contiguous subarray within an array (containing at least one number) 6 | * which has the largest product. 7 | * 8 | * For example, given the array [2,3,-2,4], 9 | * the contiguous subarray [2,3] has the largest product = 6. 10 | */ 11 | 12 | /** 13 | * @param {number[]} nums 14 | * @return {number} 15 | */ 16 | var maxProduct = function (nums) { 17 | var a = nums[0]; 18 | var imin = a; 19 | var imax = a; 20 | var max = a; 21 | 22 | for (var i = 1; i < nums.length; i++) { 23 | var t = nums[i]; 24 | if (t < 0) { 25 | var tmp = imin; 26 | imin = imax; 27 | imax = tmp; 28 | } 29 | imax = Math.max(t, t * imax); 30 | imin = Math.min(t, t * imin); 31 | max = Math.max(max, imax); 32 | } 33 | 34 | return max; 35 | }; 36 | 37 | console.log(maxProduct([-1])); 38 | console.log(maxProduct([1])); 39 | console.log(maxProduct([1, 2, 3, -4])); 40 | console.log(maxProduct([2, 3, -2, 4])); -------------------------------------------------------------------------------- /162-Find-Peak-Element.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/find-peak-element/description/ 3 | * Difficulty:Medium 4 | * 5 | * A peak element is an element that is greater than its neighbors. 6 | * Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. 7 | * The array may contain multiple peaks, in that case return the index to any one of the peaks is fine. 8 | * You may imagine that num[-1] = num[n] = -∞. 9 | * For example, in array [1, 2, 3, 1], 3 is a peak element and your function should return the index number 2. 10 | */ 11 | 12 | /** 13 | * 14 | * @param {number[]} nums 15 | * @return {number} 16 | */ 17 | var findPeakElement = function (nums) { 18 | for (var i = 1; i < nums.length; i++) { 19 | if (nums[i - 1] > nums[i]) return i - 1; 20 | } 21 | return nums.length - 1; 22 | }; 23 | 24 | // ================================================ 25 | 26 | /** 27 | * 二分查找 28 | * 29 | * @param {number[]} nums 30 | * @return {number} 31 | */ 32 | var findPeakElement = function (nums) { 33 | if (nums.length <= 1) return 0; 34 | var l = 0; 35 | var h = nums.length - 1; 36 | while (l < h) { 37 | var mid = Math.floor((l + h) / 2); 38 | if (nums[mid] > nums[mid + 1]) h = mid; 39 | else l = mid + 1; 40 | } 41 | return l; 42 | }; 43 | 44 | // ================================================ 45 | 46 | console.log(findPeakElement([3, 2, 1]), 0); 47 | console.log(findPeakElement([1, 2, 3, 1]), 2); 48 | console.log(findPeakElement([1, 3, 2]), 1); 49 | -------------------------------------------------------------------------------- /167-Two-Sum-II-Input-array-is-sorted.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/ 3 | * Difficulty:Easy 4 | * 5 | * Given an array of integers that is already sorted in ascending order, 6 | * find two numbers such that they add up to a specific target number. 7 | * The function twoSum should return indices of the two numbers such that they add up to the target, 8 | * where index1 must be less than index2. 9 | * 10 | * Please note that your returned answers (both index1 and index2) are not zero-based. 11 | * You may assume that each input would have exactly one solution and you may not use the same element twice. 12 | * 13 | * Input: numbers=[2, 7, 11, 15], target=9 14 | * Output: index1=1, index2=2 15 | * 16 | */ 17 | 18 | /** 19 | * @param {number[]} numbers 20 | * @param {number} target 21 | * @return {number[]} 22 | */ 23 | var twoSum = function (numbers, target) { 24 | 25 | for (var i = 0; i < numbers.length - 1; i++) { 26 | for (var j = i + 1; j < numbers.length; j++) { 27 | if (numbers[i] + numbers[j] === target) return [i + 1, j + 1]; 28 | } 29 | } 30 | }; 31 | 32 | // ========================================== 33 | 34 | /** 35 | * 双指针 36 | * @param {number[]} nums 37 | * @param {number} target 38 | * @return {number[]} 39 | */ 40 | var twoSum = function (nums, target) { 41 | 42 | var lo = 0; 43 | var hi = nums.length - 1; 44 | 45 | while (nums[lo] + nums[hi] !== target) { 46 | if (nums[lo] + nums[hi] > target) hi--; 47 | else lo++; 48 | } 49 | return [lo + 1, hi + 1]; 50 | }; 51 | 52 | console.log(twoSum([2, 7, 11, 15], 9)); -------------------------------------------------------------------------------- /171-Excel-Sheet-Column-Number.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/excel-sheet-column-number/description/ 3 | * Difficulty:Easy 4 | * 5 | * Given a column title as appear in an Excel sheet, return its corresponding column number. 6 | * For example: 7 | * A -> 1 8 | * B -> 2 9 | * C -> 3 10 | * ... 11 | * Z -> 26 12 | * AA -> 27 13 | * AB -> 28 14 | */ 15 | 16 | /** 17 | * @param {string} s 18 | * @return {number} 19 | */ 20 | var titleToNumber = function (s) { 21 | 22 | var num = 0; 23 | var aCode = 'A'.charCodeAt(0); 24 | for (var i = 0; i < s.length; i++) { 25 | var n = 1 + s.charCodeAt(i) - aCode; 26 | num = num * 26 + n; 27 | } 28 | return num; 29 | }; 30 | 31 | console.log(titleToNumber('AA')); -------------------------------------------------------------------------------- /190-Reverse-Bits.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/reverse-bits/#/description 3 | * Difficulty:Easy 4 | * 5 | * Reverse bits of a given 32 bits unsigned integer. 6 | * 7 | * For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), 8 | * 9 | * return 964176192 (represented in binary as 00111001011110000010100101000000). 10 | * 11 | * 00000010100101000001111010011100 12 | * 00111001011110000010100101000000 13 | */ 14 | 15 | /** 16 | * @param {number} n - a positive integer 17 | * @return {number} - a positive integer 18 | */ 19 | var reverseBits = function (n) { 20 | // reverse binary str 21 | var str = ''; 22 | var i = 32; 23 | while (i--) { 24 | str += n % 2; 25 | n = Math.floor(n / 2); 26 | } 27 | 28 | return parseInt(str, 2); 29 | }; 30 | 31 | console.log(reverseBits(43261596) == 964176192); -------------------------------------------------------------------------------- /191-Number-of-1-Bits.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * https://leetcode.com/problems/number-of-1-bits/description 4 | * Difficulty:Easy 5 | * 6 | * Write a function that takes an unsigned integer and returns the number of ’1' bits it has 7 | * (also known as the Hamming weight). 8 | * 9 | * For example, the 32-bit integer ’11' has binary representation 10 | * 00000000000000000000000000001011, so the function should return 3. 11 | */ 12 | 13 | /** 14 | * @param {number} n - a positive integer 15 | * @return {number} 16 | */ 17 | var hammingWeight = function (n) { 18 | var weight = 0; 19 | 20 | while (n > 0) { 21 | weight += n % 2; 22 | n = Math.floor(n / 2); 23 | } 24 | return weight; 25 | 26 | }; 27 | 28 | console.log(hammingWeight(11) == 3); 29 | console.log(hammingWeight(0) == 0); 30 | console.log(hammingWeight(1) == 1); -------------------------------------------------------------------------------- /198-House-Robber.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/house-robber/description/ 3 | * Difficulty:Easy 4 | * 5 | * You are a professional robber planning to rob houses along a street. 6 | * Each house has a certain amount of money stashed, 7 | * the only constraint stopping you from robbing each of them is that adjacent houses have security system connected 8 | * and it will automatically contact the police if two adjacent houses were broken into on the same night. 9 | 10 | Given a list of non-negative integers representing the amount of money of each house, 11 | determine the maximum amount of money you can rob tonight without alerting the police. 12 | */ 13 | 14 | /** 15 | * @param {number[]} nums 16 | * @return {number} 17 | */ 18 | var rob = function (nums) { 19 | if (!nums.length) return 0; 20 | var dp = [0, nums[0]]; 21 | var max = nums[0]; 22 | for (var i = 1; i < nums.length; i++) { 23 | dp[i + 1] = Math.max(dp[i], dp[i - 1] + nums[i]); 24 | max = Math.max(dp[i + 1], max); 25 | } 26 | return max; 27 | }; 28 | 29 | console.log(rob([5, 2, 3, 5, 8])); -------------------------------------------------------------------------------- /202-Happy-Number.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/happy-number/description/ 3 | * Difficulty:Easy 4 | * 5 | * Write an algorithm to determine if a number is "happy". 6 | * A happy number is a number defined by the following process: Starting with any positive integer, 7 | * replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), 8 | * or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers. 9 | * 10 | * Example: 19 is a happy number 11 | * 12 | * 1^2 + 9^2 = 82 13 | * 8^2 + 2^2 = 68 14 | * 6^2 + 8^2 = 100 15 | * 1^2 + 0^2 + 0^2 = 1 16 | * 17 | */ 18 | 19 | /** 20 | * @param {number} n 21 | * @return {boolean} 22 | */ 23 | var isHappy = function (n) { 24 | 25 | var results = [n]; 26 | 27 | while (true) { 28 | n = squareSumOfDigits(n); 29 | // console.log(n); 30 | if (n === 1) return true; 31 | if (results.indexOf(n) > -1) return false; 32 | results.push(n); 33 | } 34 | }; 35 | 36 | function squareSumOfDigits(n) { 37 | var sum = 0, tmp; 38 | while (n) { 39 | tmp = n % 10; 40 | sum += tmp * tmp; 41 | n = Math.floor(n / 10); 42 | } 43 | return sum; 44 | } 45 | 46 | console.log(isHappy(1)); 47 | console.log(isHappy(19)); 48 | -------------------------------------------------------------------------------- /204-Count-Primes.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/count-primes/description/ 3 | * Difficulty:Easy 4 | * 5 | * Description: 6 | * Count the number of prime numbers less than a non-negative number, n. 7 | */ 8 | 9 | 10 | /** 11 | * @param {number} n 12 | * @return {number} 13 | */ 14 | var countPrimes = function (n) { 15 | if (n < 3) return 0; 16 | var cnt = 0; 17 | var p = new Array(n + 1).fill(1); 18 | p[1] = 0; 19 | for (var i = 2; i < n; i++) { 20 | if (p[i]) { 21 | cnt++; 22 | for (var j = i * 2; j <= n; j += i) { 23 | p[j] = 0; 24 | } 25 | } 26 | } 27 | // console.log(p); 28 | return cnt; 29 | }; 30 | 31 | console.log(countPrimes(3)); 32 | console.log(countPrimes(4)); 33 | console.log(countPrimes(21)); 34 | console.log(countPrimes(27)); 35 | console.log(countPrimes(31)); -------------------------------------------------------------------------------- /217-Contains-Duplicate.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/contains-duplicate/description/ 3 | * Difficulty:Easy 4 | * 5 | * Given an array of integers, find if the array contains any duplicates. 6 | * Your function should return true if any value appears at least twice in the array, 7 | * and it should return false if every element is distinct. 8 | */ 9 | 10 | /** 11 | * @param {number[]} nums 12 | * @return {boolean} 13 | */ 14 | var containsDuplicate = function (nums) { 15 | var map = {}; 16 | for (var i = 0; i < nums.length; i++) { 17 | var n = nums[i]; 18 | if (map[n]) return true; 19 | map[n] = 1; 20 | } 21 | return false; 22 | }; 23 | console.log(containsDuplicate([3, 4])); -------------------------------------------------------------------------------- /226-Invert-Binary-Tree.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/invert-binary-tree/description/ 3 | * Difficulty:Easy 4 | * 5 | * Invert a binary tree. 6 | * 4 7 | * / \ 8 | * 2 7 9 | * / \ / \ 10 | * 1 3 6 9 11 | * 12 | * to 13 | * 14 | * 4 15 | * / \ 16 | * 7 2 17 | * / \ / \ 18 | * 9 6 3 1 19 | */ 20 | 21 | /** 22 | * Definition for a binary tree node. 23 | * function TreeNode(val) { 24 | * this.val = val; 25 | * this.left = this.right = null; 26 | * } 27 | */ 28 | /** 29 | * @param {TreeNode} root 30 | * @return {TreeNode} 31 | */ 32 | var invertTree = function (root) { 33 | if (!root) return root; 34 | if (!root.left && !root.right) return root; 35 | var left = invertTree(root.right); 36 | var right = invertTree(root.left); 37 | root.left = left; 38 | root.right = right; 39 | return root; 40 | }; 41 | 42 | console.log(invertTree({ 43 | val: 4, 44 | left: { 45 | val: 2, 46 | left: { 47 | val: 1, 48 | left: null, 49 | right: null, 50 | }, 51 | right: null 52 | }, 53 | right: null 54 | })); -------------------------------------------------------------------------------- /258-Add-Digits.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/add-digits/description/ 3 | * Difficulty:Easy 4 | * 5 | * Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. 6 | * For example: 7 | * Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it. 8 | * Follow up: 9 | * Could you do it without any loop/recursion in O(1) runtime? 10 | * 11 | */ 12 | 13 | /** 14 | * @param {number} num 15 | * @return {number} 16 | */ 17 | var addDigits = function (num) { 18 | if (num < 10) return num; 19 | return addDigits(sum(num)); 20 | }; 21 | 22 | function sum(num) { 23 | return ('' + num).split('').reduce((a, b) => a + parseInt(b), 0); 24 | } 25 | 26 | console.log(addDigits(38)); -------------------------------------------------------------------------------- /263-Ugly-Number.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * https://leetcode.com/problems/ugly-number/#/description 4 | * Difficulty:Easy 5 | * 6 | * Write a program to check whether a given number is an ugly number. 7 | * 8 | * Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. 9 | * 10 | * For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7. 11 | * 12 | * Note that 1 is typically treated as an ugly number. 13 | */ 14 | 15 | /** 16 | * @param {number} num 17 | * @return {boolean} 18 | */ 19 | var isUgly = function (num) { 20 | if (num <= 0) return false; 21 | if (num == 1) return true; 22 | 23 | while (num > 1) { 24 | var old = num; 25 | if (!(num % 2)) num = num / 2; 26 | if (!(num % 3)) num = num / 3; 27 | if (!(num % 5)) num = num / 5; 28 | if (old === num) return false; 29 | } 30 | return true; 31 | 32 | }; 33 | 34 | console.log(isUgly(6) === true); 35 | console.log(isUgly(8) === true); 36 | console.log(isUgly(14) === false); 37 | -------------------------------------------------------------------------------- /268-Missing-Number.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/missing-number/description/ 3 | * Difficulty:Easy 4 | * 5 | * Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, 6 | * find the one that is missing from the array. 7 | * For example, 8 | * Given nums = [0, 1, 3] return 2. 9 | * 10 | * Note: 11 | * Your algorithm should run in linear runtime complexity. 12 | * Could you implement it using only constant extra space complexity? 13 | * 14 | * 15 | */ 16 | 17 | /** 18 | * @param {number[]} nums 19 | * @return {number} 20 | */ 21 | var missingNumber = function (nums) { 22 | var n = nums.length; 23 | var ideal = n * (n + 1) / 2; 24 | var sum = nums.reduce((a, b)=> a = a + b, 0); 25 | return ideal - sum; 26 | }; 27 | 28 | console.log(missingNumber([0, 1, 3]) == 2); 29 | console.log(missingNumber([0, 1, 2]) == 3); 30 | console.log(missingNumber([1, 2, 3, 4]) == 0); 31 | 32 | 33 | -------------------------------------------------------------------------------- /283-Move-Zeroes.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/move-zeroes/description/ 3 | * Difficulty:Easy 4 | * 5 | * Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. 6 | * For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0]. 7 | * Note: 8 | * You must do this in-place without making a copy of the array. 9 | * Minimize the total number of operations. 10 | * 11 | */ 12 | 13 | /** 14 | * @param {number[]} nums 15 | * @return {void} Do not return anything, modify nums in-place instead. 16 | */ 17 | var moveZeroes = function (nums) { 18 | // console.log(nums); 19 | var i = 0; 20 | var j = 1; 21 | var n = nums.length; 22 | while (i < n && j < n) { 23 | if (nums[i] !== 0) { 24 | i++; 25 | if (i >= j) { 26 | j = i + 1; 27 | } 28 | } 29 | else if (nums[j] === 0) { 30 | j++ 31 | } else { 32 | swap(nums, i, j) 33 | } 34 | } 35 | 36 | // console.log(nums); 37 | // return nums; 38 | }; 39 | 40 | function swap(nums, i, j) { 41 | var t = nums[i]; 42 | nums[i] = nums[j]; 43 | nums[j] = t; 44 | } 45 | 46 | console.log(moveZeroes([0, 1, 0, 3, 12])); -------------------------------------------------------------------------------- /300-Longest-Increasing-Subsequence.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/longest-increasing-subsequence/description/ 3 | * Difficulty:Medium 4 | * Given an unsorted array of integers, find the length of longest increasing subsequence. 5 | * For example, 6 | * Given [10, 9, 2, 5, 3, 7, 101, 18], 7 | * The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more than one LIS combination, it is only necessary for you to return the length. 8 | * Your algorithm should run in O(n2) complexity. 9 | * Follow up: Could you improve it to O(n log n) time complexity? 10 | * 11 | */ 12 | 13 | /** 14 | * @param {number[]} nums 15 | * @return {number} 16 | */ 17 | var lengthOfLIS = function (nums) { 18 | 19 | var dp = []; 20 | for (var i = 0; i < nums.length; i++) { 21 | dp[i] = 1; 22 | 23 | var max = 0; 24 | for (var j = 0; j < i; j++) { 25 | if (nums[j] < nums[i]) { 26 | if (dp[j] > max) { 27 | max = dp[j] 28 | } 29 | } 30 | } 31 | dp[i] = max + 1; 32 | } 33 | 34 | return Math.max(...dp); 35 | }; 36 | 37 | console.log(lengthOfLIS([23, 2, 4, 5, 6])); -------------------------------------------------------------------------------- /347-Top-K-Frequent-Elements.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/top-k-frequent-elements/description/ 3 | * Difficulty:Medium 4 | * 5 | * Given a non-empty array of integers, return the k most frequent elements. 6 | * 7 | * For example, 8 | * Given [1,1,1,2,2,3] and k = 2, return [1,2]. 9 | * 10 | * Note: 11 | * You may assume k is always valid, 1 ≤ k ≤ number of unique elements. 12 | * Your algorithm's time complexity must be better than O(n log n), where n is the array's size. 13 | * 14 | */ 15 | 16 | /** 17 | * @param {number[]} nums 18 | * @param {number} k 19 | * @return {number[]} 20 | */ 21 | var topKFrequent = function (nums, k) { 22 | var map = {}; 23 | for (var i = 0; i < nums.length; i++) { 24 | map[nums[i]] = map[nums[i]] ? map[nums[i]] + 1 : 1; 25 | } 26 | 27 | var keys = Object.keys(map); 28 | keys.sort((a, b) => { 29 | if (map[a] > map[b]) return -1; 30 | if (map[a] < map[b]) return 1; 31 | return 0; 32 | }); 33 | // console.log(map, keys); 34 | return keys.slice(0, k).map(a => parseInt(a)); 35 | }; 36 | 37 | console.log(topKFrequent([1, 1, 1, 2, 2, 3, 4], 2)); -------------------------------------------------------------------------------- /349-Intersection-of-Two-Arrays.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/intersection-of-two-arrays/description/ 3 | * Difficulty:Easy 4 | * 5 | * Given two arrays, write a function to compute their intersection. 6 | * Example: 7 | * Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. 8 | * Note: 9 | * Each element in the result must be unique. 10 | * The result can be in any order. 11 | */ 12 | 13 | /** 14 | * @param {number[]} nums1 15 | * @param {number[]} nums2 16 | * @return {number[]} 17 | */ 18 | var intersection = function (nums1, nums2) { 19 | 20 | var ret = []; 21 | var map1 = nums1.reduce((map, n) => { 22 | if (!map[n]) map[n] = 1; 23 | return map; 24 | }, {}); 25 | 26 | var map2 = nums2.reduce((map, n) => { 27 | if (map1[n] && !map[n]) { 28 | ret.push(n); 29 | map[n] = 1; 30 | } 31 | return map; 32 | }, {}); 33 | 34 | return ret; 35 | }; 36 | console.log(intersection([1, 2, 2, 1], [2, 2])); -------------------------------------------------------------------------------- /383-Ransom-Note.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | Each letter in the magazine string can only be used once in your ransom note. 4 | 5 | Note: 6 | You may assume that both strings contain only lowercase letters. 7 | 8 | canConstruct("a", "b") -> false 9 | canConstruct("aa", "ab") -> false 10 | canConstruct("aa", "aab") -> true 11 | 12 | */ 13 | 14 | /** 15 | * @param {string} ransomNote 16 | * @param {string} magazine 17 | * @return {boolean} 18 | */ 19 | var canConstruct = function (ransomNote, magazine) { 20 | var canUseMap = magazine.split('').reduce((map, ch) => { 21 | if (!map[ch]) { 22 | map[ch] = 1; 23 | } else { 24 | map[ch] += 1; 25 | } 26 | return map; 27 | 28 | }, {}); 29 | 30 | var wantUseMap = ransomNote.split('').reduce((map, ch) => { 31 | if (!map[ch]) { 32 | map[ch] = 1; 33 | } else { 34 | map[ch] += 1; 35 | } 36 | return map; 37 | 38 | }, {}); 39 | 40 | for (var k in wantUseMap) { 41 | if (wantUseMap[k] > (canUseMap[k] || 0)) return false; 42 | } 43 | return true; 44 | }; 45 | 46 | console.log(canConstruct("cibdccdjhgihcdchibjfhhcafebfcdbjhfiebceeihfifbbecajfidfcfceghaddcjgcaajahbdcddebgafieehchgcigdggibjcajabbaghffgijjejcafajafagegfgihahjgajhhdfehgfgagedjbdcgdciiijciiceahbgbdfcifdadhgieiaadcjgifaaefcacgiaifiahidhchficddgaaaigdbbafabeciedcihbfefidfihcehfeihacbbjiffadbiiacjdidbfahehiicchecgjbbfdeaieafbdbhfigbihjiehjhjicbeaicbeeeefcgbihjajahjhfbddecijhhafdgbfbhicfdijecgchbcdccgajjbadfddjdbdaebehejdhjabecjgffafjbdcidhafchheefhjfciafiaffgbifgddeajfdeacgjfaaeahgaacefafcdadhaabajbdieahajfficfedhieaahdbhhbfgcicbbifbieeiheihgbebggfidfiggeaejgfgbbdjgchihibjghfceabcdjafabhceghegadgfcgeieigdjacbcecfgfhebidfajeifbgejfhhbajhidadgjfbcfgfdehcfgdbhhafgfbghfebfeccddbgebaeeghaccjiiiicgiibjfeacdbdfbahfefabgbaibbfgjcaccjjcggdidhajffacgdghcia", 47 | "icfedgijgahgbgiejjhidhdbiffbidaijcbceaihjagibhihjejbfehieeeafegdgabffacejifaddjgjejjebihddcjfgafffbffcgjggiaaicjabgjbbcdgiigfbbedbhdidbdfbfgecddcggheidhdhiiiijcjeibhdjhhiidcafibggbdjcfjbcdbafgifcefhdieibahhiiahcehbbfieihffdjffaefahjedefegahdcffaaigcddbaajafihfaaidijficdbghefdjjjaihhbhbfejhfgiiejcdahgiddjjgdjgebdbhigjdadfadibeafaaadaaaaaacdbbcbfjcbcjbgaadidigcabggigcfefbebciaeejffhiihfijgjdiejgibhehjghhacijbiaffggbfcefedagjeafhgehcajjefeghhhgeefagfabadfdheigdhchgdfcbhacdjafhddgeigccjgifdjcggjebcjhcgggbeiajbhfigcfehafabheedjjhddjieehbfebbagjhffjjjjbehajjcihbaghghicbchgbefhefagcfiacbbeebfjejjiadgaeaccbbediegbfcdifgcagiggfiafgcachhiigbjibdbjabicjihjdajhhahffcighiafhjacbghbccibcafabhbfcbcbicjfgfhcedfbjjigiajbdcjcifadeigghigcghejcaicefejdheefbfccfgbicdabcciegfccjhgeehjgfffdbfdedgefdjgbefgdheaacejeebbfecifjdcfjbjddhdjacfcedhjfdjffggaaigfbdefhhhjgiicefehehaahddddgcejfhfeggiiifacgeheejehbajfjjdhfiiiffbebfcgccgafdehgdbcaefhagaejjhhcghhcdgcifafhbcdhecafhjeebcggadffabfffecaiifahdebhjgjadgbfaehigfdcjideabgghfedcceicbbefdgbdcjgcdedbbehbiheadhcgddhjhahgjbbbaedfbifhhchcdhdbhbhddfaihdahcccgeejbhffjegjbgfghdgbeiiegfgefgecdiefebcbjhecaaechbabfigheffefaiefgcjaaafaiiagjbedihidfjjfhhjjgeaiichbijahfbadichdijcfaejdbcgbbhjbfciajbbgegfdheedifiahbhadgiieagjdhhcegieffigbajgbieicgghbibjdaedagegbfaaggabeebefbhhdahhficgadedifjecabcefgfgabdegdfgaajdbfejiejdcbajdfbhedabbggcfbadibdabiacbbfjhcbjfdjeaeiedjjbbiagadjeecgiiifcffiajchcccbbabfecegibajhjccbiadhfieihfchebebjhhgecddhjfjajjejcehajbcdhgjecbfdfhjiaijchfefgfbjfjgfadebagdbggbaeicehjcghhgfbibfcjbbfigfebgjcafgfhdjfceiaddbeeejecgjhigfahigjcjjfdcjhiddfjcgfeijicjijfgbcfehgiddbibgeadbfabjjegciechaeddbfaaibahggchfhaejjedcjachbgaccijacgbddbgjibabbiidjbdeehgeidfeabbcdejgficcjdaecgcggiihaciedihdihaiiejagejaggddiaccihbcfdegcggghgdaacfigjbjdcfcaeihaaiffbjhjfcabcgbdchfejjachhfighjbbjhifdiigabffjdaedijeifbgdfceagehcacahgdiecabddifdajchjgebjaihbdeibbjffieefagchcdeabbdfdgdjbagjihebaihehjdheheiicjhaehhfhcdfhibbbdjiggdafgggcifagehgfcefbbiihdjagiagjicjdjddecggjfjefdigdgjgcdaheabijdgijejaeciehfigffighbcigabacchbagcfffcdejhaichbbaeahjghdjbjdhcbebabbhbbcbafdcdcdifihiijjjfahgbhfcjhbiafehbgaafbciebbahfaihcfffabcfjjagbajdgggihhaigdceaaacefihicjbjdchehdjhbeceiiejhehcfhejbegceebhjiejhccagdgbibgjaegiaaaffdbiejegidaegibhdabjdhdcahaehgaihjbbacbcahijgjgchfegfdbdccfhcghdbdgiaeijagbjfgidaeefcdbjdgeaddcdhdbfibagjhgjjcbbdhehgjdebgejaegfggcfejfafieciaigcedbbgedgjajjcejiecffdijggffehjeacedbahbfgdjgcajgdjfcebghbeihcfihaahhfeibagjcjhdjgfcjgjdbbadgefffcdgbggbcgchdhddjdaabddjcfghdgejcggbijfbhihefechiehiahijbbhjdbfdcdaihaaaiccgefbhdiefeafdebihbhciedeeagbgbaibcgbjfidbfhhifccadcachihcdcbgihihjffdafcjjdbjgibbbgebdjgccefffcbbjhgedbgcachbffeaaijcfiahdeaecfaheaefegdccaccgicadfedhhdajehfjieccchhhejdaafgigeiafegdjajggifhdcdachgghajiijghjaagcihdhicaeibijjjgfhcfahegcbjadegbijdddgebcfdfcjacdbgedciefgdeggdhhcejdbcjdfgjihggchdceahchhfhcggiihfghabiadiaiiaiejdhbbjcceidffegcbdedgegaeeefhhjhcbadeichcacfbajdbfcaghffbdibfgeaeeacicdjfeiijiedaffjbchchdfehjfcghfeadgbddgiagjddfbajedfeihccbdjcgjdjjjcddeffffbdhabfcjcifdfcfefjfcfcjbhceaidihfffhfeedegjhjcfgfijchceidajgidddajhfcdhjdcfjihbbjgdeefjahhdadehfbegcedcjdhgfagcfjeidbcadagfifabgebffabbfaeidhedigdgfgibfedgebaddajbggefhbabbjihggeefhcbfdfggjihagdcjcijchaahfbeegjjhbjjbdchibhgghccejajeacgegbgaeddhecjejieehbcfhdedaifigdceadjjihgbegijgicdcebdfjhidecifjeabfhahebbcfhaejdhdcbefdchebfbcacbeegfbhdeegeddbacicfgfbegdajacfibfecgidicjhiehigcfiijicdgafbjjfcccafcfbhejbghhbbfcccdgbffcgjbhjgeadbegcbjacgjfaiagafeddjfachecejhffggbbbdbddefeighhddbdcigdiiiggihjgffijeijeeggjjdchgbegcgcbacjgjbfjdecbafihghfaicgbaihibdiecjbfadaechcijhbeiggfdigheighfjccifdahgecaijbediijdbjeieaajajbcdacijhcgedhdihhegaiffjfbaiicjedhgiadbddhhahjjibedhfeafeahabhbfaechjabhiiiigiddedgdhadedfffhcfdafhcjddbhbahhjaiffahgbeidgbfhigjidcjjdgcafbagijeaababiecaggdhhcaabfadgigegihghgeddcciccdjhdcajdahbigedeihafbgbagdgdcjhjabjddadafhjaeaifhjegjgcgdefjicjehafagjfdcdhibffjdgigfeebjdcabdgdicjhjdbbebhaahbafiiiebeejjahdcdbddfifhajfegefjgcgfefdeefcefhbjifafcejjhiaijbabafijbaidcbgddhabjijfhbggadcdahageccfadhjjgahgiggfhbheeabbfjjdjcbifhhbedafadbefiedbjghibedjcgfcgicbhbahdcdcdeibhjdfecajjaihejedhjjiijabchdjdihcibbeciidcgeijjcdiaaadhbcbfhagggbjajfjihjajbaagifdhhghhdacigccebbaahcafjfjcigehiehhchbffhdaebagcdcjbgehfchbfchaaijhghidjbbcajcaeigahacjjhefcehfbaddficjhbbceejgfdebjfadgbbijediigecjfdaafdhcfecdbdfdccaejjeifajbgaceccejaacbgdehhebhiccbibjcfbigfafbecfjbfddhjefjhjfeidcdbdgbggfejbaijdadicgciieahdfeabhbdcjagjefdehjeegjagjcfbgidiehcidagijgeaecjadiiijeegcbhdbhfhjhaaahjhcbagcadgjahbdjeifgeegagahbgbcbeihbjhcbiadejgdfdhjdbgbgdeijfiehgbighbcgbjjeffbcchddfdabedagabcigjjdjbdicaghhifbfgbjchacbfidaiehdffhbifegjgbdbfgigdgjeehjifcdddbbgihiicfggjijiajehjbhhfjfcaagijaihbhbibgegjjcfdfgbdfeabcajjhjhcddiciegaibcffeaajbcfjdhgdfcgdggbcjgfdciffaaeicfjdfhbbeachahjbfhhfdgdfeibcjhechgefceacidbcjbibhfgjjagfhgbbjfdcgccggajhiaigfbhhieghcdedfijaaabahdbidgjfabdgdfebidjhajigeihgdedfgihafegdfceebhaacbjbicbebgjgjjeibcjdgaceffbjihicdbiijcdfjhjgghgaigdiihcbhdjafdacdaighaaccdcghefidhiabajbeieiaiaajigghaacfgbfeahbadbgjjedcgacjchdhgbifbhabiedegfcfdhdchfdcdbfhahcgdiggiehihddeajbafgjcaeedhfiegcebfdgbhghhffecgjjacedeedbjhfcijheihbiegfcbcghfcecagjcfdijhgifjhejchejijhbebcjeiccjgjhfdijagbiachgibaicecehgabfgicjhijjccficagfddfdjebchbcbgigbgajefgjcabefadgdggbdeabfabicdaefhgjfjaiciaacgfdfchhaigeceedccfihjdgjffbcejicaiceeacbeicfdeaibcceejbiabjghcieegigcdjjabgabfhhdhabididecidaffhiicbdfbidgjgeefaaadabdijijdabjibiegbccdjdhbgajijbjehhjaadcjdijaijjdaidjjacbdhehcgeffiafjhjjfebedfjjiicichbjgjigjgcidcgcdajahehgfjiiegjafhbbbgjcijgcfbhecfiajiehcihbbdhhhgacefeihbefgicedeiafjihdggdgdaeijaaaiiicggabjcefeeaeddahdhghaecgagjgfgdfabaffahcbadfghjagjfjeghgdjfgeahiefagdhaaebfhabbbafeabebjcbhbdehhdgfcgaeaddddhagffgafdcfibahciefabfgcjicfaicdheeaiabjcfjaehadjcibbeghiifgihbfefagbdcgahgbhecbdghbhhjegdacjcbijhaighafddifabeagifbddihbjidcaabcjfjbeaaijjfbjfhfhbhjjhhggifdjfcgbbcefdgbbeadcifcegibfejdiibhdciejigjgfgeeigciegibabdhifdabcgdigcehfdbfgfgfacehjdiicehahgggiifjcidjjgcehcadfbaajgaiiajaehiceffjbhicfhcfeeajbbahchhafegabadihhfeccheihjfhigbhjhbdjjdiffffagdjfihdfjgecfbbebiceechidgajchgbbcjbjcffiicggccbjhgafdghbabajhggebhecbjecbbbidhhcbeagddefaifdgbdcdiibdhcdgjdgjjegjeabbgjaegjchhibfhecijhaijjhgciafhgbceaadjgibibhegighgfiiecgfhdfiegiigbgaadcfiehbcgehdchbeibbjfjedaeaieiafigajihhejgdbgdcfeideecebiefafgcfjcghihjgdeieccjiegegbfeaheijdjdbgfcjaecebaiaibbecbbheibhhdajabihdcicdehgddehcddccbffhdifhfegciccedgfbiaaahjhgbafedbgghcjcadaffagfghiagieacagiihhhjigjicdedgcfbggcjcbicechddeibjghgehicgddccggbhdecfhbjdedbchebbbjcijjcbbbebjedcabcijchaeichdahgfijjchbajabgagdcfeciejghgfgefiiigggiijfabjjaecbgiaihcjdegdiagdeebbbidfccgehefjbddagedfeeggebfdbidbibieabdaffbggiajbejdddbbgagcdicgeeechgdbdegjebdbhcabjgigcjcefdjjdhghcaaaiicabfjbcbebiccbbehhiadahbibecgfghfbjhfcdgjbfgjieahhehgedcefdeahbajgheegighdjjbbfiaffhehihhjbhfgihaieiaijhcdhijbigdhfggicjccgeifecifdfacjadahhaadaicfjbfhhfajijahiiejjeidfhefjijfdhjaigigadchiagdefhjefdbcjjdeafajdgaeecfgheibfbigadajijhcfdiffcefbjfajjgcdaiicdaajehadhhabcjeegicfiddjcdbcadhdefjcjhgbbdgccfibjijejeedcjcjcabighjeaicegaejfieebdfdbfdgfadgbhihfhjegibbibjbgfgefafejjfacigedcbididhdcebhehgdibcaaibfifaechcicecjfdhiaggddbibhdfijgdaicbgfigfeaebeechagfgbcbbdbbajgjiegbjfiaaacbjijgibcbjibfhiddaajabgffdebhdgidfbcjddhghidajeibciifhgjefdaiabbhbjfbadfgiijaabbhcjgicdaafghiijfbdjjbabeghggbjcjhabadjdhjbbeffeebjfccijbiggadeihifffdgieaidbabcjhfdhhcedadjejihbajcedebgcjeigbiaccbhgjagacagcjhafbegfabdcdicbbfbaiefahgefhjedhciccdbcgijfbeeedfihjbcceeedecfichggaachecdjjejfjaccfiiicecdidgfghgdjbeaijaabbdbhifeidcafjbijgdjbffefeciaacbebbiheihigchfbajgaajdgeiaghidfdjgigjbcfgbahgbeaceiaiafhffbcgggggjddhaabjejddhicbdifgjjfidjabjibhdidgifjbibhdcibgbcihijiccfjehhbhcbgfjichhdadbdgbbhahdefhhhibgabeiffjfdbbaeafcfegcjacccibggiigdeddagbaejehigchabcggfedhadjicibcfgaiahggbbcdcdiiiecjgjagegajjcbebcjafhffbjdajihiffcaibbjjdjgghbhjcehegbbecbjhgjbihefjifigccbedfibifdebhejeiafgehdaibchegfdiejichejgacgbhjabhacggididbhajbejaecdijadbjiahdjdbbbeaijgfcdhgeeighjgehfeajgfjcfedbjeiffibcjhfihgafjdbbjhjdagibhdhjaiebiedagcicjfacgijijeeebhfcgbfiigieijfaagihbhcjehhbdeijadegehbcahfhbfccbddgfdehdfhigdfgdfdbaaecgjchcdjjddfeichigbfcehjjhfadjadbgajjgicdfjjbfcgbehedgijifccdfaaibbcehddfbffhggfaiaegbiefbgjgcejggejiifjjhiffiiaaciidfiibbhicjgeadfchecijajijeebffdebfdjeebibfcbjfefiffcbbdfahgjghjijbdaddcagdafbbdcffbcfedadfafeaihdgjdeficbaebbhjjjfieefbfhgihaehhifjghficjeifecjajihcafccdfejfeihigfidfieefefffjjabgheijfhfhbheafaadhdjdgeajeegcdhfjecceechbedecjecbjdegcaedcjfajicbdddefahhchjeifijbbjbibhbdgbjbeaechghadfifbciaigffeeeijeggfdjhjehbiihhidcgedidaeciaibeeaebbiicehcgddjgafjidjiaejgaeegjjbbbibagddeiebfgdhifbhjhehgbhggeicaidceddjhgggeefccjjbegdcficgfcccgafdeehedcbjcgdbffjdfiadbdebefibfgjdhfjcagcacaejahjcdcgbiidbdjjaieijagaicfhjafcghjbfeabadaaehdejfgjbdcejidajgfidefcjicfajfbighfdgjbeehghfchfiaiejciffcciijhiijdbdfaffgefjbghiecgefjfhccadhgggiefegdccdjajechjgeifihfbbgecaffefhidjaaaeecbbgiabaecjjgbggidchcfacebheddccbjfaafbbjgchhefjfedhcigciecbajfcgdbhjfafbbheebgbgfiajjieffejaafgajadbcijbihcgciejijihjdhihjchgdjabfijeeieibaahbjeaaccjhcfchhajcdcidgghjaefjbaabffdhggcfhgiiiifddjgfdajhhejfcdhjcbgjgjjbdibihjahjeggcgbeddjicifigaggdacddabcadahaddjdjdgahahgdddiadigeegebjbggggfjfcjbafbhcddfjgcibebefcfdfigjihecajjfhagagcfbgjdbahceffdjdjjehdejhgjfieeaecjjeegdifibgahiiaghiibghidiffddgjhbdheaicbbehcaicdbfiiggcfjcbfcahbifgahjhgddgbhbegcidhadcjiffjecaedijhcdcdijeaghbebccdaaaeechaaaifjdaedhjgeadagfejjigdjdafddfgaeaeiibbghdccieggbgicidiegjaejgfjgjhaihhgfcggefhehaajfjbgfeieigiaffhdghbchccdaicecdcjfihheihhagjhbcihiigejeacaecaacbjcfjfjcfahgfbgddaacdieghfcfeaffbjgaffediihadeiidcaebeifhheagcdajhfhaacecigdghhbfhcibigfhhfhheiddijhbejeaiibhdddifdidfjiadccfiffcddcfeaigaagjgicjjjbicfbjadadgjccfjddjiafbchdchidaddifgafcjfjhggagefafgggebfdiciajfgegehbcibiafehafdhbbcjdadcafefcaagajbgjehbigfbfajacdjgbdjgaabbdiibeagchhdcddbcdbfecbcfdcfhejfdhjjiibcefhjejeafghbccejehehehcaifajdfhabegcbbbfgcfejdeeaggbejfdbejgbacbieiejgcgabhfihajifiebbacjdhgichbediefjefcbahaeefgabijefjbciafjgiabcefbjadheggdgfdjhfbbhddhfcijfjbgiahccgiejffjdehbajjeejgfcgihfejgebbigcgddgifhdifedghahahjdgeijhfgbeibgacabfjhhighdjafdhdcacjjifdciebjefhijhdjbhjjdjhdhecbbeagefeihgbceffheddaggcigfeadacdabfidegjhieahbjjiadcbbbghefeebecedeffjdeidhgighdfbfjibfiebigfcbdfbjjbahbcgiajbihehbhddjgjgdagbbedeadebdbdgffgfheedficbfibihejbfiaddiahaihiijdfbjdbhhabecgfbgceebfhcgfgdcdbgfgcgibajdehfiihigcffjbdbcefijaigdgcgfhijcadcehiiigdebagjgfchiadafeigbbjbbgiegiihaibddgffhihbhjjjicafebeiiifihagcceghibeedehbdajciaiahaghjfeidjdegcidgeeicfhhabafijffcgfaicegafgfdfcjgdjbejfadghfhbifgbbihabdhgbcbhiiiadccddhcehchadcdgfieghiibheagcdcieiifeihhjjefcfdfbggjggbhfdehhgeaecceccchjbeegjcgahjiaeagechdigiecihacgbfggbcbcfafbgaiicgfcdgggdcfgddhibcahhjgdagdjiacabjbhcagcdbaiicadghdfcfhegahghchjicdejaifdfheiheicfcahjacgaeabciibjdiiiafjahabaabafeaghjggfbcdgdbbaajjadjachcfijcaihifdiefdedehijdebfdahcggieghjicjfjaecfieibagahjhfdadfgfdfgbfjgcjhcjaiccgjdgfehhgbaeeigefjdafghfibeadchaadbcgaiehafdcaedejifghdfiefjebjaghcejehaabhgiddbchceghjedgfgcfjjcgjgjhicififbeccccbjdbedabjgbcjdacgifjbihbcfifijhjfidccegegfbjdccdeggddhgdcgeffachhbgeeddacagdgihgiebchhfabjihfjadaiiiigddecgabdafgafhcechffibcggabjbghjibdjgbbbibcbdbbgefdbahajdbahbghehbbffchahcabiaegdbcabbhfgedcehbjibhhdhdghgchddjebeaifjgcaecccacjhcifgdhibfcggajbfbcieachbjhdaiegdjiifgaidbebdfdegiaidgcefhaddaaafbieiafhjjajgfjjggjjhgcihhhbjchbjhigfbaiggigefeefeefigijfiaieegdhbjchacagabhagjhjbdbfghhdfdadefegicidfeeeechehiceabfbdchcjhicdeiiadiefaeeiieceiafajedaabeejabaidjedijehhechhajiefebiddagdcgcdiaediafdfigfdeiffacgjhgeecgabagaicfjfdbdbbdhcjhgijjagdegieiiabiigdfigdfgfidajhbcjdhdjdfdfjcdfeegaafiihchiechfbeaddcgffdegaafcagajddhcbcgaaihgbbjjeahjdafchjehidbhdjbcfhjcfdeaieiiabggjagadiafifjhbifhgigajeaagegaafjjfjajgjiechcidbbchgcdjfhjcjeegdccfedijhddigbhaejahdbhebjcfdaefhaehdjifedicgedhaggicfjahfijegcegihbhjbajccgffbgbbbjbjcgjchjechbbdacgjehifegebefjcibcajejedcghebebfjheifcadiegcbajigfifhdddafgebdidfcagdedecjicdeaaaedcahfbhfhchjbhgjbjfcbibccbidgeghgjhbbaijgaediaefcahdcgihhibeehdejhgdcdfjbgddfjacbeidbgggjgghbgghcfgigdgdfheadedhcejjabbfiadjggdghcdehjbhdfggeefiefgjbebgifdbdgiaddjbddfbibbfdffechgebebbdddbcfhdhhgjfcadbcgdgcahejihcgafdffbbacbhfdehehajfigdfajiegdghbagdedibjejghijgfcdfbejbcacjfbcaafchjbadaedecheibcgigfabdecdefcafjbegacadaegcjbjjdjddeefbajdfiaegcfffadjcjhjhfaidjagjjicdccebedafehfhidcehcadbfhbcjjjjicahhcaahdcbebegicahhaifihjbabhjaaebgcbchgcfejgffabbgiifhahbadahjafbjfhfbecfbbjjhidjdbhbijgecebdedgjfgggbadgadifbhefdjdhfajbgjehheihfebhcefibehhhbffbdgdcedggagdcigfjcgdehheaifedehffbgifiebcjcebahchhgcijcgchdgfjdcjjfeajacjdagiididjgjajighhegfcgiaeceacchiedhbbcigfhecfdcbbegbbaagfgeedjeejgfabhhcbjeheiifbbbjibddiehedcfgffaebdfhgaajbajfibgggibifgfaagcdjieadciicgfebbjaigcfhgcbhchhbdagigdjabiiehcdjidceeciefihcdjfchbacejbjdbhdjbejaehbiebjhdgfeiiafaiaiafaagbfhhbceiichhjhdggeeifggeaibfbejgdhcgjigbdicfejdhghbcegeegeacdfijjbdccbcjecheacfbgfdaficfgfgdggccbigcbgdeidjaafdeefaadbdbjebcifijdcjjggijbbgdabibdhgachbhiedbfbeecefcjcgcbdddaffcbfbgfdgehajidhdhgjbefjjcdifcjdgggibiicbgeecdjeiefcajchcbediehggffiachhhifehjcdjghagfdcgcabfcffaaecefdjfehdicacgjadeheijhdjgeffghgjihgjcejjidjidejgfdfihehachdbjdcijeaicfadaejjdibbjebehcegaeegbehceacbbhdgichdgdfiffiigdgiebihfibjdffeidfbcegdebhehjgiiejafegcejhcgheecjjhdicededejhadcdhjegegijgfeahgjgggeffjiegacjibjhefjeafiagjcbgjgcffefhbbaaeieeddggijehfjghigahcehiajcicehhhfabbjcgachbhhaedbicebjdbfdfadbeihbgecajefdcgaedjigcagcebiadafjadbfhbabfidcfcdcgabafcghgfcfagdhhjccibafgcgagfbeiddcbjdhiidechchecbehfbhjifbfibggafciaaecdbdgaibagibacdjehjdjheifcjgheejaagjiajbihfddfhidjceebeajfehdabegdeiieaeighiigfchaifbghieiddgfjbjjihbggcjbjbiieccgjeccefciejgcjcgafchfgghbjeeeffgebhhgghdeijdaajebcedjgdcecfdhgfigeiddhcgbjcjifaedjbbgihfbdghiefifadahgabgecfecadjafffcgabadagddjjfabecjecccehcbagcaijcajhcihaachbfjefbfdddigieieiedaegjfcejbjaciicjhciifagjhigigahajgdfehcjbaffdbidhcchcafdeihjgcdggeiciffibjabcdjfcgdfaefjhdbbcigeddgfaefibbacfdgecjagaddfeedjifceijfcefjdhddgbgacdaffjedhafjidafagdejheadhgefggebgddgjgihbbecgcdgejbifbcccdgcgeijidfdahdhaijadiciijdjaciahehbghabbehabiaijaaeeaedihhijjedbeiifcdffbgcgccjigiajgfaafhfibhfbhbbbcecfcgbfgceifecdjabaaadfbcfdifiagdejecaigicjbfajfchieacdicjhjddafjedajhiecdaihiafeecaehdidbdbfcbbhgafibeejhfbceheieabjhjafdgjihhhdeacbgeieggcbcfdehebhebdefjgeeefabhfcighiiagddbbfbaghcfgehijcaechaideggjahdagheieebciabadbhaffdaejcjghhjiahhddddibcfijgicabahffeedhabdagihgghaacfcjcedhjiaedbgffecjffdbfbhjeghdcbeibhjihedghegihciidbfedcfdajdgbcccfbbffedjbciadibcjibdhdbaafggcagdiibiiigcgcjjbdfaaejgijdghchgabcdcfhijbeiaffacigjbdhccjjdjfdihjfaijbehejhfbjjagadgcfceffgabaeeiebjfdidgjfajhegjhieabaaacajidebafffbdaabccidfbjdfieifcdaabbagddgdieagecgfbhjfjbijibigfjaffgafaebgafjghiaigehhfbfhdiaafifagcjfefcehccefghjbigdjdghdeeiaidbbaeicdbfigafibcchccfhgbdagihhgjaiibdffcbeabfjjbbdbdcbijfecehcgejddgiaeaifhgebbeajdaigacbabiegigdbjhgigeihcedicifbgecfjfdidajfcfgbegfgejbeiddcgjhgfbfgijeahehjjfidcjbecfedejiiadcicchbgcebdjghcbabfeggjcfigffigggbggeiibdejefhajehejaajcghhciadjjfihjabbhbjacbifcegiahjfbdjjhgbdhbfehhhdcicbbgaicjehdiibgiibedfgecegibdhcicbgeijafegdfgedbfeffcdaicfjdgbhfjdjfbgffbfbciicgbcigjjbdghgedijfdiiibjcjejdhcjchgchejedbabfibhhejjcjfjdfafdfbcfdidhhgjdbhgehbedfdehdejjeeebijibcecgabgjjcbjfaaihdiiibcghcidabaedgjdiiifhehgejcbcjeechdaihbbeicaefajajahjggdgcecaiadagghfjgibdehfadaejifjhidjijffijjcjbbjiagbdhbdcceibhjgiegdhifgejidebiihjaccggcidcjcejhefgghfgbgccfabhibijdfiaihbffjbcdfhhcjdfgdficbicehajiaciccfjiieiggbheijeicaabgciagfcbgaeffcbddajgdgcjahcgehccgjgacahififcdabcbhhdiiidafbccchbjfaddchgefahhcfechiiaciaeedafiddbegbebaaiaggeihbbageejjjfdgdeeaddeijdebijhebbddjjjbdabfgfbgjhagihjibhdegieacdgdbggbhbefjdfcgfjaccdfdfddcfcfcfjdbiigabhbcedcijaabhijiebgjdbehcbjeaajbhbfggbjccjgdbhggjfhdafhhfgaigbgfjjhgfggdddjebhghiciededgajebddjacibeehhhbabbafbcadhfjjahgjdgicicdiagfeaiffgdjffcjcadhbcieaffehjbhbiafgjiddadjjadadgicichjdceghejhcchbggiicjgeeebdidagagfghcchffdhafgeiccidgefdbehhggehhbiajfijfeafjhjcjcbbijjegcdegiigffgbgfcaiceacajjajcchjhibihjagbbdbahhjjfajfeehbedjceidaajdacbcdgjgjeajgebegddfacddhehcfdfheddddchccafadbjbcacieeiibdgebjjiidjeaibdiaiiihgifebgaabhbejbigfgehefeihiejdjehcaffhchicejaccffgihiiiagebdaegcgfhfjehidgbadjjchffaiafcjjgcefhaeafdhddeafaedccdhabeicjjbeaghciijddiiiegaibfdedjdaaaeccfcadgdcedbdiijdchbbcbedfdidaafiijidfgbfebfdadbbbchihachhhcdgjafaicjjhibfbibhcieddafgbcafjgaehbjbjdaaigeeehfhaahejegddjcjchicbjhhhbaeaaiafhgbbgagcfhhbdhfahffjieiididadjihgdjdjifiiedbbdcgbafcdgfdecffdbbfgejjddhiccagcfhchhdjhdiggjbceheejaejjfdjidediidgibcjidfiicbbbfhcdjccijcfifjjecfcddbjhiabijjjghcabjibeiadbcgbiageafgfhdicdgcfbffdcaibeajffibjhibbahdcejieedeichjccaibjcagffgaiehceaahgggbhcgfijdccfjijehacdiefcjdghjbehieffdbdhhijhdhcdagiiaahhbhdegjggadagcbebddjhbcfjdhcdiegfbigifaebbbdcdaefdgbfjibbdiiiidfbeahhjhegbebjbagiiejgfibibefeecdcagehjhddcbaffeciidahadeefjgaeeiabhegjfbajbbchcggjihacgiibijifcfcibachddcfhjadgihagabhehabchiachdjbfbgaebcagfefaeeaidagehcchjbbeajbihidfcaijbigeijdedfeahddjccchcafhcfhgaiabfghgdbjgbgdbigiahacjhicgbafbhgecfahaecfaecjicbiadfcacecaghcabedjaigcdabdiffafgigcfjfhejgiijbecbcdbfbjjcijebdjedbajabicjieiiaiddegdeiijccjdgcahhhjfiabdbbgjaaaeacdcgidcieiafhbdfhhdcagdhiegecebccbeehfchaaaafefieiccebgajbfhhiecjfghdihgehdidfehidcfhahabfachahigjfibbbihgebjeidhicaabigfjgicehfjabfiiejifdaihbhjagihfgfjdbibaejiaadiajidbihfadgggaaicjehghdjegjafccgbdhbjghghgjcgjhbddaacdihiiaibehdafjbfhjgaibfcbeihdjhbdfaefdjchijcbedfbaajjadebbhgdefhhhcfjjgiacjgbbggffidbjaajbeicjijahdhegdjagidafjddadaadafjcbcihjbjcghhfghceafagcejadcafdecheadajccbeccaifdfghjjiciefheibjccejafehjffdfaghcjbccagdhfadjafjbjijfhbgfjabajadehfifcgfejgibgggeifbabgaefcaeehfhjiebhaadaeggajefajdciiiiahdciihfcfigcdeaaiibhajihadecgfdedhaffjjbfgieefdjjachbiidjbfiiaejbaiehdbddhgeedjiegfbdhhehjjfadfiibchdcdfghcdfjfbjeaefjejfacedfaebchffgijibjbhdbbjecbbcdihgaabaffhdfihhagfjhdicfidgididifcijegeadaiadihccciigijccbeagheigjhbhcjghigdhjeigaafffceebagjgdhaijiaghcfibbfdecahjaijbjbjbchcfdfiaicfgdebceghhhbchhhgfijgbjdebdgfebbhijhjacejjbagaiecfiicbgdadebddfgibefdhcibggdhhebffedfadbhceddiaeggjhafieijhhaeedjcaccachicdffjcgbdaaeaifjgcgjcigciaaidjdbjfafacdhdfdiicjcghhddebfdgcdgjacbcjjhjecdfcfidhcgfcdcdehjibjecgcejdigfeacicjhiieifefbdficiijfiaacbdffgfjdbhbjfagadgeagjbaigdaaifdcjiefijcicchaidhhjdgbfffiibhjabdigchebcafjedjigbfaihebecgafajaeecdccdgcgdchdbjdjfdbejfdjbbedaiehadiheiiiehcfcdghbbgddagdgcdcifaiifdhggbfjjajebggjehffgajeggffiifdabaeigjieefaiiejggiaegfdchibeccbfjeaehcdbiedfdhjgbaffgjadgggjhjiiadgfgidhbcjbccbfahdgfajeahagjddeefgjegggeaaifbiihbfiejgbdhbcbhjjchdbeiagjbdbefjigheededbjgbggbbjjcidjjbcdfcagggffejdeehijifhicifhcafdgidgcdagdhjecgjibdbefihacfccaagebghjeajgghdiejhchccidafjdjcbibficjigbaahjcbeccabibihhbdehjbficcdjdfaciiagagjcajaifgeccgjcaehcgjjcacejchfdhjfiahfhgejejegcijciihghfageiedbibicgfidghiiicghjdiagigibgbbgfdbcfhbgaehfhjcfhiihajfehbegfiihhfdjhjhcdabaddejaifheifhjehggejhcfcfeidhbcabdhejcbjcbajhdeffdfeifaiihfebahjecdidjgbebjciagaihffjhebjjbeihiecbdhhgdaffeefecehgdebbhbfefbjhdfiabhdadhfiehacidciaccehegcffhbfbgdidagjehhddjcbbeeajcadhcbdiigdfehggfibgdgiejdiiajagichcdhgjcbhjdcdeicgahcahbbfbigjgeecijhjdfjeadjgddghiagfidjjeeibiihjibihejahcgjfbgcefffcgfgddabbbcbecieadibcfajjgabjbiefdhgdiajijfhebgihecdfhicbbhbhcbjbiggcjgdajicecbeejjbhiddbifdfahjffeefgibeedgciicfabebefjcejbefgajddhehecdeefbajgejifghiddbjhfegdiachafefbidecggacbjjeecfhiibghjegbbdagafggjbhfcbdcbcddceifjjddcieadcafbjfiihddbbfjifhjcgaefgbbacdgcbdigdheaheaejdfiiahedagidbhdjbdgjjcjibfcchchcgeaegfbchdcdbhjebbahjdcijjfdfhceeiidfbiicadgebghicgeacegbffdgfibebecdibhbhefjdjjcghcghbcdeejbedhbhdaabchcdgfcagdebhaifbjjgjdihjahjfhfcihjeafdafifhifiejcbhgieefffgiehifdaegebafjfhfcfhjdbhhcgeaeagbhjdhahhbgdcdjdfcgeedhfiidgcjciigdbhadehdgejhjaddchbecjfgcdeabfiieieddhbecddhaebfbjaibhhigichbcgbdcejfidfdcjddbfchhbiejgajfhabgefbddiaijidhfcgchihhaaifjihdibgaejffjejecffjahiahcgibgaccahhdfgccfahfjcdcigfchcgjhjjbdjfjdhbhcfafcejbgghaijaejjcgbgcghihafgcgggebgajdicidcdigbcgeadfagggjebgadfdgfiadabhdaediheaceahjjfaghdejchjeegfdbgjhihfcbaebffhjbgacfdjfbjcefdgjcijjgaiihcgifbcedddbgajccebhaicbecbedbaegbcgcgjagihiajbecfecbcecacdeihcahhgjcfgedihigbhjchcidgcbhjhfbhhdhbigffhihgadeaeeejidbeeaddfdebcbgbjibdhfbjebghjjgdiahebaajfifjfcidijfefiieceafbijibjhgabdbfghejgifggcjdjcfbehbejhcgeefidedhdicghjehdifeehdbgaecaadcjgfegffjicehheebihaddjjbgbdifaajcdgacdcehagjabfjdeheihgbeidbeigbjgigigefgegigahebfgigcjggjegejjbcbejgejfdfhhcbffaaehdghfejjjhbjeaagchcicgiggeggghjdeghdeaibbeedhjcjhihfahghibdfdidjghjiaagdhbgbcieddbddggjaecghechejfahgigajdegdafjjgedjjbieheeafbhaedccabjhfjfgfjijhabeaadfacgedjegjhighhfchjbbjejiadbfajeeeiabddfbeacijifbigjajcjhcaagjjabggcgaiejiihadjigaiaigjdeiaefhhibejfhcgcajaceaecahehegecgehaaihjihbeaedabaeecbhegeiihefiihbdjaifdfcidejhibecibghjhaibgeeabbiedcdfijeaecefghccagifejadcjggajibjffcegjgejhfbdhfhecgdjfdabgbcjgabgibffeaajchcjgideaigjgcchdcdidibbehjjhacdifajiccgdfgfedcdccceiejiaadijccfhhahjdhgfhjhcgehhdbgfdjbdcjddecbegiabgebdhgadcfbadbibjdjifieidjgedhcebjjaajeadbgacjcdchjefdbfbfagdbagdgijfdejedebaffgfbddciddcbhbheiifahdacgfgchifbhbbbhgijgdhggfdggghhddjeadjaiiffehhijbhehjfifbjfhbcgiijhdagbbegfjfdeighfeiifcjcfdgaiijbejfaahbieicdchacjdgdeeijhggcdgfejeffgbgcdhdfjcaajdjdeaghdigdhaeefbjifggbajadjfgbjagcbiegacbbcajjjhbcaijgcccdeiaciegcgcbahdahiibjdjehbhcbfbahjibgacfehgjjadjccdjijaddcdgdjcbbhjbidehfgdidbidgjicadddhjigeedafcbfcbajefbheaagbhdjfjhbeiibiehbejfbcdjfiaeaadgegdcffifcffjgjeibbcfjjbjgdjjabfjgbffaefaficgeiihcjbcaacbefjagfadjgjfdjeghgagifdibcahddejffggfjaijhjifbgdjabhdecdiihjibcjbehbggbiibhadbfjhiadbgjfaeiejhggigjfejfgfadcchhdghedjgcaajbijhagbicdeagcifibdfgbdjffcadahejjicfgjfgfbfiechghhaejbjbhffecahigffeiffdgjdffhbccddbjheeehggijeagjcfajjhibhjefjfjabihgeeiihegjhdcjadjccffgaejchbeeghahbihjaahggjacaafbadchifiifcfeidbiggaggfdadafdigedbidbfcaaheeffdhiachiaddidchbibifaccjbjfhdhiabfgeagfdjgjbdcegdjihdgcaacjdeccecjcjfgiggafeadjdcgfieedaihcjdhgcageehecijfigadideahejbfcgdfdfiebbfgiegcaacgdiffbdjeghdhcfhijcdehdgaghegjehghjhaebbhcifgiehhdggdjbcjbdefhgdiejbidebgfjdgjfcijajbeajhhdhfbehgfgdahhhbedcfahgbifcebfhfhbhcdfddgdjaeajdjegbjchfiahjhagjjhadgajfbhacbbjgedbaibbbfiehjhadhfgiahbciaccabjaghdiijaebdagebbighjbbbbdahcjbcegbcihfaeehjhgdfdgbbfgbbcihhdcfbhfcdeeidgfcfaididdchccdhdjijhbjfafgjdgdgigfieiecbcchhiheedhjciieggeagcgahffcdajbgdbhegdjidbdidjhdadfhebcaccabejbjggijaccfjdibfcdhdahejachcifbbdicgdjafadddgjigdjggiijhafghcbhaiebibfjhgdchhjeiebcggccaaicggiedfdcafcgaffadbgbcibdhgedhdjejhjcebbbaecfbeegjdbaagahdddeahdccefafaibeheagiaifdhbfbhhaebfjbdjcahgfhhicghjbjfbaiaciigjbejhigjfdgegchjgaihhbibihfifedcchcaaefbibeicgdjjijhgecifhdedfeifihjechccdjdbaibddeieiggaahdgdgcjgichgibhcbcjhddegdahaehjgcdbgbdhdbbbejhjchadfjgcefahhchecdbihgedafaiegjcdfjiedhfjjbijcgihbeahcjebcdchddghgiabgbhidjacgbigbedfgbhghaihhjaijchceibhabchdbiddcddffaaiediciejebjjgbjciiaehdhcijiigbegdihdgjjdgeiafdiececjfdjhfjagcjdiedjjbcacbeiaggbfibfgfficbjgddghacdjiagdegfdgehbjdgcafgahfieegjgfbedidcejhaieccbgegaecfeifaehghchdffcfeagdgbcdjfhiieabbgjecfiefgecbcicbcedbeeffidihciijbcgibhddiajfachcefejdbjeceafeibdhbaffjijjihbaafbhjjfejgagehefdjfbaeaibcgcfffbhfcfgggjidbafbdhfcahbbhifbfdjcjjcaigjgdgdhgfebhfgjhfhcjchaigegdfdjgiifiaddebibcchbijficefhijafhaggdhfcabcafbcijbcjebhibbaigcighfjcdabdggejbdjbhfgghgjadfbgaiebbbbbddcdacaheaggeiidecejijiccfgbjabhidfdfhcebcgcebjfegcfdcbjdbbdffjfggbdhbibecgdeafijgdcbdccihacfebcijfijgabhgdeecefejhdehifafhbhjbheahgdejbdabjafebifjbbadcecgjeaejhdhciaecjgibcafdiegdbdhbgjafhbegefdahajbbcjbbifaajeggcdfigjdaijjfjffgcghbheaedihaedcdbdhbgbedfcjcfdiigfegjjhfidhhghbiiggefebaedfagjcccjhcfdgibbfajhdgbadecdfaabgbcagfcebjdcifgeehijdggdgjbffhhehbbhdbaihjeffaigefhdcahcbjegcfeifgfgagcicjccbhdgibegjdejjabcdjcahdidhjghbihddcjadbhjjjbfcighggjigbcdfjjhjehdcdeiaagaiffajjbegchjgadijedgffbifeifghejcgcghigfbaffigdiihffghbcfbedicdhcgigheeadcaafbhihjgccjaiaaggfaigcehggghghhifgafabegjgddicbhjhhdcbdhdhadbeeaaejaadhdghfgacbhgdgdbifabjbigifcfeejhbdeggejhidfaegacajgiebcihfafheejaijecjiahihbbfcgieahhbbgedeihjdhfbhaeghbgijjjaeffceijgdahacgdidbjdgifaheahadfbidacgjbegadigihegjfifghbbjgcihafdcfeacgibigfbjfjdceeedjehaaggjehbdfbiabigaiajfiigcjchfaaejjjeccgjcdgaihdgcbefedgchhdgahhgcaadjaehbcdaeaciajfacfafabeihbahjcejcjjjebhiihfeihiahjcddfbjffihifhibjfhfbcdaebgieejfbiiiiaieghajbceefgfjjfjfgfbffhhgcdjedjgajccdggdjcegbidhgjhieieijjiggbafgfiahihabceaejegddcfeecjjiiigbbfaifafeeffcjcgefigaejfcdejffiiaggcbgfdhgjdeiadebfdfgcbhffhdjefjfagdfeghdadjhbjicefefgfibjajcdbacjjdfjgdjjhfehjfbighjddjeccdgiiahheejgjjhcajigeabhggfdhhfejjbaedaddhjcdjedhgebidicgecgccdjggfaahbdgfgchbjhgdhijhgbijchddegbgigdhbfjediegfebdgcdiiifjdaggabegafgchbjfijgeidacdjbjjhbedafigebfebiiadcdjjjagaibccaggcgdeaicbijahichchcddidgaidachiegddcaejejddiigfihfaifgdfddbfaidcjiaajifgjfdddhejiijhchecdbbjchhfdigdhadbeifcghghcchbfbacheijbcgejbhccabdccbfgahhfggjigdaccefjidhchjigjdhgcchigheehbcjgffigfjfdfbcgfghhaghdciacjfidgghgdcijcgeiecggbfbjihfdjjdifbjahgfgjjgjbjcdcghfaafbjjjjcbcidfgicbjedjdbegadiefdcicegjibicjeajcjcciaigadbgbcehfbbdjidccddbjegebaggjcgcjfjfafcbiecggdebfhbajeagifaeaieabbbddabiecdcdeeechhafachfcejggcbbbhcegjbbgccfhjiejhfehcefbadgjeffcjjidecgahfdfcchejijajebcjfcbbifihciibccgbcbjbbeahcihhddjhcaijdfabcejdhacfcgjiaceiaebdfgecfjchecdgfddcjbcjdheeeahbfjajdfdfbgbfiaddeebehjjjagjbeefcciahdgebfajbafiegjideahfjibiggajgejcedgccciehhefehdaejfefahbjieadhcaiahejeeegbfddbcfbdcheedadejeaejhhdachidbgbchcbgiiegghdiihagighdhjaaccheeebaagcjadfbjjcihdjfcchdjcjjebahighbcbggbbbhgibfffcjgjfdafifcjhbafigdiejajecafajhbeiiehdcjbhbbbbcffbdffhdgfjjiiihihdaeidjifdicjcgadgccfjihjjhagebibgjbbhhbjeddjjejidcchjidaaddbfihffigifhcecihcigjaehcfaffedbgbjajgdjfcfifcgdidcjccgieaiedcacgegebhiacicebegbjaidcjfggbcdgjhcfihgcibcccejbicigbccgigdhfaabgibigicddfbegbbhjeedbdgedcbbibgfgcahajfbgiheffdeidcjfbefejjhcafdjfjfiifadabgeifchfhjccicdfhghidefafcadiiibhhegdjhbfbddceeajhbcbhdhihbhjcdbghibciceghcejidcghcihhadibddficigbgfbijihcfgiddhedbdfaggcbebihhehaahicjfegffbefffcdbabececiacihaedbdddaddgjgejgdgafhaedbbfiahajcjdajadbcghhdejajhafjgjjhjbbdbgcacffjbcbiaciejbgjffjaabhjciibfdcecaffafifcihjdgafgiijedadhfhceiechebhfigaageadjhheafihhgachgcafeedahdiaehiheiffijecjahafadfejdbaafgcedacigfeibjffgbddbgcgicaeehggihgiidifhhigeihccafehacacdihbgddafcaggaifdcbgbeijfdddegdgjfcidbjhcfgeffbefdhbehagjahgbbhiicdcfijjifdibfadeaicebdjeddichdghhijbhiaedfeabebbagggafeaiahggjacjbhjdcgcgfjbcjgiidddhefcffacdafgdgbegfbggbdihgabagdcbfcfbebajiefbaacjiiggiiacaafcjecabibaagfaahaggbjadijjihgbhicjdjgjgahafiedjegdabecjaajaeegbcbefigbhahfhgcdiddchjgaacehgjdhaehbjbjiieaidefiajafdgbadgedabhiiffdhjeibgcagafddahhihfdejeaaafgaighdgaaabcadigcigbdadbedcefidjacjjhdbedfehijigchceifefgeifihccbegbcfgjgabcegdceiahbccajgejdadjdajgjjhgfjgahciiajjjijbgfffffjbbcgjbaedcjjbhgebfjgfgjjghhaiaajehbcaieeaedgfehaajifigffijefabaaebbcedbbjebbcdhceahcdgbdhgebifbcaghagfcbidgfeehadbfhggbfdbcciggjfeaijjccfceiaijafdfagjadahjeabfhccibhddcjieijgaghcjcjchdjfjgaadicghjgfajcabajbhecgajbiiaefbbjibfgjedhijachhfiecdbeedhadcdiihdhccjaaaigjdcecbfjadccaejjbhfbibceaachdcdgffhjfdichgffjgeajgjdbjfedgceehafjbfdbdjieceifbaicbbjgddajihegcaicbcfhfgjajjgfbbhhhdhefcfaghhfhcifdcaafiifdcgjjccajciijideeccjbegaahjehiggadfijdefbaidhebfcjibcchebidbbffdiecbfiebedcjhaehjbcgjcbicibchibbdbhghhdjifaghifefgdhcdjcabgggcfhchjadfcaedbjhihhhgejdbhfbhagaifabbfcdbiceaafifddhjaciehjdeigehiagaaebfifegdecegbccgecdcbajgadajfdgfbjdcbafijdhdbabgajfdiagagfdcdchbagfdeafehhifhcabfciechjfiagdebadjeahcadfcahaagacjiehahacejaccegfhghiggfibfbhfffidbahcijhfbfdcciaajgjjjjadjjbjdgcediahhhjgcjhfddejfjbibeaiebhgjbjgigbacdiihjgcbegdgeibihbfffccfgicgifajafdeafheiigddhjaiieibhdfidfbbdbgicajeeiecdidhaibfaefbhjfadbfaadbhafghdijdegchegfigaaaeajcjhjdfjgcadbhdibhfgdffbeeggjdjdcbigfgheddhhcghficdiedciighdgaidfgbchhabjegfjddhegigbcadedihcbfdgajdbjjiegbicbhhiedhagccagcbdhhbgbfihiagefhaiaaigiaijjdabhiecejcbciajefggaceheiiaecdechiaefdcijffdfgdchedbbdhciiadaebbdgdhccfgdiaggcgffaigadecbiecbdhfedaehhbaejaeidgjdfacehaaeicbifggjaacfgcjgfjhiaeehahdicbfidgahehchfigajcbafahhjbhihfhcgdhihbijidahfgfjeiiabhagdbgcdffhajcdchjdedeeabhgecfgcbfccabgjbhdjddfigbcfachhdebajeidhjadccheadfjdgjaagedahhjefidfdciheechciebgfghaihhdhgiieaijgbfdjghejeeiejchgbfjhijfbfdjhjbbhahcecdcjidgggfgbdjbdhgeacdgbfjbhbeeegdgceigjidjhbbfjaggbbbjfabfijcefbagbjdcdiigbghhgejhacjecahbbbicicedbigbfghbhgbdjgceiibijhgbjbejifhjcejdjhaeejeghedjhibihfffbjbgiedcafgebaadgccbabcgfcjgbigfgfjahcadibffgfdjhgbgjcbaficgcihiehghhgggejadfghhebhhdfgcgcaacdedjheehdbddacgiijaiaiebbjiifihajhgajbififdcijgfcbgieidadiefhfefchfbfjdadibgcecfbaejjhfchedcgjdeggfjbebiaihihccefgibdafgfddhbgeghhgdcdfceajfbfibeieehbhcgjiddghfgagchefbeaegfbajcihjbihbgcgfgfeaegebbhhfcgcfahjffifghhhbbaiiigejcjafigcjbadjefgafccajccjffhfgcidchabfbbfiedcgcdbaiaaebjcjjfdhjaicbhdcifcdbfhhgcfabijciedhiejeacjhfjchddcjgfibghijcdeehbjcgijgfdfaedbeiicbfjjecfhebdbhiiefcgcbcbjbhhbcgbdhfjejaecibgbagcjjeffjbcedifbdddifahahgebceacbjjgacgjbhhchgecbjhiegebdhadcieegjfgaigfaceidiabhifghbjihjfjgjjafabjcigchjijadgiaibghfhgjjfdffbfagaededjiifijeggaffdbgidhacbeheajjeiaeigbihcajcbajchhdgdccbccfecejbbfhcaghbagffjcjicdcachjggdcdjhhigbbffhhbjccjbfgdhieaadfhgfhabcjfhhafiacbeaaicigcdhecchiebcaajaiedbghffiacijfebeahgchhheecaejdcibefbjheidhffefbgfieibaffgidifabghbifhfbcecccjihijecffhjaggdbbfffejfeihdjdhiidbaehaechhdfidggjicfahjiigfjgahjebadhjbjbjcacjehfjjfhecffighjfdjdchjdijbcibddbgdeffhjaccigcfbeihciaffifggbdabbbgiiecfcjhgedajghfbhcheecejegdeheicbdiieecdfcidicbhhdgddcfeiaajefjifjghgdjfgcijjfhihiigicdcjagfdabcaehajeaebcchbifbgcdbabcjhbjcegihefafchdgeabgdcccjdgidbibdfaifiejjhehdfaecifhghhjacacaijeajcehbgcjadcgigddifidfghgcbecaeidcfagjcbbjaaccdbacgbjcciadceigfadajgbadcchhgbjbccedeejbifjjidcjbaegcdgidigffcgedaajjffhhajeiiiffcfgjbfdhiccddieideehejccjefeidfeebiighabiehdehdfggbjcdecjfcffjbggbgbfcfdahccigbbibejbjhjcjdcijgebfiafiibgcafecegccjfcefddeifheddeiahcbicbafgcbhbgcgcijjbacdigicfghifbaddjheeiiicdfjeeejhaibigcicgecgechbhbffhfgbdbgjdcdhijibahbggagejccjbfhfhdcjcdbgbehigbbfbcfcdaeadggjgaadjhgeicefhahiafbhgjaajeibbafhbeifgfcgdbhjcbbjbiifhecgfcbjdhjhhgdjdajeeebghgjcbeegbciddchihcecjbdebjiaabaiidccjccchecdeeffccgjajfihibiaagihbjbghjfjdfbfeccgdgebdhbefhfifjgajabjdbchfjiiaihecibajgicfijbgfebbeghdjfigfbiadefccfcgafabgbcjgjjfdahhjihafibcgacdcejfiffihbegcggadeegfigjhfcgdaidaefaicgddfcdibdbifbjbbddiahghagjefdaegiciihheegjfhgebhfiddjaaajgagcafgahgagjijhfagiabjiiicdfjejebeegbgbbaifeaeghgeejajfhibgggjdhdbfedeadciheggjbdahegchibffdbhdfbhgcddebhafjicheghgcaajgjjebcceahabgigbgadaeiejfhfcgjfciidgfafibijjiadihffdciiaeegacjiiiijfdhbhfbbfabigigehijeiidieeedaahgcgeeicgaccffcaabhgbjgegibggbcbhebgcahgicfggghaigdehgjachfihcbaifjgbafjdjfahcecehcegbaedidggadbjajjfeeedidgiaeffhcaagabdbhhfgbjafedaajjffdagejcfgfgbdbijcdaiaebcafjccicjgcdbiehcbieeaeijdfiiaecceiijjdcbgieggciiceeeahgjacffhcedhaajccidfjafdigbbgcdchibeijhccdccciffjedcediehcfhffiageeffdcgfihjbijghaififjhgheabejghjgdjabhjgbigffbabicfbbgadhbccijfijgjfihfgjeccjjbdagjgchdiggefdhhcafaajiccdhidjgggadfehhgecfgdgaghhbjdhcdbabhjdcigbedcbgbhgdahhbebaifeecjhcjcgfcjehfghigjjagaaceccjcifeibbbcjdcjbdiibdgaacaeedeccgabecbeigdfhjajdbaibffjdchedcbbgbbaaffeaifejdeefcejdgdhhjicbdcdjigjccfjccbhefbbggajeahcidfajaifcggedbihdcacbbjcecdhbigjdifdbdhdbafeifagefhjjbifeaagcjicecaagehbcijegjediacdhjagcgdjihihicefdhjcadcigicfadggjfjggbcbehgejfjgbeahbjiabcgehficcagigefjbjaeiddjjdaigcbbgiiebhfhbhbcghgcefgjgjeaabghhgjdhicedfcgfdiajbedcbcfeiaddhagaaccdciibebadgbieagdgefgbgeeagefaehcbeichhfaebgbddgadcbghcgeiccjcciaifbegegfjajjdjdcgbhcjcjgihcjajehhbhjjdighdcagafffgehbbedbaidegghfgigidhijcbdgdcadgcefebjagdhjfeabbcjbfdgjgcdjfgfjdffajeaeddbfbigddjibcehafiedabaecdedfafacbgdbjiigbdefabdhdcbjbefaeedhchbecjifgaggcjjafchdegfdfgjfjjjjcfaiaddibebebfbbebdiaacgjafcjjhfhhfgbdbebbgajcffjcgabcjfeehgadhcdhbifeceiheiahajchhchcicbjahbbbjidfdahhjcdhibbddhbcbddjcchfdicgbaheabjefjgdjeddeiigjfccgdajdfiibbdjgdfehbhhcdejjfdjacffijhbaihfjddejafbfaedfefdecidjacdhheefdcdbcfgibbg")); -------------------------------------------------------------------------------- /387-First-Unique-Character-in-a-String.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/first-unique-character-in-a-string/description/ 3 | * Difficulty:Easy 4 | * 5 | * Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. 6 | * 7 | * Examples: 8 | * s = "leetcode" 9 | * return 0. 10 | * s = "loveleetcode", 11 | * return 2. 12 | * Note: You may assume the string contain only lowercase letters. 13 | * 14 | */ 15 | 16 | /** 17 | * @param {string} s 18 | * @return {number} 19 | */ 20 | var firstUniqChar = function (s) { 21 | 22 | for (var i = 0; i < s.length; i++) { 23 | var ch = s[i]; 24 | if (s.lastIndexOf(ch) === s.indexOf(ch)) return i; 25 | } 26 | return -1; 27 | }; 28 | 29 | console.log(firstUniqChar('leetcode')); 30 | console.log(firstUniqChar('loveleetcode')); -------------------------------------------------------------------------------- /392-Is-Subsequence.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/is-subsequence/description/ 3 | * Difficulty:Medium 4 | * 5 | * Given a string s and a string t, check if s is subsequence of t. 6 | * You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string (<=100). 7 | * A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace" is a subsequence of "abcde" while "aec" is not). 8 | * 9 | * Example 1: 10 | * s = "abc", t = "ahbgdc" 11 | * Return true. 12 | * 13 | * Example 2: 14 | * s = "axc", t = "ahbgdc" 15 | * Return false. 16 | * 17 | * Follow up: 18 | * If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code? 19 | */ 20 | 21 | /** 22 | * @param {string} s 23 | * @param {string} t 24 | * @return {boolean} 25 | */ 26 | var isSubsequence = function (s, t) { 27 | var i = 0; 28 | var j = 0; 29 | var m = s.length; 30 | var n = t.length; 31 | while (j < n && i < m) { 32 | if (s[i] === t[j]) { 33 | i++; 34 | } 35 | j++; 36 | } 37 | 38 | return i === m; 39 | }; 40 | 41 | console.log(isSubsequence('abc', 'asssbsssc')); 42 | console.log(isSubsequence('abc', 'asssbsss')); 43 | console.log(isSubsequence('', 'asssbsss')); -------------------------------------------------------------------------------- /404-Sum-of-Left-Leaves.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/sum-of-left-leaves/description/ 3 | * Difficulty:Easy 4 | * 5 | * Find the sum of all left leaves in a given binary tree. 6 | * Example: 7 | * 3 8 | * / \ 9 | * 9 20 10 | * / \ 11 | * 15 7 12 | * There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24. 13 | * 14 | */ 15 | 16 | /** 17 | * Definition for a binary tree node. 18 | * function TreeNode(val) { 19 | * this.val = val; 20 | * this.left = this.right = null; 21 | * } 22 | */ 23 | 24 | /** 25 | * @param {TreeNode} root 26 | * @return {number} 27 | */ 28 | var sumOfLeftLeaves = function (root) { 29 | return fn(root, false); 30 | }; 31 | 32 | function fn(node, isLeft) { 33 | if (!node) return 0; 34 | if (!node.left && !node.right) { 35 | return isLeft ? node.val : 0; 36 | } 37 | 38 | return fn(node.left, true) + fn(node.right, false); 39 | 40 | } 41 | 42 | -------------------------------------------------------------------------------- /413-Arithmetic-Slices.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/arithmetic-slices/description/ 3 | * Difficulty:Medium 4 | * 5 | * A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. 6 | * 7 | * For example, these are arithmetic sequence: 8 | * 1, 3, 5, 7, 9 9 | * 7, 7, 7, 7 10 | * 3, -1, -5, -9 11 | * 12 | * The following sequence is not arithmetic. 13 | * 1, 1, 2, 5, 7 14 | * 15 | * A zero-indexed array A consisting of N numbers is given. A slice of that array is any pair of integers (P, Q) such that 0 <= P < Q < N. 16 | * 17 | * A slice (P, Q) of array A is called arithmetic if the sequence: 18 | * 19 | * A[P], A[p + 1], ..., A[Q - 1], A[Q] is arithmetic. In particular, this means that P + 1 < Q. 20 | * 21 | * The function should return the number of arithmetic slices in the array A. 22 | * 23 | * Example: 24 | * A = [1, 2, 3, 4] 25 | * 26 | * return: 3, for 3 arithmetic slices in A: [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itself. 27 | * 28 | */ 29 | 30 | /** 31 | * @param {number[]} A 32 | * @return {number} 33 | */ 34 | var numberOfArithmeticSlices = function (A) { 35 | if (A.length < 3) return 0; 36 | var dp = []; 37 | if (A[2] - A[1] === A[1] - A[0]) { 38 | dp[2] = 1; 39 | } else { 40 | dp[2] = 0; 41 | } 42 | var result = dp[2]; 43 | 44 | for (var i = 3; i < A.length; i++) { 45 | if (A[i] - A[i - 1] === A[i - 1] - A[i - 2]) { 46 | dp[i] = dp[i - 1] + 1; 47 | result += dp[i]; 48 | } else { 49 | dp[i] = 0; 50 | } 51 | } 52 | 53 | return result; 54 | } 55 | ; -------------------------------------------------------------------------------- /415-Add-Strings.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/add-strings/description/ 3 | * Difficulty:Easy 4 | * 5 | * Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2. 6 | * Note: 7 | * 8 | * The length of both num1 and num2 is < 5100. 9 | * Both num1 and num2 contains only digits 0-9. 10 | * Both num1 and num2 does not contain any leading zero. 11 | * You must not use any built-in BigInteger library or convert the inputs to integer directly. 12 | */ 13 | 14 | /** 15 | * @param {string} num1 16 | * @param {string} num2 17 | * @return {string} 18 | */ 19 | var addStrings = function (num1, num2) { 20 | var carry = 0; 21 | var len1 = num1.length; 22 | var len2 = num2.length; 23 | var forCount = Math.max(len1, len2); 24 | var newNum = ''; 25 | for (var i = 0; i < forCount; i++) { 26 | var a = parseInt(num1[len1 - 1 - i]) || 0; 27 | var b = parseInt(num2[len2 - 1 - i]) || 0; 28 | var c = a + b + carry; 29 | carry = Math.floor(c / 10); 30 | var e = c % 10; 31 | newNum = e + newNum; 32 | } 33 | 34 | if (carry) { 35 | newNum = carry + newNum 36 | } 37 | return newNum; 38 | }; 39 | 40 | console.log(addStrings('12', '123') == '135'); -------------------------------------------------------------------------------- /416-Partition-Equal-Subset-Sum.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/partition-equal-subset-sum/description/ 3 | * Difficulty:Medium 4 | * 5 | * Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal. 6 | * Note: 7 | * Each of the array element will not exceed 100. 8 | * The array size will not exceed 200. 9 | * Example 1: 10 | * Input: [1, 5, 11, 5] 11 | * Output: true 12 | * Explanation: The array can be partitioned as [1, 5, 5] and [11]. 13 | * Example 2: 14 | * Input: [1, 2, 3, 5] 15 | * Output: false 16 | * Explanation: The array cannot be partitioned into equal sum subsets. 17 | */ 18 | 19 | /** 20 | * 21 | * 01背包问题 22 | * @param {number[]} nums 23 | * @return {boolean} 24 | */ 25 | var canPartition = function (nums) { 26 | var sum = nums.reduce((a, b) => a + b, 0); 27 | if (sum % 2) return false; 28 | sum = sum / 2; 29 | 30 | var n = nums.length; 31 | var dp = []; 32 | 33 | while (dp.push(new Array(sum + 1).fill(0)) < n + 1) ; 34 | 35 | 36 | for (var i = 0; i < n + 1; i++) { 37 | dp[i][0] = 1; 38 | } 39 | 40 | for (var i = 1; i < n + 1; i++) { 41 | for (var j = 1; j < sum + 1; j++) { 42 | if (dp[i - 1][j]) dp[i][j] = 1; 43 | if (j >= nums[i - 1] && dp[i - 1][j - nums[i - 1]]) dp[i][j] = 1; 44 | } 45 | } 46 | 47 | // console.log(dp); 48 | 49 | return !!dp[n][sum]; 50 | 51 | }; 52 | 53 | console.log(canPartition([1, 5])); 54 | console.log(canPartition([1, 5, 11, 5])); 55 | // console.log(canPartition([1, 5, 11, 5, 1, 1])); 56 | // console.log(canPartition([1, 5, 11, 5, 2])); 57 | // console.log(canPartition([1, 5, 11, 5])); 58 | // console.log(canPartition([1, 2, 3, 5])); -------------------------------------------------------------------------------- /447-Number-of-Boomerangs.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/number-of-boomerangs/description/ 3 | * Difficulty:Easy 4 | * 5 | * Given n points in the plane that are all pairwise distinct, 6 | * a "boomerang" is a tuple of points (i, j, k) such that the distance 7 | * between i and j equals the distance between i and k (the order of the tuple matters). 8 | * 9 | * Find the number of boomerangs. 10 | * You may assume that n will be at most 500 and 11 | * coordinates of points are all in the range [-10000, 10000] (inclusive). 12 | * 13 | * 14 | * 15 | * Example: 16 | * Input: 17 | * [[0,0],[1,0],[2,0]] 18 | * Output: 19 | * 2 20 | * Explanation: 21 | * The two boomerangs are [[1,0],[0,0],[2,0]] and [[1,0],[2,0],[0,0]] 22 | * 23 | * 24 | * 25 | * 解释 26 | * 27 | * 给定一个Point的数组, 找到所有的长度为3的Point数组(i, j, k) 28 | * 使得 i&j, i&k 的距离相同 29 | * 顺序有关 30 | * 31 | */ 32 | 33 | /** 34 | * @param {number[][]} points 35 | * @return {number} 36 | */ 37 | var numberOfBoomerangs = function (points) { 38 | if (points.length < 3) return 0; 39 | var ret = 0; 40 | for (var i = 0; i < points.length; i++) { 41 | var p1 = points[i]; 42 | var disMap = {}; 43 | 44 | for (var j = 0; j < points.length; j++) { 45 | if (i == j) continue; 46 | var p2 = points[j]; 47 | var dis = getDis(p1, p2); 48 | if (disMap[dis] == undefined) disMap[dis] = 1; 49 | else disMap[dis] += 1; 50 | } 51 | 52 | for (var key in disMap) { 53 | var n = disMap[key]; 54 | ret += n * (n - 1) 55 | } 56 | 57 | } 58 | 59 | return ret; 60 | 61 | }; 62 | 63 | function getDis(p1, p2) { 64 | var d1 = p2[1] - p1[1]; 65 | var d2 = p2[0] - p1[0]; 66 | return d1 * d1 + d2 * d2; 67 | } 68 | 69 | console.log(numberOfBoomerangs([[0, 0], [1, 0], [2, 0]]) == 2); 70 | console.log(numberOfBoomerangs([[1, 1]]) == 0); 71 | console.log(numberOfBoomerangs([[1, 1], [1, 2]]) == 0); 72 | console.log(numberOfBoomerangs([[0, 0], [1, 0], [-1, 0], [0, 1], [0, -1]]) == 20); 73 | console.log(1062 == numberOfBoomerangs([[693, 334], [439, 334], [421, 159], [985, 957], [354, 761], [762, 972], [541, 716], [852, 850], [662, 482], [399, 217], [154, 173], [15, 506], [851, 364], [790, 263], [491, 172], [37, 537], [859, 828], [871, 280], [987, 856], [590, 341], [970, 352], [665, 511], [69, 517], [361, 83], [351, 112], [300, 506], [638, 667], [364, 489], [32, 154], [104, 875], [679, 141], [412, 538], [969, 636], [170, 956], [844, 760], [649, 814], [465, 314], [326, 886], [183, 39], [969, 535], [152, 621], [393, 790], [289, 109], [631, 673], [264, 735], [548, 295], [877, 313], [833, 198], [949, 355], [155, 793], [468, 156], [960, 933], [823, 286], [171, 358], [677, 140], [245, 181], [761, 990], [323, 50], [100, 954], [75, 364], [42, 624], [659, 919], [289, 844], [469, 238], [551, 976], [383, 19], [133, 343], [304, 956], [981, 475], [666, 11], [967, 912], [192, 729], [902, 868], [131, 2], [174, 207], [718, 216], [183, 377], [487, 472], [573, 957], [62, 125], [933, 797], [496, 418], [141, 153], [726, 474], [980, 393], [485, 948], [305, 30], [29, 559], [898, 160], [562, 424], [719, 280], [641, 902], [10, 480], [726, 583], [789, 140], [708, 723], [938, 557], [493, 431], [710, 220], [905, 690], [613, 391], [638, 270], [421, 667], [829, 671], [180, 743], [95, 899], [24, 88], [154, 386], [569, 232], [969, 710], [373, 30], [433, 663], [587, 279], [94, 649], [499, 351], [339, 464], [742, 330], [86, 515], [349, 915], [186, 881], [11, 634], [133, 387], [722, 287], [773, 643], [519, 742], [354, 244], [124, 139], [259, 63], [418, 353], [712, 269], [705, 404], [733, 799], [734, 819], [315, 435], [87, 853], [669, 450], [487, 802], [837, 562], [89, 610], [205, 960], [704, 911], [557, 829], [403, 816], [892, 821], [522, 605], [443, 579], [361, 528], [378, 447], [700, 45], [882, 787], [899, 551], [589, 386], [353, 426], [948, 794], [36, 506], [107, 92], [417, 664], [921, 820], [480, 166], [994, 354], [123, 437], [933, 484], [317, 312], [931, 17], [709, 165], [156, 608], [69, 745], [995, 422], [171, 295], [569, 559], [801, 676], [652, 571], [340, 925], [743, 172], [91, 89], [527, 566], [878, 812], [50, 196], [124, 333], [213, 186], [499, 370], [794, 568], [115, 141], [342, 639], [437, 263], [198, 590], [939, 202], [513, 631], [128, 257], [804, 571], [346, 683], [138, 225], [495, 540], [421, 972], [226, 634], [158, 725], [356, 952], [645, 472], [446, 339], [111, 883], [603, 661], [825, 542], [216, 339], [174, 344], [596, 330], [267, 294], [13, 757], [519, 860], [650, 292], [832, 876], [279, 990], [953, 635], [295, 598], [107, 741], [937, 570], [976, 540], [584, 801], [83, 800], [492, 609], [496, 440], [939, 763], [735, 304], [873, 606], [164, 523], [251, 349], [751, 530], [339, 704], [165, 986], [302, 625], [79, 591], [547, 407], [484, 131], [561, 919], [931, 53], [528, 427], [494, 819], [543, 581], [123, 768], [187, 639], [643, 438], [988, 394], [320, 680], [98, 486], [18, 752], [463, 98], [695, 10], [505, 179], [142, 66], [98, 425], [472, 978], [853, 966], [797, 748], [547, 272], [516, 86], [912, 159], [877, 900], [553, 197], [932, 3], [35, 951], [107, 498], [49, 154], [861, 906], [334, 3], [325, 784], [428, 797], [763, 633], [115, 560], [381, 14], [185, 897], [100, 97], [408, 329], [349, 313], [879, 634], [316, 914], [585, 775], [765, 986], [930, 626], [892, 616], [629, 569], [752, 409], [366, 515], [395, 833], [428, 776], [847, 613], [26, 300], [62, 786], [629, 763], [100, 508], [397, 416], [775, 982], [544, 540], [320, 826], [518, 565], [794, 499], [134, 546], [908, 853], [62, 303], [686, 842], [432, 534], [807, 810], [834, 869], [596, 815], [984, 696], [324, 382], [465, 451], [716, 361], [343, 37], [187, 861], [602, 981], [360, 88], [879, 620], [941, 293], [924, 628], [135, 708], [162, 942], [870, 996], [163, 466], [811, 500], [515, 487], [234, 332], [290, 950], [693, 633], [339, 880], [494, 293], [213, 854], [382, 444], [475, 323], [738, 751], [951, 873], [811, 465], [168, 681], [813, 683], [499, 977], [535, 14], [464, 769], [346, 107], [72, 391], [740, 411], 74 | [623, 587], [57, 836], [793, 439], [281, 620], [114, 371], [371, 418], [596, 534], [883, 764], [567, 697], [800, 67], [674, 335], [433, 490], [457, 132], [949, 529], [523, 690], [292, 147], [629, 349], [335, 422], [140, 968], [43, 255], [339, 766], [25, 936], [653, 260], [52, 220], [957, 204], [287, 983], [540, 721], [826, 997], [205, 127], [878, 728], [169, 170], [227, 798], [520, 563], [221, 660], [883, 616], [267, 223], [382, 292], [511, 35], [553, 563], [608, 862], [768, 895], [198, 660], [968, 376], [9, 173], [503, 887], [254, 673], [57, 481], [823, 929], [396, 44], [942, 280], [12, 209], [855, 747], [854, 366], [134, 759], [281, 742], [973, 401], [638, 171], [413, 958], [899, 422], [484, 755], [661, 90], [780, 71], [923, 603], [0, 320], [0, 942], [952, 12], [504, 807], [759, 358], [525, 894], [117, 158], [636, 90], [560, 626], [614, 973], [937, 865], [748, 421], [620, 409], [863, 400], [832, 786], [4, 833], [458, 356], [127, 410], [368, 631], [569, 128], [341, 446], [374, 458], [605, 10], [901, 165], [989, 867], [490, 926], [732, 238], [699, 705], [0, 562], [457, 832], [348, 461], [17, 807], [169, 497], [569, 538], [128, 139], [18, 470], [585, 392], [280, 542], [754, 533], [707, 743], [400, 550], [21, 485], [788, 720], [190, 140], [634, 647], [325, 983], [461, 694], [142, 630], [191, 63], [520, 320], [554, 538], [142, 492], [930, 422], [34, 685], [308, 94], [780, 708], [644, 802], [545, 784], [522, 87], [925, 157], [735, 602], [492, 548], [296, 986], [530, 840], [49, 51], [512, 956], [589, 654], [448, 872], [428, 482], [557, 88], [576, 337], [797, 220], [139, 694], [5, 14], [782, 282], [523, 869], [236, 15], [417, 884], [353, 299], [724, 754], [350, 236], [710, 292], [242, 158], [164, 23], [641, 721], [111, 569], [410, 260], [790, 902], [955, 147], [916, 89], [781, 439], [958, 17], [806, 375], [901, 511], [674, 978], [265, 377], [566, 976], [669, 161], [134, 833]])) -------------------------------------------------------------------------------- /448-Find-All-Numbers-Disappeared-in-an-Array.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/ 3 | * Difficulty:Easy 4 | * 5 | * Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. 6 | * Find all the elements of [1, n] inclusive that do not appear in this array. 7 | * Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space. 8 | * Example: 9 | * Input: 10 | * [4,3,2,7,8,2,3,1] 11 | * Output: 12 | * [5,6] 13 | */ 14 | 15 | /** 16 | * @param {number[]} nums 17 | * @return {number[]} 18 | */ 19 | var findDisappearedNumbers = function (nums) { 20 | const ret = []; 21 | let i = 0; 22 | const n = nums.length; 23 | while (i < n) { 24 | // console.log(nums); 25 | const t = nums[i]; 26 | if (t !== i + 1 && nums[t - 1] !== t) { 27 | swap(nums, i, t - 1); 28 | continue; 29 | } 30 | i++; 31 | } 32 | 33 | for (let i = 0; i < n; i++) { 34 | if (nums[i] !== i + 1) ret.push(i + 1); 35 | } 36 | return ret; 37 | 38 | }; 39 | 40 | function swap(nums, i, j) { 41 | var t = nums[i]; 42 | nums[i] = nums[j]; 43 | nums[j] = t; 44 | } 45 | 46 | console.log(findDisappearedNumbers([4, 3, 2, 7, 8, 2, 3, 1])); -------------------------------------------------------------------------------- /453-Minimum-Moves-to-Equal-Array-Elements.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/minimum-moves-to-equal-array-elements/description/ 3 | * Difficulty:Easy 4 | * 5 | * Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1. 6 | * Example: 7 | * Input: 8 | * [1,2,3] 9 | * Output: 10 | * 3 11 | * Explanation: 12 | * Only three moves are needed (remember each move increments two elements): 13 | * [1,2,3] => [2,3,3] => [3,4,3] => [4,4,4] 14 | * 15 | */ 16 | 17 | /** 18 | * 19 | * reverse thinking! 20 | * 21 | * Adding 1 to n - 1 elements is the same as subtracting 1 from one element 22 | * 23 | * goal of making the elements in the array equal. 24 | * So, best way to do this is make all the elements in the array equal to the min element. 25 | */ 26 | 27 | /** 28 | * @param {number[]} nums 29 | * @return {number} 30 | */ 31 | var minMoves = function (nums) { 32 | 33 | var min = nums[0]; 34 | for (var i = 1; i < nums.length; i++) { 35 | min = Math.min(min, nums[i]); 36 | } 37 | 38 | var ret = 0; 39 | for (i = 0; i < nums.length; i++) { 40 | ret += nums[i] - min; 41 | } 42 | return ret; 43 | 44 | }; 45 | 46 | console.log(minMoves([1, 999, 1000])); 47 | -------------------------------------------------------------------------------- /454-4Sum-II.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) 3 | * there are such that A[i] + B[j] + C[k] + D[l] is zero. 4 | 5 | To make problem a bit easier, all A, B, C, D have same length of N where 0 ≤ N ≤ 500. 6 | All integers are in the range of -228 to 228 - 1 and the result is guaranteed to be at most 231 - 1. 7 | 8 | Example: 9 | 10 | Input: 11 | A = [ 1, 2] 12 | B = [-2,-1] 13 | C = [-1, 2] 14 | D = [ 0, 2] 15 | 16 | Output: 17 | 2 18 | 19 | Explanation: 20 | The two tuples are: 21 | 1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0 22 | 2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0 23 | */ 24 | 25 | /** 26 | * @param {number[]} A 27 | * @param {number[]} B 28 | * @param {number[]} C 29 | * @param {number[]} D 30 | * @return {number} 31 | */ 32 | var fourSumCount = function (A, B, C, D) { 33 | var cnt = 0; 34 | var map = {}; 35 | var n = A.length; 36 | 37 | for (var i = 0; i < n; i++) { 38 | for (var j = 0; j < n; j++) { 39 | var sum = A[i] + B[j]; 40 | if (map[sum] === undefined) map[sum] = 1; 41 | else map[sum]++; 42 | } 43 | } 44 | 45 | for (var i = 0; i < n; i++) { 46 | for (var j = 0; j < n; j++) { 47 | var sum = C[i] + D[j]; 48 | if (map[-sum] !== undefined) cnt += map[-sum]; 49 | } 50 | } 51 | return cnt; 52 | }; 53 | 54 | console.log(fourSumCount([1, 2], [-2, -2], [-1, 2], [0, 2])); -------------------------------------------------------------------------------- /455-Assign-Cookies.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/assign-cookies/description/ 3 | * Difficulty:Easy 4 | * 5 | * Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of a cookie that the child will be content with; and each cookie j has a size sj. If sj >= gi, we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number. 6 | * 7 | * Note: 8 | * You may assume the greed factor is always positive. 9 | * You cannot assign more than one cookie to one child. 10 | * Example 1: 11 | * Input: [1,2,3], [1,1] 12 | * Output: 1 13 | * Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3. 14 | * And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content. 15 | * You need to output 1. 16 | * 17 | */ 18 | 19 | /** 20 | * @param {number[]} g 21 | * @param {number[]} s 22 | * @return {number} 23 | */ 24 | var findContentChildren = function (g, s) { 25 | 26 | var ret = 0; 27 | g.sort((a, b) => a - b); 28 | s.sort((a, b) => a - b); 29 | var gIndex = 0; 30 | var sIndex = 0; 31 | 32 | while (gIndex < g.length && sIndex < s.length) { 33 | var gi = g[gIndex]; 34 | var si = s[sIndex]; 35 | if (gi <= si) { 36 | ret += 1; 37 | gIndex++; 38 | sIndex++; 39 | } else if (gi > si) { 40 | sIndex++; 41 | } else { 42 | break; 43 | } 44 | } 45 | 46 | return ret; 47 | 48 | }; 49 | 50 | console.log(findContentChildren([1, 2, 3], [1, 1])); 51 | console.log(findContentChildren([1,2], [1,2,3])); 52 | 53 | 54 | -------------------------------------------------------------------------------- /461-Hamming-Distance.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/hamming-distance/description/ 3 | * Difficulty:Easy 4 | * 5 | * The Hamming distance between two integers is the number of positions at which the corresponding bits are different. 6 | * Given two integers x and y, calculate the Hamming distance. 7 | * Note: 8 | * 0 ≤ x, y < 231. 9 | * Example: 10 | * Input: x = 1, y = 4 11 | * Output: 2 12 | * Explanation: 13 | * 1 (0 0 0 1) 14 | * 4 (0 1 0 0) 15 | * ↑ ↑ 16 | * The above arrows point to positions where the corresponding bits are different. 17 | * 18 | */ 19 | 20 | /** 21 | * @param {number} x 22 | * @param {number} y 23 | * @return {number} 24 | */ 25 | var hammingDistance = function (x, y) { 26 | 27 | var z = x ^ y; 28 | var ret = 0; 29 | while (z) { 30 | ret += z % 2; 31 | z = Math.floor(z / 2); 32 | } 33 | return ret; 34 | 35 | }; 36 | 37 | console.log(hammingDistance(1, 4)); -------------------------------------------------------------------------------- /476-Number-Complement.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/number-complement/description/ 3 | * Difficulty:Easy 4 | * 5 | * Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. 6 | * Note: 7 | * 8 | * The given integer is guaranteed to fit within the range of a 32-bit signed integer. 9 | * You could assume no leading zero bit in the integer’s binary representation. 10 | * 11 | * Example: 12 | * Input: 5 13 | * Output: 2 14 | * Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2. 15 | */ 16 | 17 | /** 18 | * @param {number} num 19 | * @return {number} 20 | */ 21 | var findComplement = function (num) { 22 | var str = ''; 23 | 24 | while (num) { 25 | str = ((num % 2) ? 0 : 1) + str; 26 | num = Math.floor(num / 2); 27 | } 28 | 29 | return parseInt(str, 2); 30 | }; 31 | 32 | console.log(findComplement(5)); 33 | console.log(findComplement(2)); -------------------------------------------------------------------------------- /492-Construct-the-Rectangle.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * https://leetcode.com/problems/construct-the-rectangle/description/ 4 | * Difficulty:Easy 5 | * 6 | * For a web developer, it is very important to know how to design a web page's size. 7 | * So, given a specific rectangular web page’s area, your job by now is to design a rectangular web page, 8 | * whose length L and width W satisfy the following requirements: 9 | * 1. The area of the rectangular web page you designed must equal to the given target area. 10 | * 2. The width W should not be larger than the length L, which means L >= W. 11 | * 3. The difference between length L and width W should be as small as possible. 12 | * You need to output the length L and the width W of the web page you designed in sequence. 13 | * 14 | * Example: 15 | * Input: 4 16 | * Output: [2, 2] 17 | * Explanation: The target area is 4, and all the possible ways to construct it are [1,4], [2,2], [4,1]. 18 | * But according to requirement 2, [1,4] is illegal; according to requirement 3, [4,1] is not optimal compared to [2,2]. So the length L is 2, and the width W is 2. 19 | * 20 | * Note: 21 | * The given area won't exceed 10,000,000 and is a positive integer 22 | * The web page's width and length you designed must be positive integers. 23 | * */ 24 | 25 | /** 26 | * @param {number} area 27 | * @return {number[]} 28 | */ 29 | var constructRectangle = function (area) { 30 | 31 | var w = Math.floor(Math.sqrt(area)); 32 | while (area % w != 0) w--; 33 | return [area / w, w]; 34 | 35 | }; 36 | 37 | console.log(constructRectangle(4)); 38 | console.log(constructRectangle(17)); -------------------------------------------------------------------------------- /500-Keyboard-Row.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/keyboard-row/description/ 3 | * Difficulty:Easy 4 | * 5 | * Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below. 6 | * 7 | * Example 1: 8 | * Input: ["Hello", "f", "Dad", "Peace"] 9 | * Output: ["Alaska", "Dad"] 10 | * Note: 11 | * You may use one character in the keyboard more than once. 12 | * You may assume the input string will only contain letters of alphabet. 13 | */ 14 | 15 | /** 16 | * @param {string[]} words 17 | * @return {string[]} 18 | */ 19 | var findWords = function (words) { 20 | 21 | var s = 'qwertyuiopasdfghjklzxcvbnm'; 22 | return words.filter(w => { 23 | if (!w) return true; 24 | w = w.toLowerCase(); 25 | var t = row(w[0]); 26 | for (var i = 1; i < w.length; i++) { 27 | if (t !== row(w[i])) return false; 28 | } 29 | return true; 30 | }); 31 | 32 | function row(ch) { 33 | var i = s.indexOf(ch); 34 | if (i < 10) return 0; 35 | if (i < 19) return 1; 36 | return 2; 37 | } 38 | 39 | }; 40 | console.log(findWords(["Hello", "Alaska", "Dad", "Peace"])) -------------------------------------------------------------------------------- /504-Base-7.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/base-7/description/ 3 | * Difficulty:Easy 4 | * 5 | * Given an integer, return its base 7 string representation. 6 | * Example 1: 7 | * Input: 100 8 | * Output: "202" 9 | * Example 2: 10 | * Input: -7 11 | * Output: "-10" 12 | * Note: The input will be in range of [-1e7, 1e7]. 13 | * 14 | */ 15 | 16 | /** 17 | * @param {number} num 18 | * @return {string} 19 | */ 20 | var convertToBase7 = function (num) { 21 | 22 | if (num === 0) return '0'; 23 | var str = ''; 24 | var sign = num > 0; 25 | num = Math.abs(num); 26 | while (num) { 27 | str = (num % 7) + str; 28 | num = Math.floor(num / 7); 29 | } 30 | 31 | return sign ? str : '-' + str; 32 | 33 | }; 34 | 35 | console.log(convertToBase7(0)); 36 | console.log(convertToBase7(100)); 37 | console.log(convertToBase7(-7)); -------------------------------------------------------------------------------- /506-Relative-Ranks.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/relative-ranks/description/ 3 | * Difficulty:Easy 4 | * 5 | * Given scores of N athletes, find their relative ranks and the people with the top three highest scores, who will be awarded medals: "Gold Medal", "Silver Medal" and "Bronze Medal". 6 | * 7 | * Example 1: 8 | * Input: [5, 4, 3, 2, 1] 9 | * Output: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"] 10 | * Explanation: The first three athletes got the top three highest scores, so they got "Gold Medal", "Silver Medal" and "Bronze Medal". 11 | * For the left two athletes, you just need to output their relative ranks according to their scores. 12 | * Note: 13 | * N is a positive integer and won't exceed 10,000. 14 | * All the scores of athletes are guaranteed to be unique. 15 | * 16 | */ 17 | 18 | /** 19 | * @param {number[]} nums 20 | * @return {string[]} 21 | */ 22 | var findRelativeRanks = function (nums) { 23 | 24 | var medals = ["Gold Medal", "Silver Medal", "Bronze Medal"]; 25 | var ranks = []; 26 | Array.prototype.push.apply(ranks, nums); 27 | ranks.sort(function (a, b) { 28 | return b - a; 29 | }); 30 | 31 | return nums.map((n) => { 32 | var rank = ranks.indexOf(n); 33 | return medals[rank] || (rank + 1) + ''; 34 | }); 35 | }; 36 | 37 | // ["Gold Medal","5","Bronze Medal","Silver Medal","4"] 38 | console.log(findRelativeRanks([10, 3, 8, 9, 4])); 39 | 40 | console.log(findRelativeRanks([5, 4, 1, 2, 3])); -------------------------------------------------------------------------------- /520-Detect-Capital.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/detect-capital/description/ 3 | * Difficulty:Easy 4 | * 5 | * Given a word, you need to judge whether the usage of capitals in it is right or not. 6 | * We define the usage of capitals in a word to be right when one of the following cases holds: 7 | * All letters in this word are capitals, like "USA". 8 | * All letters in this word are not capitals, like "leetcode". 9 | * Only the first letter in this word is capital if it has more than one letter, like "Google". 10 | * Otherwise, we define that this word doesn't use capitals in a right way. 11 | * 12 | */ 13 | 14 | /** 15 | * @param {string} word 16 | * @return {boolean} 17 | */ 18 | var detectCapitalUse = function (word) { 19 | 20 | var len = word.length; 21 | var upperLen = 0; 22 | var lowerLen = 0; 23 | var firstCap = false; 24 | for (var i = 0; i < len; i++) { 25 | var ch = word[i]; 26 | if (/[A-Z]/.test(ch)) { 27 | upperLen++; 28 | if (i == 0) { 29 | firstCap = true; 30 | } 31 | } 32 | if (/[a-z]/.test(ch)) { 33 | lowerLen++; 34 | } 35 | } 36 | if (upperLen == len) return true; 37 | if (lowerLen == len) return true; 38 | return (upperLen == 1 && firstCap && len > 1); 39 | }; 40 | 41 | /** 42 | * @param {string} word 43 | * @return {boolean} 44 | */ 45 | var detectCapitalUse = function (word) { 46 | 47 | var isFirstChUpper = false; 48 | var upperCnt = 0; 49 | var n = word.length; 50 | 51 | for (let i = 0; i < n; i++) { 52 | let ch = word[i]; 53 | if (ch >= 'A' && ch <= 'Z') { 54 | upperCnt++; 55 | if (i === 0) isFirstChUpper = true; 56 | } 57 | } 58 | 59 | if (isFirstChUpper) { 60 | return upperCnt === 1 || upperCnt === n; 61 | } else { 62 | return upperCnt === 0; 63 | } 64 | }; 65 | 66 | console.log(detectCapitalUse('USA')); 67 | console.log(detectCapitalUse('FlaG')); 68 | console.log(detectCapitalUse('A')); 69 | console.log(detectCapitalUse('Google')); 70 | console.log(detectCapitalUse('leetcode')); -------------------------------------------------------------------------------- /523-Continuous-Subarray-Sum.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/continuous-subarray-sum/description/ 3 | * Difficulty:Medium 4 | * 5 | * Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where n is also an integer 6 | * 7 | * Example 1: 8 | * Input: [23, 2, 4, 6, 7], k=6 9 | * Output: True 10 | * Explanation: Because [2, 4] is a continuous subarray of size 2 and sums up to 6. 11 | * 12 | * Example 2: 13 | * Input: [23, 2, 6, 4, 7], k=6 14 | * Output: True 15 | * Explanation: Because [23, 2, 6, 4, 7] is an continuous subarray of size 5 and sums up to 42. 16 | * 17 | * Note: 18 | * The length of the array won't exceed 10,000. 19 | * You may assume the sum of all the numbers is in the range of a signed 32-bit integer. 20 | */ 21 | 22 | /** 23 | * @param {number[]} nums 24 | * @param {number} k 25 | * @return {boolean} 26 | */ 27 | var checkSubarraySum = function (nums, k) { 28 | var arr = []; 29 | var sum = 0; 30 | if (nums.length < 2) return false; 31 | for (var i = 0; i < nums.length - 1; i++) { 32 | if (nums[i] + nums[i + 1] === 0) return true; 33 | } 34 | 35 | if (k < 0) k = -k; 36 | if (k === 0) return false; 37 | 38 | for (i = 0; i < nums.length; i++) { 39 | sum += nums[i]; 40 | sum = sum % k; 41 | if (i > 0 && sum === 0) return true; 42 | var index = arr.indexOf(sum); 43 | if (index !== -1) { 44 | return true; 45 | } 46 | arr.push(sum); 47 | } 48 | return false; 49 | }; 50 | 51 | console.log(checkSubarraySum([23, 2, 4, 6, 7], 6)); 52 | console.log(checkSubarraySum([23, 2, 4, 6, 7], 7)); 53 | console.log(checkSubarraySum([23, 2, 4, 6, 7], 8)); 54 | console.log(checkSubarraySum([0, 0], 0), true); 55 | console.log(checkSubarraySum([0, 1, 0], 0), false); 56 | console.log(checkSubarraySum([1, 1], 2), true); 57 | 58 | -------------------------------------------------------------------------------- /557-Reverse-Words-in-a-String-III.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/reverse-words-in-a-string-iii/description/ 3 | * Difficulty:Easy 4 | * 5 | * Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. 6 | * Example 1: 7 | * Input: "Let's take LeetCode contest" 8 | * Output: "s'teL ekat edoCteeL tsetnoc" 9 | * Note: In the string, each word is separated by single space and there will not be any extra space in the string. 10 | */ 11 | 12 | /** 13 | * @param {string} s 14 | * @return {string} 15 | */ 16 | var reverseWords = function (s) { 17 | return s.split(' ').map(w => w.split('').reverse().join('')).join(' '); 18 | }; 19 | 20 | console.log(reverseWords(`Let's take LeetCode contest`)) -------------------------------------------------------------------------------- /561-Array-Partition-I.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/array-partition-i/description/ 3 | * Difficulty:Easy 4 | * 5 | * Given an array of 2n integers, your task is to group these integers into n pairs of integer, 6 | * say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible. 7 | * 8 | * Example 1: 9 | * Input: [1,4,3,2] 10 | * Output: 4 11 | * Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4). 12 | * 13 | * Note: 14 | * n is a positive integer, which is in the range of [1, 10000]. 15 | * All the integers in the array will be in the range of [-10000, 10000]. 16 | */ 17 | 18 | /** 19 | * @param {number[]} nums 20 | * @return {number} 21 | */ 22 | var arrayPairSum = function (nums) { 23 | nums.sort((a, b) => { 24 | if (a < b) return -1; 25 | if (a > b) return 1; 26 | return 0; 27 | }); 28 | 29 | // console.log(nums); 30 | var sum = 0; 31 | for (var i = 0; i < nums.length / 2; i++) { 32 | sum += nums[2 * i]; 33 | } 34 | 35 | return sum; 36 | }; 37 | 38 | console.log(arrayPairSum([1, 4, 3, 2])); 39 | -------------------------------------------------------------------------------- /617-Merge-Two-Binary-Trees.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/merge-two-binary-trees/description/ 3 | * Difficulty:Easy 4 | * 5 | * Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. 6 | * You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree. 7 | * 8 | * Example 1: 9 | * Input: 10 | * Tree 1 Tree 2 11 | * 1 2 12 | * / \ / \ 13 | * 3 2 1 3 14 | * / \ \ 15 | * 5 4 7 16 | * 17 | * Output: 18 | * Merged tree: 19 | * 3 20 | * / \ 21 | * 4 5 22 | * / \ \ 23 | * 5 4 7 24 | * Note: The merging process must start from the root nodes of both trees. 25 | */ 26 | 27 | /** 28 | * Definition for a binary tree node. 29 | * function TreeNode(val) { 30 | * this.val = val; 31 | * this.left = this.right = null; 32 | * } 33 | */ 34 | /** 35 | * @param {TreeNode} t1 36 | * @param {TreeNode} t2 37 | * @return {TreeNode} 38 | */ 39 | var mergeTrees = function (t1, t2) { 40 | if (!t1) return t2; 41 | if (!t2) return t1; 42 | t1.val += t2.val; 43 | t1.left = mergeTrees(t1.left, t2.left); 44 | t1.right = mergeTrees(t1.right, t2.right); 45 | return t1; 46 | }; 47 | 48 | console.log(mergeTrees({ 49 | val: 1, 50 | left: { 51 | val: 3, 52 | left: { 53 | val: 5 54 | } 55 | }, 56 | right: { 57 | val: 2 58 | } 59 | }, { 60 | val: 2, 61 | left: { 62 | val: 1, 63 | right: { 64 | val: 4 65 | } 66 | }, 67 | right: { 68 | val: 3, 69 | right: { 70 | val: 7 71 | } 72 | } 73 | })) -------------------------------------------------------------------------------- /646-Maximum-Length-of-Pair-Chain.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/maximum-length-of-pair-chain/description/ 3 | * Difficulty:Medium 4 | * 5 | * You are given n pairs of numbers. In every pair, the first number is always smaller than the second number. 6 | * Now, we define a pair (c, d) can follow another pair (a, b) if and only if b < c. Chain of pairs can be formed in this fashion. 7 | * Given a set of pairs, find the length longest chain which can be formed. You needn't use up all the given pairs. You can select pairs in any order. 8 | * Example 1: 9 | * Input: [[1,2], [2,3], [3,4]] 10 | * Output: 2 11 | * Explanation: The longest chain is [1,2] -> [3,4] 12 | */ 13 | 14 | /** 15 | * 16 | * IN ANY ORDER! 17 | * SORT FIRST! 18 | * 19 | * @param {number[][]} pairs 20 | * @return {number} 21 | */ 22 | var findLongestChain = function (pairs) { 23 | var n = pairs.length; 24 | if (!n) return 0; 25 | 26 | pairs = pairs.sort((a, b) => { 27 | var diff = a[1] - b[1]; 28 | if (diff > 0) return 1; 29 | else if (diff === 0) return 0; 30 | return -1; 31 | }); 32 | // console.log(pairs); 33 | 34 | var dp = []; 35 | dp[0] = 1; 36 | var max = 1; 37 | for (var i = 1; i < n; i++) { 38 | var now = pairs[i]; 39 | dp[i] = 1; 40 | for (var j = 0; j < i; j++) { 41 | var last = pairs[j]; 42 | if (last[1] < now[0]) { 43 | dp[i] = Math.max(dp[j] + 1, dp[i]); 44 | } 45 | } 46 | } 47 | console.log(dp); 48 | return Math.max(...dp); 49 | }; 50 | 51 | // console.log(findLongestChain([])); 52 | // console.log(findLongestChain([[1, 2], [2, 3], [3, 4]])); 53 | // console.log(findLongestChain([[1, 2], [3, 4], [5, 46], [1, 2]])); 54 | console.log(findLongestChain([[3, 4], [2, 3], [1, 2]])); -------------------------------------------------------------------------------- /647-Palindromic-Substrings.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/palindromic-substrings/description/ 3 | * Difficulty:Medium 4 | * 5 | * 6 | * Given a string, your task is to count how many palindromic substrings in this string. 7 | * 8 | * The substrings with different start indexes or end indexes are counted as different substrings even they consist of same characters. 9 | * 10 | * Example 1: 11 | * Input: "abc" 12 | * Output: 3 13 | * Explanation: Three palindromic strings: "a", "b", "c". 14 | * 15 | * Example 2: 16 | * Input: "aaa" 17 | * Output: 6 18 | * Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa". 19 | * 20 | * Note: 21 | * The input string length won't exceed 1000. 22 | */ 23 | 24 | /** 25 | * @param {string} s 26 | * @return {number} 27 | */ 28 | var countSubstrings = function (s) { 29 | 30 | var dp = []; 31 | var n = s.length; 32 | for (var i = 0; i < n; i++) { 33 | dp.push(new Array(n).fill(false)); 34 | // dp[i][i] = 1; 35 | } 36 | var cnt = 0; 37 | for (var i = n - 1; i >= 0; i--) { 38 | for (var j = i; j < n; j++) { 39 | if (i === j) dp[i][j] = true; 40 | else { 41 | dp[i][j] = s[i] === s[j] ? (i + 1 >= j - 1 || dp[i + 1][j - 1]) : 0; 42 | } 43 | if (dp[i][j]) cnt++; 44 | } 45 | } 46 | // console.log(dp); 47 | 48 | return cnt; 49 | 50 | }; 51 | 52 | // console.log(countSubstrings('abc')); 53 | console.log(countSubstrings('aaaaa')); -------------------------------------------------------------------------------- /654-Maximum-Binary-Tree.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/maximum-binary-tree/description/ 3 | * Difficulty:Medium 4 | * 5 | * Given an integer array with no duplicates. 6 | * A maximum tree building on this array is defined as follow: 7 | * 8 | * The root is the maximum number in the array. 9 | * The left subtree is the maximum tree constructed from left part subarray divided by the maximum number. 10 | * The right subtree is the maximum tree constructed from right part subarray divided by the maximum number. 11 | * Construct the maximum tree by the given array and output the root node of this tree. 12 | * 13 | * Example 1: 14 | * Input: [3,2,1,6,0,5] 15 | * Output: return the tree root node representing the following tree: 16 | * 6 17 | * / \ 18 | * 3 5 19 | * \ / 20 | * 2 0 21 | * \ 22 | * 1 23 | * Note: 24 | * The size of the given array will be in the range [1,1000]. 25 | */ 26 | 27 | //Definition for a binary tree node. 28 | function TreeNode(val) { 29 | this.val = val; 30 | this.left = this.right = null; 31 | } 32 | 33 | /** 34 | * @param {number[]} nums 35 | * @return {TreeNode} 36 | */ 37 | var constructMaximumBinaryTree = function (nums) { 38 | return helper(nums, 0, nums.length); 39 | }; 40 | 41 | function helper(nums, s, e) { 42 | if (e <= s) return null; 43 | if (s + 1 === e) return new TreeNode(nums[s]); 44 | else { 45 | var maxIndex = findMaxIndex(nums, s, e); 46 | var root = new TreeNode(nums[maxIndex]); 47 | root.left = helper(nums, s, maxIndex); 48 | root.right = helper(nums, maxIndex + 1, e); 49 | return root; 50 | } 51 | } 52 | 53 | function findMaxIndex(nums, s, e) { 54 | 55 | var maxIndex = 0; 56 | var max = Number.NEGATIVE_INFINITY; 57 | for (var i = s; i < e; i++) { 58 | if (nums[i] > max) { 59 | max = nums[i]; 60 | maxIndex = i; 61 | } 62 | } 63 | return maxIndex; 64 | } 65 | 66 | console.log(constructMaximumBinaryTree([3, 2, 1, 6, 0, 5])) -------------------------------------------------------------------------------- /657-Judge-Route-Circle.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/judge-route-circle/description/ 3 | * Difficulty:Easy 4 | * 5 | * Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to the original place. 6 | * The move sequence is represented by a string. And each move is represent by a character. The valid robot moves are R (Right), L (Left), U (Up) and D (down). The output should be true or false representing whether the robot makes a circle. 7 | * 8 | * Example 1: 9 | * Input: "UD" 10 | * Output: true 11 | * 12 | * Example 2: 13 | * Input: "LL" 14 | * Output: false 15 | */ 16 | 17 | /** 18 | * @param {string} moves 19 | * @return {boolean} 20 | */ 21 | var judgeCircle = function (moves) { 22 | let v = 0; 23 | let h = 0; 24 | for (let i = 0; i < moves.length; i++) { 25 | switch (moves[i]) { 26 | case 'U': 27 | v++; 28 | break; 29 | case 'D': 30 | v--; 31 | break; 32 | case 'L': 33 | h--; 34 | break; 35 | case 'R': 36 | h++; 37 | } 38 | } 39 | return v === 0 && h === 0; 40 | }; 41 | console.log(judgeCircle('UD')); 42 | console.log(judgeCircle('LL')); 43 | console.log(judgeCircle('UDLLRR')); -------------------------------------------------------------------------------- /673-Number-of-Longest-Increasing-Subsequence.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/number-of-longest-increasing-subsequence/description/ 3 | * Difficulty:Medium 4 | * 5 | * Given an unsorted array of integers, find the number of longest increasing subsequence. 6 | * 7 | * Example 1: 8 | * Input: [1,3,5,4,7] 9 | * Output: 2 10 | * Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7]. 11 | * 12 | * Example 2: 13 | * Input: [2,2,2,2,2] 14 | * Output: 5 15 | * Explanation: The length of longest continuous increasing subsequence is 1, and there are 5 subsequences' length is 1, so output 5. 16 | * 17 | * Note: Length of the given array will be not exceed 2000 and the answer is guaranteed to be fit in 32-bit signed int. 18 | * 19 | */ 20 | /** 21 | * @param {number[]} nums 22 | * @return {number} 23 | */ 24 | var findNumberOfLIS = function (nums) { 25 | if (!nums.length) return 0; 26 | var dp = []; 27 | var longest = 0; 28 | for (var i = 0; i < nums.length; i++) { 29 | 30 | var max = 0; 31 | var count = 0; 32 | for (var j = 0; j < i; j++) { 33 | if (nums[j] < nums[i] && dp[j][0] > max) { 34 | max = dp[j][0]; 35 | } 36 | } 37 | 38 | for (var j = 0; j < i; j++) { 39 | if (dp[j][0] === max && nums[j] < nums[i]) { 40 | count += dp[j][1]; 41 | } 42 | } 43 | 44 | dp[i] = [max + 1, count ? count : 1]; 45 | longest = Math.max(longest, max + 1); 46 | } 47 | 48 | return dp.filter(t => t[0] === longest) 49 | .reduce((a, b) => a + b[1], 0); 50 | 51 | }; 52 | 53 | // console.log(findNumberOfLIS([1, 3, 5, 4, 7])); 54 | 55 | console.log(findNumberOfLIS([1, 2, 4, 3, 5, 4, 7, 2])); 56 | 57 | // console.log(findNumberOfLIS([1, 3, 5, 4])); 58 | // console.log(findNumberOfLIS([2, 2])); 59 | // console.log(findNumberOfLIS([2])); 60 | // console.log(findNumberOfLIS([2, 2, 2])); -------------------------------------------------------------------------------- /693-Binary-Number-with-Alternating-Bits.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/binary-number-with-alternating-bits/description/ 3 | * Difficulty:Easy 4 | * 5 | * Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values. 6 | * 7 | * Example 1: 8 | * Input: 5 9 | * Output: True 10 | * Explanation: 11 | * The binary representation of 5 is: 101 12 | * 13 | * Example 2: 14 | * Input: 7 15 | * Output: False 16 | * Explanation: 17 | * The binary representation of 7 is: 111. 18 | * 19 | * Example 3: 20 | * Input: 11 21 | * Output: False 22 | * Explanation: 23 | * The binary representation of 11 is: 1011. 24 | * 25 | * Example 4: 26 | * Input: 10 27 | * Output: True 28 | * Explanation: 29 | * The binary representation of 10 is: 1010. 30 | */ 31 | 32 | /** 33 | * @param {number} n 34 | * @return {boolean} 35 | */ 36 | var hasAlternatingBits = function (n) { 37 | var last = n % 2; 38 | n = Math.floor(n / 2); 39 | 40 | while (n > 0) { 41 | 42 | var r = n % 2; 43 | n = Math.floor(n / 2); 44 | // console.log(last); 45 | if (r === last) return false; 46 | last = r; 47 | } 48 | return true; 49 | }; 50 | 51 | console.log(hasAlternatingBits(3)); 52 | console.log(hasAlternatingBits(5)); 53 | console.log(hasAlternatingBits(7)); 54 | console.log(hasAlternatingBits(11)); 55 | console.log(hasAlternatingBits(10)); -------------------------------------------------------------------------------- /695-Max-Area-of-Island.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/max-area-of-island/description/ 3 | * 4 | * Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water. 5 | * Find the maximum area of an island in the given 2D array. (If there is no island, the maximum area is 0.) 6 | * 7 | * Example 1: 8 | * [[0,0,1,0,0,0,0,1,0,0,0,0,0], 9 | * [0,0,0,0,0,0,0,1,1,1,0,0,0], 10 | * [0,1,1,0,1,0,0,0,0,0,0,0,0], 11 | * [0,1,0,0,1,1,0,0,1,0,1,0,0], 12 | * [0,1,0,0,1,1,0,0,1,1,1,0,0], 13 | * [0,0,0,0,0,0,0,0,0,0,1,0,0], 14 | * [0,0,0,0,0,0,0,1,1,1,0,0,0], 15 | * [0,0,0,0,0,0,0,1,1,0,0,0,0]] 16 | * Given the above grid, return 6. Note the answer is not 11, because the island must be connected 4-directionally. 17 | * 18 | * Example 2: 19 | * [[0,0,0,0,0,0,0,0]] 20 | * Given the above grid, return 0. 21 | * 22 | * Note: The length of each dimension in the given grid does not exceed 50. 23 | * 24 | */ 25 | 26 | /** 27 | * @param {number[][]} grid 28 | * @return {number} 29 | */ 30 | var maxAreaOfIsland = function (grid) { 31 | var dp = []; 32 | var m = grid.length; 33 | if (!m) return 0; 34 | var n = grid[0].length; 35 | if (!n) return 0; 36 | while (dp.push(new Array(n).fill(0)) < m) ; 37 | var max = 0; 38 | for (var i = 0; i < m; i++) { 39 | for (var j = 0; j < n; j++) { 40 | max = Math.max(dfs(grid, dp, i, j), max) 41 | } 42 | } 43 | 44 | // console.log(dp); 45 | 46 | return max; 47 | }; 48 | 49 | function dfs(grid, dp, i, j) { 50 | var m = grid.length; 51 | var n = grid[0].length; 52 | 53 | if (i < 0 || i > m - 1) return 0; 54 | if (j < 0 || j > n - 1) return 0; 55 | // console.log(i, j, grid[i][j]); 56 | if (grid[i][j] === 0) dp[i][j] = -1; 57 | if (dp[i][j] === 0) { 58 | dp[i][j] = -1; 59 | dp[i][j] = 1 + dfs(grid, dp, i, j - 1) + dfs(grid, dp, i + 1, j) + dfs(grid, dp, i, j + 1) + dfs(grid, dp, i - 1, j); 60 | return dp[i][j]; 61 | } 62 | return 0; 63 | } 64 | 65 | console.log(maxAreaOfIsland([ 66 | [0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0], 67 | [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0], 68 | [0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], 69 | [0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0], 70 | [0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0], 71 | [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0], 72 | [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0], 73 | [0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0]])); 74 | 75 | console.log(maxAreaOfIsland([ 76 | [1, 1, 0, 0, 0], 77 | [1, 1, 0, 1, 0], 78 | [0, 0, 0, 1, 1], 79 | [0, 0, 0, 1, 1]])); 80 | console.log(maxAreaOfIsland([[0, 0, 1, 0, 0, 1, 1, 0]])); -------------------------------------------------------------------------------- /718-Maximum-Length-of-Repeated-Subarray.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/maximum-length-of-repeated-subarray/description/ 3 | * Difficulty:Medium 4 | * 5 | * Given two integer arrays A and B, return the maximum length of an subarray that appears in both arrays. 6 | * 7 | * Example 1: 8 | * Input: 9 | * A: [1, 2, 3, 2, 1] 10 | * B: [3, 2, 1, 4, 7, 8] 11 | * Output: 3 12 | * Explanation: 13 | * The repeated subarray with maximum length is [3, 2, 1]. 14 | * 15 | * Note: 16 | * 1 <= len(A), len(B) <= 1000 17 | * 0 <= A[i], B[i] < 100 18 | * 19 | */ 20 | 21 | /** 22 | * 解题思路 23 | * 24 | * 动态规划 25 | * 26 | * dp[i][j] 以 A[i-1] B[j-1] 结尾的最长子串长度 27 | * dp[i][j] = A[i - 1] === B[j - 1] ? dp[i - 1][j - 1] + 1 : 0; 28 | * 29 | * @param {number[]} A 30 | * @param {number[]} B 31 | * @return {number} 32 | */ 33 | var findLength = function (A, B) { 34 | 35 | var m = A.length; 36 | var n = B.length; 37 | if (m * n === 0) return 0; 38 | var dp = []; 39 | var max = 0; 40 | for (var i = 0; i <= m; i++) { 41 | dp.push(new Array(n + 1).fill(0)); 42 | } 43 | 44 | for (var i = 1; i <= m; i++) { 45 | for (var j = 1; j <= n; j++) { 46 | dp[i][j] = A[i - 1] === B[j - 1] ? dp[i - 1][j - 1] + 1 : 0; 47 | max = Math.max(max, dp[i][j]); 48 | } 49 | } 50 | // console.log(dp); 51 | 52 | return max; 53 | 54 | }; 55 | 56 | console.log(findLength([1, 2, 3, 2, 1], [3, 2, 1, 4, 7, 8])) -------------------------------------------------------------------------------- /728-Self-Dividing-Numbers.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/self-dividing-numbers/description/ 3 | * Difficulty:Easy 4 | * 5 | * A self-dividing number is a number that is divisible by every digit it contains. 6 | * 7 | * For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0. 8 | * Also, a self-dividing number is not allowed to contain the digit zero. 9 | * Given a lower and upper number bound, output a list of every possible self dividing number, including the bounds if possible. 10 | * 11 | * Example 1: 12 | * Input: 13 | * left = 1, right = 22 14 | * Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22] 15 | * Note: 16 | * The boundaries of each input argument are 1 <= left <= right <= 10000. 17 | */ 18 | 19 | /** 20 | * @param {number} left 21 | * @param {number} right 22 | * @return {number[]} 23 | */ 24 | var selfDividingNumbers = function (left, right) { 25 | var res = []; 26 | for (var i = left; i <= right; i++) { 27 | if (isSelfDividingNumber(i)) res.push(i); 28 | } 29 | return res; 30 | }; 31 | 32 | function isSelfDividingNumber(n) { 33 | var t = n; 34 | if (!t) return false; 35 | while (t) { 36 | var r = t % 10; 37 | t = Math.floor(t / 10); 38 | if (!r) return false; 39 | if (n % r) return false; 40 | } 41 | return true; 42 | } 43 | 44 | console.log(selfDividingNumbers(1, 22)); -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LeetCode in Javascript 2 | 3 | 一起来刷 [LeetCode](https://leetcode.com/) :D 4 | 5 | 如果你有感兴趣的题目或者需要更加详细的解释, 可以创建 [issue](https://github.com/xiaoyu2er/leetcode-js/issues) -------------------------------------------------------------------------------- /pack/01.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 01 背包问题 3 | * 4 | * 有 N 件物品和一个容量为 V 的背包。 5 | * 放入第 i 件物品耗费的费用是 Ci ,得到的 价值是 Wi。 6 | * 求解将哪些物品装入背包可使价值总和最大。 7 | * 8 | * F[i, v] = max{F[i − 1, v], F[i − 1, v − Ci ] + Wi } 9 | * 10 | * @param Cost 11 | * @param Worth 12 | * @param V 13 | * @returns {*} 14 | */ 15 | function pack(Cost, Worth, V) { 16 | var n = Cost.length; 17 | var dp = []; 18 | while (dp.push(new Array(V + 1).fill(0)) < n + 1); 19 | 20 | for (var i = 1; i < n + 1; i++) { 21 | for (var j = 1; j < V + 1; j++) { 22 | dp[i][j] = dp[i - 1][j]; 23 | if (j > Cost[i - 1]) { 24 | dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - Cost[i - 1]] + Worth[i - 1]); 25 | } 26 | 27 | } 28 | } 29 | 30 | for (var i = 1; i < dp.length; i++) { 31 | console.log(dp[i].slice(1)) 32 | } 33 | 34 | return dp[n][V]; 35 | } 36 | 37 | console.log(pack([5, 4, 7, 2, 6], [12, 3, 10, 3, 6], 15), 25); -------------------------------------------------------------------------------- /pack/02.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 2 完全背包问题 3 | * 有 N 种物品和一个容量为 V 的背包, 4 | * 每种物品都有无限件可用。放入第 i 种物品 的费用是 Ci,价值是 Wi。 5 | * 求解:将哪些物品装入背包,可使这些物品的耗费的费用总和不超过背包容量,且价值总和最大。 6 | * 7 | * F[i, v] = max{F[i − 1, v − k*Ci ] + k*Wi | 0 ≤ k*Ci ≤ v} 8 | * 9 | * @param Cost 10 | * @param Worth 11 | * @param V 12 | */ 13 | function pack(Cost, Worth, V) { 14 | var n = Cost.length; 15 | var dp = []; 16 | while (dp.push(new Array(V + 1).fill(0)) < n + 1); 17 | 18 | for (var i = 1; i < n + 1; i++) { 19 | for (var v = 1; v < V + 1; v++) { 20 | var c = Cost[i - 1]; 21 | var w = Worth[i - 1]; 22 | var count = Math.floor(v / c); 23 | var max = 0; 24 | for (var t = 0; t <= count; t++) { 25 | var newV = dp[i - 1][v - t * c] + t * w; 26 | if (newV > max) max = newV; 27 | } 28 | dp[i][v] = max; 29 | 30 | } 31 | } 32 | 33 | for (var i = 1; i < dp.length; i++) { 34 | console.log(dp[i].slice(1)) 35 | } 36 | 37 | return dp[n][V]; 38 | } 39 | 40 | console.log(pack([3, 2, 1], [7, 3, 1], 16)); 41 | console.log(pack([3, 2, 1], [7, 3, 1], 17)); 42 | console.log(pack([5, 4, 7, 2, 6], [12, 3, 10, 3, 6], 15)); 43 | console.log(pack([5, 4, 7, 2, 6], [12, 3, 10, 3, 6], 17)); -------------------------------------------------------------------------------- /pack/03.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 多重背包问题 3 | * 有 N 种物品和一个容量为 V 的背包。 4 | * 第 i 种物品最多有 Mi 件可用,每件耗费的 空间是 Ci ,价值是 Wi 。 5 | * 求解将哪些物品装入背包可使这些物品的耗费的空间总和不超 过背包容量,且价值总和最大。 6 | * 7 | * F[i,v] = max{F[i − 1, v − k∗Ci ] + k∗Wi | 0 ≤ k ≤ Mi } 8 | * 9 | * @param Cost 10 | * @param Worth 11 | * @param Much 12 | * @param V 13 | */ 14 | function pack(Cost, Worth, Much, V) { 15 | var n = Cost.length; 16 | var dp = []; 17 | while (dp.push(new Array(V + 1).fill(0)) < n + 1); 18 | 19 | for (var i = 1; i < n + 1; i++) { 20 | for (var v = 1; v < V + 1; v++) { 21 | var c = Cost[i - 1]; 22 | var w = Worth[i - 1]; 23 | var count = Math.min(Math.floor(v / c), Much[i - 1]); 24 | var max = 0; 25 | for (var t = 0; t <= count; t++) { 26 | var newV = dp[i - 1][v - t * c] + t * w; 27 | if (newV > max) max = newV; 28 | } 29 | dp[i][v] = max; 30 | 31 | } 32 | } 33 | 34 | for (var i = 1; i < dp.length; i++) { 35 | console.log(dp[i].slice(1)) 36 | } 37 | 38 | return dp[n][V]; 39 | } 40 | 41 | console.log(pack([5, 4, 7, 2, 6], [12, 3, 10, 3, 6], [2, 2, 1, 2, 7], 15)); 42 | console.log(pack([5, 4, 7, 2, 6], [12, 3, 10, 3, 6], [3, 2, 1, 2, 7], 15)); 43 | console.log(pack([5, 4, 7, 2, 6], [12, 3, 10, 3, 6], [3, 2, 1, 2, 7], 17)); --------------------------------------------------------------------------------