├── 0001.TwoSum.js ├── 0002.AddTwoNumbers.js ├── 0003.LongestSubstringWithoutRepeatingCharacters.js ├── 0005.LongestPalindromicSubstring.js ├── 0015.3Sum.js ├── 0019.RemoveNthNodeFromEndofList.js ├── 0020.ValidParentheses.js ├── 0021.MergeTwoSortedLists.js ├── 0024.SwapNodesinPairs.js ├── 0049.GroupAnagrams.js ├── 0053.MaximumSubarray.js ├── 0054.SpiralMatrix.js ├── 0055.JumpGame.js ├── 0056.MergeIntervals.js ├── 0062.UniquePaths.js ├── 0066.PlusOne.js ├── 0070.ClimbingStairs.js ├── 0073.SetMatrixZeroes.js ├── 0078.Subsets.js ├── 0083.RemoveDuplicatesfromSortedList.js ├── 0090.SubsetsII.js ├── 0092.ReverseLinkedListII.js ├── 0098.ValidateBinarySearchTree.js ├── 0102.BinaryTreeLevelOrderTraversal.js ├── 0104.MaximumDepthofBinaryTree.js ├── 0108.ConvertSortedArraytoBinarySearchTree.js ├── 0112.PathSum.js ├── 0113.PathSumII.js ├── 0121.BestTimetoBuyandSellStock.js ├── 0122.BestTimetoBuyandSellStockII.js ├── 0123.BestTimetoBuyandSellStockIII.js ├── 0125.ValidPalindrome.js ├── 0134.GasStation.js ├── 0141.LinkedListCycle.js ├── 0142.LinkedListCycleII.js ├── 0152.MaximumProductSubarray.js ├── 0153.FindMinimuminRotatedSortedArray.js ├── 0160.IntersectionofTwoLinkedLists.js ├── 0187.RepeatedDNASequences.js ├── 0188.BestTimetoBuyandSellStockIV.js ├── 0198.HouseRobber.js ├── 0200.NumberofIslands.js ├── 0206.ReverseLinkedList.js ├── 0217.ContainsDuplicate.js ├── 0219.ContainsDuplicateII.js ├── 0226.InvertBinaryTree.js ├── 0235.LowestCommonAncestorofaBinarySearchTree.js ├── 0238.ProductofArrayExceptSelf.js ├── 0242.ValidAnagram.js ├── 0283.MoveZeroes.js ├── 0328.OddEvenLinkedList.js ├── 0349.IntersectionofTwoArrays.js ├── 0404.SumofLeftLeaves.js ├── 0419.BattleshipsinaBoard.js ├── 0445.AddTwoNumbersII.js ├── 0509.FibonacciNumber.js ├── 0680.ValidPalindromeII.js ├── 0695.MaxAreaofIsland.js ├── 0704.BinarySearch.js ├── 0733.FloodFill.js ├── 0796.RotateString.js ├── 0836.RectangleOverlap.js ├── 0844.BackspaceStringCompare.js ├── 0876.MiddleoftheLinkedList.js ├── 0904.FruitIntoBaskets.js ├── 0905.SortArrayByParity.js └── 0922.SortArrayByParityII.js /0001.TwoSum.js: -------------------------------------------------------------------------------- 1 | var twoSum = function (nums, target) { 2 | const map = new Map(); 3 | for (let i = 0; i < nums.length; i++) { 4 | const complement = target - nums[i]; 5 | if (map.has(complement)) { 6 | return [map.get(complement), i]; 7 | } else { 8 | map.set(nums[i], i); 9 | } 10 | } 11 | return []; 12 | }; 13 | -------------------------------------------------------------------------------- /0002.AddTwoNumbers.js: -------------------------------------------------------------------------------- 1 | var addTwoNumbers = function(l1, l2) { 2 | let dummy = new ListNode(); 3 | let curr = dummy; 4 | let carry = 0; 5 | while(l1 !== null || l2 !== null){ 6 | let sum = 0; 7 | if(l1!== null){ 8 | sum += l1.val; 9 | l1 = l1.next; 10 | } 11 | if(l2!== null){ 12 | sum += l2.val; 13 | l2 = l2.next; 14 | } 15 | 16 | sum += carry; 17 | curr.next = new ListNode(sum%10); 18 | carry = Math.floor(sum/10); 19 | curr = curr.next; 20 | } 21 | 22 | if(carry >0){ 23 | curr.next = new ListNode(carry); 24 | } 25 | return dummy.next; 26 | }; -------------------------------------------------------------------------------- /0003.LongestSubstringWithoutRepeatingCharacters.js: -------------------------------------------------------------------------------- 1 | var lengthOfLongestSubstring = function (s) { 2 | const set = new Set(); 3 | let i = 0, 4 | j = 0, 5 | maxLength = 0; 6 | if (s.length === 0) { 7 | return 0; 8 | } 9 | 10 | for (i; i < s.length; i++) { 11 | if (!set.has(s[i])) { 12 | set.add(s[i]); 13 | maxLength = Math.max(maxLength, set.size); 14 | } else { 15 | while (set.has(s[i])) { 16 | set.delete(s[j]); 17 | j++; 18 | } 19 | set.add(s[i]); 20 | } 21 | } 22 | 23 | return maxLength; 24 | }; 25 | -------------------------------------------------------------------------------- /0005.LongestPalindromicSubstring.js: -------------------------------------------------------------------------------- 1 | var longestPalindrome = function (s) { 2 | if (s.length < 2) { 3 | return s; 4 | } 5 | 6 | let start = 0; 7 | let maxLength = 1; 8 | 9 | function expandAroundCenter(left, right) { 10 | while (left >= 0 && right < s.length && s[left] === s[right]) { 11 | if (right - left + 1 > maxLength) { 12 | maxLength = right - left + 1; 13 | start = left; 14 | } 15 | left--; 16 | right++; 17 | } 18 | } 19 | 20 | for (let i = 0; i < s.length; i++) { 21 | expandAroundCenter(i - 1, i + 1); 22 | expandAroundCenter(i, i + 1); 23 | } 24 | 25 | return s.substring(start, start + maxLength); 26 | }; 27 | -------------------------------------------------------------------------------- /0015.3Sum.js: -------------------------------------------------------------------------------- 1 | var threeSum = function (nums) { 2 | const result = []; 3 | nums.sort(function (a, b) { 4 | return a - b; 5 | }); 6 | 7 | for (let i = 0; i < nums.length - 2; i++) { 8 | if (i === 0 || nums[i] !== nums[i - 1]) { 9 | let start = i + 1, 10 | end = nums.length - 1; 11 | while (start < end) { 12 | if (nums[i] + nums[start] + nums[end] === 0) { 13 | result.push([nums[i], nums[start], nums[end]]); 14 | start++; 15 | end--; 16 | while (start < end && nums[start] === nums[start - 1]) { 17 | start++; 18 | } 19 | while (start < end && nums[end] === nums[end + 1]) { 20 | end--; 21 | } 22 | } else if (nums[i] + nums[start] + nums[end] < 0) { 23 | start++; 24 | } else { 25 | end--; 26 | } 27 | } 28 | } 29 | } 30 | 31 | return result; 32 | }; 33 | -------------------------------------------------------------------------------- /0019.RemoveNthNodeFromEndofList.js: -------------------------------------------------------------------------------- 1 | var removeNthFromEnd = function (head, n) { 2 | let dummy = new ListNode(); 3 | dummy.next = head; 4 | 5 | let n1 = dummy; 6 | let n2 = dummy; 7 | for (let i = 0; i < n; i++) { 8 | n2 = n2.next; 9 | } 10 | while (n2.next != null) { 11 | n1 = n1.next; 12 | n2 = n2.next; 13 | } 14 | n1.next = n1.next.next; 15 | return dummy.next; 16 | }; 17 | -------------------------------------------------------------------------------- /0020.ValidParentheses.js: -------------------------------------------------------------------------------- 1 | var isValid = function (s) { 2 | const mappings = new Map(); 3 | mappings.set("(", ")"); 4 | mappings.set("[", "]"); 5 | mappings.set("{", "}"); 6 | 7 | const stack = []; 8 | for (let i = 0; i < s.length; i++) { 9 | if (mappings.has(s[i])) { 10 | stack.push(mappings.get(s[i])); 11 | } else { 12 | if (stack.pop() !== s[i]) { 13 | return false; 14 | } 15 | } 16 | } 17 | 18 | if (stack.length !== 0) { 19 | return false; 20 | } 21 | 22 | return true; 23 | }; 24 | -------------------------------------------------------------------------------- /0021.MergeTwoSortedLists.js: -------------------------------------------------------------------------------- 1 | var mergeTwoLists = function (l1, l2) { 2 | let curr = new ListNode(); 3 | let dummy = curr; 4 | 5 | while (l1 !== null && l2 !== null) { 6 | if (l1.val < l2.val) { 7 | curr.next = l1; 8 | l1 = l1.next; 9 | } else { 10 | curr.next = l2; 11 | l2 = l2.next; 12 | } 13 | 14 | curr = curr.next; 15 | } 16 | 17 | if (l1 !== null) { 18 | curr.next = l1; 19 | } 20 | 21 | if (l2 !== null) { 22 | curr.next = l2; 23 | } 24 | 25 | return dummy.next; 26 | }; 27 | -------------------------------------------------------------------------------- /0024.SwapNodesinPairs.js: -------------------------------------------------------------------------------- 1 | var swapPairs = function (head) { 2 | let dummy = new ListNode(); 3 | dummy.next = head; 4 | let current = dummy; 5 | while (current.next !== null && current.next.next !== null) { 6 | let n1 = current.next; 7 | let n2 = current.next.next; 8 | current.next = n2; 9 | n1.next = n2.next; 10 | n2.next = n1; 11 | current = n1; 12 | } 13 | 14 | return dummy.next; 15 | }; 16 | -------------------------------------------------------------------------------- /0049.GroupAnagrams.js: -------------------------------------------------------------------------------- 1 | var groupAnagrams = function (strs) { 2 | if (strs.length === 0) { 3 | return []; 4 | } 5 | const map = new Map(); 6 | 7 | for (const str of strs) { 8 | const characters = Array(26).fill(0); 9 | for (let i = 0; i < str.length; i++) { 10 | const ascii = str.charCodeAt(i) - 97; 11 | characters[ascii]++; 12 | } 13 | const key = characters.join(" "); // 感谢L30Yu提交bug并提出解决方案: https://github.com/jslaobi/jslaobi-leetcode-js/issues/1 14 | if (map.has(key)) { 15 | map.set(key, [...map.get(key), str]); 16 | } else { 17 | map.set(key, [str]); 18 | } 19 | } 20 | 21 | const result = []; 22 | for (const arr of map) { 23 | result.push(arr[1]); 24 | } 25 | 26 | return result; 27 | }; 28 | -------------------------------------------------------------------------------- /0053.MaximumSubarray.js: -------------------------------------------------------------------------------- 1 | var maxSubArray = function (nums) { 2 | const memo = []; 3 | memo[0] = nums[0]; 4 | 5 | for (let i = 1; i < nums.length; i++) { 6 | memo[i] = Math.max(nums[i] + memo[i - 1], nums[i]); 7 | } 8 | 9 | let max = nums[0]; 10 | 11 | for (let i = 1; i < memo.length; i++) { 12 | max = Math.max(max, memo[i]); 13 | } 14 | 15 | return max; 16 | }; 17 | -------------------------------------------------------------------------------- /0054.SpiralMatrix.js: -------------------------------------------------------------------------------- 1 | var spiralOrder = function (matrix) { 2 | if (matrix.length === 0) { 3 | return []; 4 | } 5 | 6 | let top = 0; 7 | let bottom = matrix.length - 1; 8 | let left = 0; 9 | let right = matrix[0].length - 1; 10 | 11 | let direction = "right"; 12 | let result = []; 13 | 14 | while (left <= right && top <= bottom) { 15 | if (direction === "right") { 16 | for (let i = left; i <= right; i++) { 17 | result.push(matrix[top][i]); 18 | } 19 | top++; 20 | direction = "down"; 21 | } else if (direction === "down") { 22 | for (let i = top; i <= bottom; i++) { 23 | result.push(matrix[i][right]); 24 | } 25 | right--; 26 | direction = "left"; 27 | } else if (direction === "left") { 28 | for (let i = right; i >= left; i--) { 29 | result.push(matrix[bottom][i]); 30 | } 31 | bottom--; 32 | direction = "top"; 33 | } else if (direction === "top") { 34 | for (let i = bottom; i >= top; i--) { 35 | result.push(matrix[i][left]); 36 | } 37 | left++; 38 | direction = "right"; 39 | } 40 | } 41 | 42 | return result; 43 | }; 44 | -------------------------------------------------------------------------------- /0055.JumpGame.js: -------------------------------------------------------------------------------- 1 | var canJump = function (nums) { 2 | let maxJump = nums.length - 1; 3 | for (let i = nums.length - 2; i >= 0; i--) { 4 | if (i + nums[i] >= maxJump) { 5 | maxJump = i; 6 | } 7 | } 8 | 9 | return maxJump === 0; 10 | }; 11 | -------------------------------------------------------------------------------- /0056.MergeIntervals.js: -------------------------------------------------------------------------------- 1 | var merge = function (intervals) { 2 | if (intervals.length < 2) { 3 | return intervals; 4 | } 5 | intervals.sort(function (a, b) { 6 | return a[0] - b[0]; 7 | }); 8 | 9 | let curr = intervals[0]; 10 | let result = []; 11 | 12 | for (let interval of intervals) { 13 | if (curr[1] >= interval[0]) { 14 | curr[1] = Math.max(curr[1], interval[1]); 15 | } else { 16 | result.push(curr); 17 | curr = interval; 18 | } 19 | } 20 | 21 | if (curr.length !== 0) { 22 | result.push(curr); 23 | } 24 | 25 | return result; 26 | }; 27 | -------------------------------------------------------------------------------- /0062.UniquePaths.js: -------------------------------------------------------------------------------- 1 | var uniquePaths = function (m, n) { 2 | const memo = []; 3 | for (let i = 0; i < n; i++) { 4 | memo.push([]); 5 | } 6 | for (let row = 0; row < n; row++) { 7 | memo[row][0] = 1; 8 | } 9 | 10 | for (let col = 0; col < m; col++) { 11 | memo[0][col] = 1; 12 | } 13 | 14 | for (let row = 1; row < n; row++) { 15 | for (let col = 1; col < m; col++) { 16 | memo[row][col] = memo[row - 1][col] + memo[row][col - 1]; 17 | } 18 | } 19 | 20 | return memo[n - 1][m - 1]; 21 | }; 22 | -------------------------------------------------------------------------------- /0066.PlusOne.js: -------------------------------------------------------------------------------- 1 | var plusOne = function (digits) { 2 | for (let i = digits.length - 1; i >= 0; i--) { 3 | if (digits[i] !== 9) { 4 | digits[i]++; 5 | return digits; 6 | } else { 7 | digits[i] = 0; 8 | } 9 | } 10 | 11 | const result = [1, ...digits]; 12 | 13 | return result; 14 | }; 15 | -------------------------------------------------------------------------------- /0070.ClimbingStairs.js: -------------------------------------------------------------------------------- 1 | var climbStairs = function (n) { 2 | const memo = []; 3 | memo[1] = 1; 4 | memo[2] = 2; 5 | for (let i = 3; i <= n; i++) { 6 | memo[i] = memo[i - 1] + memo[i - 2]; 7 | } 8 | 9 | return memo[n]; 10 | }; 11 | -------------------------------------------------------------------------------- /0073.SetMatrixZeroes.js: -------------------------------------------------------------------------------- 1 | var setZeroes = function (matrix) { 2 | let firstColHasZero = false; 3 | let firstRowHasZero = false; 4 | // 检查第一列是否有0 5 | for (let i = 0; i < matrix.length; i++) { 6 | if (matrix[i][0] === 0) { 7 | firstColHasZero = true; 8 | } 9 | } 10 | // 检查第一行是否有0 11 | for (let i = 0; i < matrix[0].length; i++) { 12 | if (matrix[0][i] === 0) { 13 | firstRowHasZero = true; 14 | } 15 | } 16 | // 使用第一行和第一列,来标记其余行列是否含有0 17 | for (let row = 1; row < matrix.length; row++) { 18 | for (let col = 1; col < matrix[0].length; col++) { 19 | if (matrix[row][col] === 0) { 20 | matrix[row][0] = 0; 21 | matrix[0][col] = 0; 22 | } 23 | } 24 | } 25 | // 接下来,利用第一行和第一列的标0情况,将matrix中的数字标0 26 | for (let row = 1; row < matrix.length; row++) { 27 | for (let col = 1; col < matrix[0].length; col++) { 28 | if (matrix[row][0] === 0 || matrix[0][col] === 0) { 29 | matrix[row][col] = 0; 30 | } 31 | } 32 | } 33 | // 最后,处理第一行和第一列 34 | // 如果firstColHasZero等于true,将第一列全设为0 35 | if (firstColHasZero) { 36 | for (let i = 0; i < matrix.length; i++) { 37 | matrix[i][0] = 0; 38 | } 39 | } 40 | // 如果firstRowHasZero等于true,将第一行全设为0 41 | if (firstRowHasZero) { 42 | for (let i = 0; i < matrix[0].length; i++) { 43 | matrix[0][i] = 0; 44 | } 45 | } 46 | 47 | return matrix; 48 | }; 49 | -------------------------------------------------------------------------------- /0078.Subsets.js: -------------------------------------------------------------------------------- 1 | var subsets = function(nums) { 2 | const result = []; 3 | function backtrack(start, curr){ 4 | result.push([...curr]); 5 | for(let i=start;i{ 4 | return a-b; 5 | }); 6 | 7 | function backtrack(start, curr){ 8 | result.push([...curr]); 9 | for(let i=start;istart && nums[i] === nums[i-1]){ 11 | continue; 12 | } 13 | curr.push(nums[i]) 14 | backtrack(i+1,curr); 15 | curr.pop(); 16 | } 17 | } 18 | 19 | backtrack(0,[]); 20 | return result; 21 | }; -------------------------------------------------------------------------------- /0092.ReverseLinkedListII.js: -------------------------------------------------------------------------------- 1 | var reverseBetween = function (head, m, n) { 2 | let prev = null; 3 | let curr = head; 4 | for (let i = 1; i < m; i++) { 5 | prev = curr; 6 | curr = curr.next; 7 | } 8 | 9 | let prev2 = prev; 10 | let curr2 = curr; 11 | 12 | for (let i = m; i <= n; i++) { 13 | [curr.next, prev, curr] = [prev, curr, curr.next]; 14 | } 15 | if (m > 1) { 16 | prev2.next = prev; 17 | } else { 18 | head = prev; 19 | } 20 | curr2.next = curr; 21 | return head; 22 | }; 23 | -------------------------------------------------------------------------------- /0098.ValidateBinarySearchTree.js: -------------------------------------------------------------------------------- 1 | var isValidBST = function(root) { 2 | function isValid(node, min, max){ 3 | if(node === null){ 4 | return true; 5 | }else{ 6 | if(node.val <= min || node.val >= max){ 7 | return false; 8 | } 9 | 10 | return isValid(node.left, min, node.val) && isValid(node.right, node.val, max); 11 | } 12 | } 13 | 14 | return isValid(root, -Infinity, Infinity) 15 | }; -------------------------------------------------------------------------------- /0102.BinaryTreeLevelOrderTraversal.js: -------------------------------------------------------------------------------- 1 | var levelOrder = function(root) { 2 | const result = []; 3 | 4 | if(root === null){ 5 | return result; 6 | } 7 | 8 | const queue = []; 9 | queue.push(root); 10 | 11 | while(queue.length !== 0){ 12 | const size = queue.length; 13 | const level = []; 14 | for(let i=0;i right){ 8 | return null; 9 | } 10 | 11 | const mid = Math.floor(left + (right - left) / 2); 12 | const root = new TreeNode(nums[mid]); 13 | root.left = convertToBST(left, mid-1); 14 | root.right = convertToBST(mid+1, right); 15 | return root; 16 | } 17 | 18 | return convertToBST(0, nums.length-1); 19 | }; -------------------------------------------------------------------------------- /0112.PathSum.js: -------------------------------------------------------------------------------- 1 | var hasPathSum = function(root, sum) { 2 | if(root === null){ 3 | return false; 4 | }else if(root.left === null && root.right === null && sum - root.val === 0){ 5 | return true; 6 | }else{ 7 | return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val) 8 | } 9 | }; -------------------------------------------------------------------------------- /0113.PathSumII.js: -------------------------------------------------------------------------------- 1 | var pathSum = function(root, sum) { 2 | const result = []; 3 | 4 | findPath(root, sum, []); 5 | function findPath(node, sum, path){ 6 | 7 | if(node === null){ 8 | return; 9 | } 10 | path = [...path, node.val]; 11 | sum -= node.val; 12 | if(node.left === null && node.right === null && sum === 0){ 13 | result.push(path); 14 | return; 15 | } 16 | 17 | findPath(node.left, sum, path); 18 | findPath(node.right, sum, path); 19 | } 20 | 21 | return result; 22 | }; -------------------------------------------------------------------------------- /0121.BestTimetoBuyandSellStock.js: -------------------------------------------------------------------------------- 1 | var maxProfit = function (prices) { 2 | if (prices.length === 0) { 3 | return 0; 4 | } 5 | 6 | let minPrice = prices[0], 7 | maxProfit = 0; 8 | 9 | for (let i = 0; i < prices.length; i++) { 10 | if (prices[i] < minPrice) { 11 | minPrice = prices[i]; 12 | } else if (prices[i] - minPrice > maxProfit) { 13 | maxProfit = prices[i] - minPrice; 14 | } 15 | } 16 | 17 | return maxProfit; 18 | }; 19 | -------------------------------------------------------------------------------- /0122.BestTimetoBuyandSellStockII.js: -------------------------------------------------------------------------------- 1 | var maxProfit = function (prices) { 2 | if (prices.length === 0) { 3 | return 0; 4 | } 5 | let profit = 0, 6 | valley = prices[0], 7 | peak = prices[0]; 8 | let i = 0; 9 | while (i < prices.length - 1) { 10 | while (i < prices.length - 1 && prices[i] >= prices[i + 1]) { 11 | i++; 12 | } 13 | valley = prices[i]; 14 | while (i < prices.length - 1 && prices[i] <= prices[i + 1]) { 15 | i++; 16 | } 17 | peak = prices[i]; 18 | profit += peak - valley; 19 | } 20 | 21 | return profit; 22 | }; 23 | -------------------------------------------------------------------------------- /0123.BestTimetoBuyandSellStockIII.js: -------------------------------------------------------------------------------- 1 | var maxProfit = function (prices) { 2 | if (prices.length === 0) { 3 | return 0; 4 | } 5 | const dp = Array.from(Array(3), () => new Array(prices.length)); 6 | for (let i = 0; i < prices.length; i++) { 7 | dp[0][i] = 0; 8 | } 9 | for (let i = 0; i < 3; i++) { 10 | dp[i][0] = 0; 11 | } 12 | 13 | for (let i = 1; i < 3; i++) { 14 | let maxProfit = -prices[0]; 15 | for (let j = 1; j < prices.length; j++) { 16 | dp[i][j] = Math.max(dp[i][j - 1], prices[j] + maxProfit); 17 | maxProfit = Math.max(maxProfit, dp[i - 1][j] - prices[j]); 18 | } 19 | } 20 | 21 | return dp[2][prices.length - 1]; 22 | }; 23 | -------------------------------------------------------------------------------- /0125.ValidPalindrome.js: -------------------------------------------------------------------------------- 1 | var isPalindrome = function (s) { 2 | s = s.toLowerCase().replace(/[\W_]/g, ""); 3 | 4 | if (s.length < 2) { 5 | return true; 6 | } 7 | 8 | let left = 0; 9 | let right = s.length - 1; 10 | 11 | while (left < right) { 12 | if (s[left] !== s[right]) { 13 | return false; 14 | } 15 | left++; 16 | right--; 17 | } 18 | 19 | return true; 20 | }; 21 | -------------------------------------------------------------------------------- /0134.GasStation.js: -------------------------------------------------------------------------------- 1 | var canCompleteCircuit = function (gas, cost) { 2 | let totalGas = 0; 3 | let currentGas = 0; 4 | let start = 0; 5 | for (let i = 0; i < gas.length; i++) { 6 | totalGas = totalGas - cost[i] + gas[i]; 7 | } 8 | 9 | if (totalGas < 0) { 10 | return -1; 11 | } 12 | 13 | for (let i = 0; i < gas.length; i++) { 14 | currentGas = currentGas - cost[i] + gas[i]; 15 | if (currentGas < 0) { 16 | currentGas = 0; 17 | start = i + 1; 18 | } 19 | } 20 | 21 | return start; 22 | }; 23 | -------------------------------------------------------------------------------- /0141.LinkedListCycle.js: -------------------------------------------------------------------------------- 1 | var hasCycle = function (head) { 2 | if (head === null) { 3 | return false; 4 | } 5 | 6 | let slow = head; 7 | let fast = head; 8 | while (fast.next !== null && fast.next.next !== null) { 9 | slow = slow.next; 10 | fast = fast.next.next; 11 | if (slow === fast) { 12 | return true; 13 | } 14 | } 15 | 16 | return false; 17 | }; 18 | -------------------------------------------------------------------------------- /0142.LinkedListCycleII.js: -------------------------------------------------------------------------------- 1 | var detectCycle = function (head) { 2 | if (head === null) { 3 | return null; 4 | } 5 | 6 | let slow = head; 7 | let fast = head; 8 | let isCycle = false; 9 | while (fast.next !== null && fast.next.next !== null) { 10 | slow = slow.next; 11 | fast = fast.next.next; 12 | if (slow === fast) { 13 | isCycle = true; 14 | break; 15 | } 16 | } 17 | if (!isCycle) { 18 | return null; 19 | } 20 | slow = head; 21 | while (slow !== fast) { 22 | slow = slow.next; 23 | fast = fast.next; 24 | } 25 | 26 | return slow; 27 | }; 28 | -------------------------------------------------------------------------------- /0152.MaximumProductSubarray.js: -------------------------------------------------------------------------------- 1 | var maxProduct = function (nums) { 2 | const maxProductMemo = []; 3 | const minProductMemo = []; 4 | maxProductMemo[0] = nums[0]; 5 | minProductMemo[0] = nums[0]; 6 | let max = nums[0]; 7 | for (let i = 1; i < nums.length; i++) { 8 | maxProductMemo[i] = Math.max( 9 | nums[i], 10 | nums[i] * maxProductMemo[i - 1], 11 | nums[i] * minProductMemo[i - 1] 12 | ); 13 | minProductMemo[i] = Math.min( 14 | nums[i], 15 | nums[i] * maxProductMemo[i - 1], 16 | nums[i] * minProductMemo[i - 1] 17 | ); 18 | max = Math.max(max, maxProductMemo[i]); 19 | } 20 | return max; 21 | }; 22 | -------------------------------------------------------------------------------- /0153.FindMinimuminRotatedSortedArray.js: -------------------------------------------------------------------------------- 1 | var findMin = function (nums) { 2 | if (nums.length === 1) { 3 | return nums[0]; 4 | } 5 | 6 | let left = 0, 7 | right = nums.length - 1; 8 | 9 | if (nums[right] > nums[0]) { 10 | return nums[0]; 11 | } 12 | 13 | while (left < right) { 14 | let mid = Math.floor(left + (right - left) / 2); 15 | 16 | if (nums[mid] > nums[mid + 1]) { 17 | return nums[mid + 1]; 18 | } 19 | 20 | if (nums[mid - 1] > nums[mid]) { 21 | return nums[mid]; 22 | } 23 | 24 | if (nums[mid] > nums[left]) { 25 | left = mid + 1; 26 | } else { 27 | right = mid - 1; 28 | } 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /0160.IntersectionofTwoLinkedLists.js: -------------------------------------------------------------------------------- 1 | var getIntersectionNode = function (headA, headB) { 2 | let n1 = headA; 3 | let n2 = headB; 4 | while (n1 !== n2) { 5 | if (n1 === null) { 6 | n1 = headB; 7 | } else { 8 | n1 = n1.next; 9 | } 10 | if (n2 === null) { 11 | n2 = headA; 12 | } else { 13 | n2 = n2.next; 14 | } 15 | } 16 | return n1; 17 | }; 18 | -------------------------------------------------------------------------------- /0187.RepeatedDNASequences.js: -------------------------------------------------------------------------------- 1 | var findRepeatedDnaSequences = function (s) { 2 | const map = new Map(); 3 | const result = []; 4 | let i = 0; 5 | while (i + 10 <= s.length) { 6 | const dna = s.substring(i, i + 10); 7 | if (map.get(dna) === undefined) { 8 | map.set(dna, 1); 9 | } else if (map.get(dna) === 1) { 10 | result.push(dna); 11 | map.set(dna, 2); 12 | } else { 13 | map.set(dna, map.get(dna) + 1); 14 | } 15 | 16 | i++; 17 | } 18 | 19 | return result; 20 | }; 21 | -------------------------------------------------------------------------------- /0188.BestTimetoBuyandSellStockIV.js: -------------------------------------------------------------------------------- 1 | var maxProfit = function (k, prices) { 2 | if (prices.length === 0) { 3 | return 0; 4 | } 5 | const dp = Array.from(Array(k + 1), () => new Array(prices.length)); 6 | for (let i = 0; i < prices.length; i++) { 7 | dp[0][i] = 0; 8 | } 9 | for (let i = 0; i < k + 1; i++) { 10 | dp[i][0] = 0; 11 | } 12 | 13 | for (let i = 1; i < k + 1; i++) { 14 | let maxProfit = -prices[0]; 15 | for (let j = 1; j < prices.length; j++) { 16 | dp[i][j] = Math.max(dp[i][j - 1], prices[j] + maxProfit); 17 | maxProfit = Math.max(maxProfit, dp[i - 1][j] - prices[j]); 18 | } 19 | } 20 | 21 | return dp[k][prices.length - 1]; 22 | }; 23 | -------------------------------------------------------------------------------- /0198.HouseRobber.js: -------------------------------------------------------------------------------- 1 | var rob = function (nums) { 2 | if (nums.length === 0) { 3 | return 0; 4 | } 5 | if (nums.length === 1) { 6 | return nums[0]; 7 | } 8 | 9 | let result = 0; 10 | const memo = []; 11 | memo[0] = nums[0]; 12 | memo[1] = Math.max(nums[0], nums[1]); 13 | for (let i = 2; i < nums.length; i++) { 14 | memo[i] = Math.max(nums[i] + memo[i - 2], memo[i - 1]); 15 | } 16 | 17 | return memo[nums.length - 1]; 18 | }; 19 | -------------------------------------------------------------------------------- /0200.NumberofIslands.js: -------------------------------------------------------------------------------- 1 | var numIslands = function (grid) { 2 | let count = 0; 3 | 4 | for (let row = 0; row < grid.length; row++) { 5 | for (let col = 0; col < grid[0].length; col++) { 6 | if (grid[row][col] === "1") { 7 | count++; 8 | dfs(row, col); 9 | } 10 | } 11 | } 12 | 13 | function dfs(row, col) { 14 | if ( 15 | row < 0 || 16 | row >= grid.length || 17 | col < 0 || 18 | col >= grid[0].length || 19 | grid[row][col] === "0" 20 | ) { 21 | return; 22 | } 23 | 24 | grid[row][col] = "0"; 25 | dfs(row, col - 1); 26 | dfs(row, col + 1); 27 | dfs(row - 1, col); 28 | dfs(row + 1, col); 29 | } 30 | 31 | return count; 32 | }; 33 | -------------------------------------------------------------------------------- /0206.ReverseLinkedList.js: -------------------------------------------------------------------------------- 1 | var reverseList = function (head) { 2 | let prev = null; 3 | let curr = head; 4 | let next = null; 5 | while (curr !== null) { 6 | next = curr.next; 7 | curr.next = prev; 8 | prev = curr; 9 | curr = next; 10 | } 11 | return prev; 12 | }; 13 | -------------------------------------------------------------------------------- /0217.ContainsDuplicate.js: -------------------------------------------------------------------------------- 1 | var containsDuplicate = function (nums) { 2 | const set = new Set(); 3 | for (let i = 0; i < nums.length; i++) { 4 | if (set.has(nums[i])) { 5 | return true; 6 | } 7 | 8 | set.add(nums[i]); 9 | } 10 | 11 | return false; 12 | }; 13 | -------------------------------------------------------------------------------- /0219.ContainsDuplicateII.js: -------------------------------------------------------------------------------- 1 | var containsNearbyDuplicate = function (nums, k) { 2 | const map = new Map(); 3 | 4 | for (let i = 0; i < nums.length; i++) { 5 | if (map.has(nums[i]) && i - map.get(nums[i]) <= k) { 6 | return true; 7 | } else { 8 | map.set(nums[i], i); 9 | } 10 | } 11 | 12 | return false; 13 | }; 14 | -------------------------------------------------------------------------------- /0226.InvertBinaryTree.js: -------------------------------------------------------------------------------- 1 | var invertTree = function(root) { 2 | if(root === null){ 3 | return root; 4 | } 5 | 6 | let left = invertTree(root.left); 7 | let right = invertTree(root.right); 8 | 9 | root.left = right; 10 | root.right = left; 11 | 12 | return root; 13 | }; -------------------------------------------------------------------------------- /0235.LowestCommonAncestorofaBinarySearchTree.js: -------------------------------------------------------------------------------- 1 | var lowestCommonAncestor = function(root, p, q) { 2 | if(root.val < p.val && root.val < q.val){ 3 | return lowestCommonAncestor(root.right, p, q); 4 | }else if(root.val > p.val && root.val > q.val){ 5 | return lowestCommonAncestor(root.left, p, q); 6 | }else{ 7 | return root 8 | } 9 | }; -------------------------------------------------------------------------------- /0238.ProductofArrayExceptSelf.js: -------------------------------------------------------------------------------- 1 | var productExceptSelf = function (nums) { 2 | const result = Array(nums.length).fill(1); 3 | let product = 1; 4 | 5 | for (let i = 0; i < nums.length; i++) { 6 | result[i] = result[i] * product; 7 | product = product * nums[i]; 8 | } 9 | 10 | product = 1; 11 | 12 | for (let i = nums.length - 1; i >= 0; i--) { 13 | result[i] = result[i] * product; 14 | product = product * nums[i]; 15 | } 16 | 17 | return result; 18 | }; 19 | -------------------------------------------------------------------------------- /0242.ValidAnagram.js: -------------------------------------------------------------------------------- 1 | var isAnagram = function (s, t) { 2 | if (s.length !== t.length) { 3 | return false; 4 | } 5 | 6 | const map = new Map(); 7 | for (let i = 0; i < s.length; i++) { 8 | if (map.has(s[i])) { 9 | map.set(s[i], map.get(s[i]) + 1); 10 | } else { 11 | map.set(s[i], 1); 12 | } 13 | 14 | if (map.has(t[i])) { 15 | map.set(t[i], map.get(t[i]) - 1); 16 | } else { 17 | map.set(t[i], -1); 18 | } 19 | } 20 | 21 | for (const letter of map) { 22 | if (letter[1] !== 0) { 23 | return false; 24 | } 25 | } 26 | 27 | return true; 28 | }; 29 | -------------------------------------------------------------------------------- /0283.MoveZeroes.js: -------------------------------------------------------------------------------- 1 | var moveZeroes = function (nums) { 2 | let nonZeroIndex = 0; 3 | 4 | for (let i = 0; i < nums.length; i++) { 5 | if (nums[i] !== 0) { 6 | nums[nonZeroIndex] = nums[i]; 7 | nonZeroIndex++; 8 | } 9 | } 10 | 11 | for (i = nonZeroIndex; i < nums.length; i++) { 12 | nums[i] = 0; 13 | } 14 | 15 | return nums; 16 | }; 17 | -------------------------------------------------------------------------------- /0328.OddEvenLinkedList.js: -------------------------------------------------------------------------------- 1 | var oddEvenList = function (head) { 2 | if (head === null) { 3 | return null; 4 | } 5 | if (head.next === null) { 6 | return head; 7 | } 8 | 9 | let odd = head; 10 | let even = head.next; 11 | let evenHead = head.next; 12 | while (even !== null && even.next !== null) { 13 | odd.next = odd.next.next; 14 | odd = odd.next; 15 | even.next = even.next.next; 16 | even = even.next; 17 | } 18 | 19 | odd.next = evenHead; 20 | 21 | return head; 22 | }; 23 | -------------------------------------------------------------------------------- /0349.IntersectionofTwoArrays.js: -------------------------------------------------------------------------------- 1 | var intersection = function (nums1, nums2) { 2 | const set = new Set(); 3 | 4 | const result = new Set(); 5 | 6 | for (num of nums1) { 7 | set.add(num); 8 | } 9 | 10 | for (num of nums2) { 11 | if (set.has(num)) { 12 | result.add(num); 13 | } 14 | } 15 | 16 | return Array.from(result); 17 | }; 18 | -------------------------------------------------------------------------------- /0404.SumofLeftLeaves.js: -------------------------------------------------------------------------------- 1 | var sumOfLeftLeaves = function(root) { 2 | let result = 0; 3 | 4 | function findLeftLeaf(node){ 5 | if(node === null){ 6 | return; 7 | } 8 | 9 | if(node.left !== null && node.left.left === null && node.left.right === null){ 10 | result += node.left.val; 11 | } 12 | 13 | findLeftLeaf(node.left); 14 | findLeftLeaf(node.right); 15 | } 16 | 17 | findLeftLeaf(root); 18 | return result; 19 | 20 | }; -------------------------------------------------------------------------------- /0419.BattleshipsinaBoard.js: -------------------------------------------------------------------------------- 1 | var countBattleships = function (board) { 2 | let result = 0; 3 | for (let row = 0; row < board.length; row++) { 4 | for (let col = 0; col < board[0].length; col++) { 5 | if (board[row][col] === "X") { 6 | result++; 7 | dfs(row, col); 8 | } 9 | } 10 | } 11 | 12 | function dfs(row, col) { 13 | if ( 14 | row < 0 || 15 | row >= board.length || 16 | col < 0 || 17 | col >= board[0].length || 18 | board[row][col] !== "X" 19 | ) { 20 | return; 21 | } 22 | board[row][col] = "."; 23 | dfs(row - 1, col); 24 | dfs(row + 1, col); 25 | dfs(row, col - 1); 26 | dfs(row, col + 1); 27 | } 28 | 29 | return result; 30 | }; 31 | -------------------------------------------------------------------------------- /0445.AddTwoNumbersII.js: -------------------------------------------------------------------------------- 1 | var addTwoNumbers = function (l1, l2) { 2 | const stack1 = [], 3 | stack2 = []; 4 | while (l1 !== null) { 5 | stack1.push(l1.val); 6 | l1 = l1.next; 7 | } 8 | while (l2 !== null) { 9 | stack2.push(l2.val); 10 | l2 = l2.next; 11 | } 12 | 13 | let curr = null, 14 | carry = 0; 15 | 16 | while (stack1.length !== 0 || stack2.length !== 0) { 17 | let sum = 0; 18 | if (stack1.length !== 0) { 19 | sum += stack1.pop(); 20 | } 21 | if (stack2.length !== 0) { 22 | sum += stack2.pop(); 23 | } 24 | sum += carry; 25 | 26 | const node = new ListNode(sum % 10); 27 | carry = Math.floor(sum / 10); 28 | 29 | node.next = curr; 30 | curr = node; 31 | } 32 | 33 | if (carry !== 0) { 34 | const node = new ListNode(carry); 35 | node.next = curr; 36 | curr = node; 37 | } 38 | 39 | return curr; 40 | }; 41 | -------------------------------------------------------------------------------- /0509.FibonacciNumber.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jslaobi/jslaobi-leetcode-js/5f913873dc653ada82ac0e5f7cb3508a9d5a42b5/0509.FibonacciNumber.js -------------------------------------------------------------------------------- /0680.ValidPalindromeII.js: -------------------------------------------------------------------------------- 1 | var validPalindrome = function (s) { 2 | function isPalindrome(left, right) { 3 | while (left < right) { 4 | if (s[left] !== s[right]) { 5 | return false; 6 | } 7 | left++; 8 | right--; 9 | } 10 | return true; 11 | } 12 | 13 | let left = 0, 14 | right = s.length - 1; 15 | while (left < right) { 16 | if (s[left] !== s[right]) { 17 | const result = 18 | isPalindrome(left + 1, right) || isPalindrome(left, right - 1); 19 | return result; 20 | } 21 | left++; 22 | right--; 23 | } 24 | 25 | return true; 26 | }; 27 | -------------------------------------------------------------------------------- /0695.MaxAreaofIsland.js: -------------------------------------------------------------------------------- 1 | var maxAreaOfIsland = function(grid) { 2 | let max = 0; 3 | for(let row=0;row=grid.length || col<0 || col>=grid[0].length || grid[row][col] === 0){ 15 | return 0; 16 | } 17 | 18 | grid[row][col] = 0; 19 | let count = 1; 20 | count += dfs(row-1,col,count); 21 | count += dfs(row+1,col,count); 22 | count += dfs(row,col-1,count); 23 | count += dfs(row,col+1,count); 24 | return count; 25 | } 26 | 27 | return max; 28 | }; -------------------------------------------------------------------------------- /0704.BinarySearch.js: -------------------------------------------------------------------------------- 1 | var search = function (nums, target) { 2 | let left = 0, 3 | right = nums.length - 1; 4 | while (left <= right) { 5 | mid = Math.floor(left + (right - left) / 2); 6 | if (nums[mid] === target) { 7 | return mid; 8 | } else if (target < nums[mid]) { 9 | right = mid - 1; 10 | } else { 11 | left = mid + 1; 12 | } 13 | } 14 | 15 | return -1; 16 | }; 17 | -------------------------------------------------------------------------------- /0733.FloodFill.js: -------------------------------------------------------------------------------- 1 | var floodFill = function (image, sr, sc, newColor) { 2 | if (image[sr][sc] === newColor) { 3 | return image; 4 | } 5 | const oldColor = image[sr][sc]; 6 | 7 | function dfs(sr, sc) { 8 | if ( 9 | sr < 0 || 10 | sr >= image.length || 11 | sc < 0 || 12 | sc >= image[0].length || 13 | image[sr][sc] !== oldColor 14 | ) { 15 | return; 16 | } 17 | image[sr][sc] = newColor; 18 | dfs(sr - 1, sc); 19 | dfs(sr + 1, sc); 20 | dfs(sr, sc - 1); 21 | dfs(sr, sc + 1); 22 | } 23 | 24 | dfs(sr, sc); 25 | return image; 26 | }; 27 | -------------------------------------------------------------------------------- /0796.RotateString.js: -------------------------------------------------------------------------------- 1 | var rotateString = function (A, B) { 2 | if (A.length !== B.length) { 3 | return false; 4 | } 5 | 6 | const str = A + A; 7 | 8 | // indexOf 9 | return str.includes(B); 10 | }; 11 | -------------------------------------------------------------------------------- /0836.RectangleOverlap.js: -------------------------------------------------------------------------------- 1 | var isRectangleOverlap = function (rec1, rec2) { 2 | if ( 3 | rec1[2] <= rec2[0] || 4 | rec1[1] >= rec2[3] || 5 | rec1[0] >= rec2[2] || 6 | rec1[3] <= rec2[1] 7 | ) { 8 | return false; 9 | } else { 10 | return true; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /0844.BackspaceStringCompare.js: -------------------------------------------------------------------------------- 1 | var backspaceCompare = function (S, T) { 2 | let i = S.length - 1, 3 | j = T.length - 1; 4 | let backspaceS = 0, 5 | backspaceT = 0; 6 | 7 | while (i >= 0 || j >= 0) { 8 | while (i >= 0) { 9 | if (S[i] === "#") { 10 | backspaceS++; 11 | i--; 12 | } else if (backspaceS > 0) { 13 | backspaceS--; 14 | i--; 15 | } else { 16 | break; 17 | } 18 | } 19 | 20 | while (j >= 0) { 21 | if (T[j] === "#") { 22 | backspaceT++; 23 | j--; 24 | } else if (backspaceT > 0) { 25 | backspaceT--; 26 | j--; 27 | } else { 28 | break; 29 | } 30 | } 31 | 32 | if (S[i] !== T[j]) { 33 | return false; 34 | } 35 | if ((i < 0 && j > 0) || (i > 0 && j < 0)) { 36 | return false; 37 | } 38 | i--; 39 | j--; 40 | } 41 | 42 | return true; 43 | }; 44 | -------------------------------------------------------------------------------- /0876.MiddleoftheLinkedList.js: -------------------------------------------------------------------------------- 1 | var middleNode = function (head) { 2 | let slow = head; 3 | let fast = head; 4 | while (fast !== null && fast.next !== null) { 5 | slow = slow.next; 6 | fast = fast.next.next; 7 | } 8 | 9 | return slow; 10 | }; 11 | -------------------------------------------------------------------------------- /0904.FruitIntoBaskets.js: -------------------------------------------------------------------------------- 1 | var totalFruit = function (tree) { 2 | let max = 1; 3 | const map = new Map(); 4 | let j = 0; 5 | 6 | for (let i = 0; i < tree.length; i++) { 7 | map.set(tree[i], i); 8 | if (map.size > 2) { 9 | let minIndex = tree.length - 1; 10 | for (const fruit of map) { 11 | if (fruit[1] < minIndex) { 12 | minIndex = fruit[1]; 13 | } 14 | } 15 | 16 | map.delete(tree[minIndex]); 17 | j = minIndex + 1; 18 | } 19 | 20 | max = Math.max(max, i - j + 1); 21 | } 22 | 23 | return max; 24 | }; 25 | -------------------------------------------------------------------------------- /0905.SortArrayByParity.js: -------------------------------------------------------------------------------- 1 | var sortArrayByParity = function (A) { 2 | let i = 0, 3 | j = A.length - 1; 4 | while (i < j) { 5 | if (A[i] % 2 === 1 && A[j] % 2 === 0) { 6 | [A[i], A[j]] = [A[j], A[i]]; 7 | } 8 | 9 | if (A[i] % 2 === 0) { 10 | i++; 11 | } 12 | if (A[j] % 2 === 1) { 13 | j--; 14 | } 15 | } 16 | 17 | return A; 18 | }; 19 | -------------------------------------------------------------------------------- /0922.SortArrayByParityII.js: -------------------------------------------------------------------------------- 1 | var sortArrayByParityII = function (A) { 2 | let j = 1; 3 | for (let i = 0; i < A.length; i += 2) { 4 | if (A[i] % 2 === 1) { 5 | while (A[j] % 2 === 1 && j < A.length) { 6 | j += 2; 7 | } 8 | 9 | [A[i], A[j]] = [A[j], A[i]]; 10 | } 11 | } 12 | 13 | return A; 14 | }; 15 | --------------------------------------------------------------------------------