Permalink
Please
sign in to comment.
Showing
with
829 additions
and 12 deletions.
- +61 −0 152.maximum-product-subarray.js
- +64 −0 189.rotate-array.js
- +55 −0 217.contains-duplicate.js
- +85 −0 23.merge-k-sorted-lists.js
- +72 −0 236.lowest-common-ancestor-of-a-binary-tree.js
- +56 −0 238.product-of-array-except-self.js
- +57 −0 326.power-of-three.js
- +65 −0 334.increasing-triplet-subsequence.js
- +55 −0 344.reverse-string.js
- +67 −0 350.intersection-of-two-arrays-ii.js
- +43 −0 387.first-unique-character-in-a-string.js
- +5 −5 README.md
- +15 −3 problems/150.evaluate-reverse-polish-notation.md
- +11 −1 problems/20.validParentheses.md
- +2 −2 problems/279.perfect-squares.md
- +69 −1 thinkings/basic-data-structure.md
- +5 −0 thinkings/binary-tree-traversal.md
- +42 −0 todo/candidates/324.wiggle-sort-ii.js
| @@ -0,0 +1,61 @@ | |||
| /* | |||
| * @lc app=leetcode id=152 lang=javascript | |||
| * | |||
| * [152] Maximum Product Subarray | |||
| * | |||
| * https://leetcode.com/problems/maximum-product-subarray/description/ | |||
| * | |||
| * algorithms | |||
| * Medium (28.61%) | |||
| * Total Accepted: 202.8K | |||
| * Total Submissions: 700K | |||
| * Testcase Example: '[2,3,-2,4]' | |||
| * | |||
| * Given an integer array nums, find the contiguous subarray within an array | |||
| * (containing at least one number) which has the largest product. | |||
| * | |||
| * Example 1: | |||
| * | |||
| * | |||
| * Input: [2,3,-2,4] | |||
| * Output: 6 | |||
| * Explanation: [2,3] has the largest product 6. | |||
| * | |||
| * | |||
| * Example 2: | |||
| * | |||
| * | |||
| * Input: [-2,0,-1] | |||
| * Output: 0 | |||
| * Explanation: The result cannot be 2, because [-2,-1] is not a subarray. | |||
| * | |||
| */ | |||
| /** | |||
| * @param {number[]} nums | |||
| * @return {number} | |||
| */ | |||
| var maxProduct = function(nums) { | |||
| // let max = nums[0]; | |||
| // let temp = null; | |||
| // for(let i = 0; i < nums.length; i++) { | |||
| // temp = nums[i]; | |||
| // max = Math.max(temp, max); | |||
| // for(let j = i + 1; j < nums.length; j++) { | |||
| // temp *= nums[j]; | |||
| // max = Math.max(temp, max); | |||
| // } | |||
| // } | |||
|
|
|||
| // return max; | |||
| let max = nums[0]; | |||
| let min = nums[0]; | |||
| let res = nums[0]; | |||
|
|
|||
| for (let i = 1; i < nums.length; i++) { | |||
| let tmp = min; | |||
| min = Math.min(nums[i], Math.min(max * nums[i], min * nums[i])); // 取最小 | |||
| max = Math.max(nums[i], Math.max(max * nums[i], tmp * nums[i])); /// 取最大 | |||
| res = Math.max(res, max); | |||
| } | |||
| return res; | |||
| }; | |||
| @@ -0,0 +1,64 @@ | |||
| /* | |||
| * @lc app=leetcode id=189 lang=javascript | |||
| * | |||
| * [189] Rotate Array | |||
| * | |||
| * https://leetcode.com/problems/rotate-array/description/ | |||
| * | |||
| * algorithms | |||
| * Easy (29.07%) | |||
| * Total Accepted: 287.3K | |||
| * Total Submissions: 966.9K | |||
| * Testcase Example: '[1,2,3,4,5,6,7]\n3' | |||
| * | |||
| * Given an array, rotate the array to the right by k steps, where k is | |||
| * non-negative. | |||
| * | |||
| * Example 1: | |||
| * | |||
| * | |||
| * Input: [1,2,3,4,5,6,7] and k = 3 | |||
| * Output: [5,6,7,1,2,3,4] | |||
| * Explanation: | |||
| * rotate 1 steps to the right: [7,1,2,3,4,5,6] | |||
| * rotate 2 steps to the right: [6,7,1,2,3,4,5] | |||
| * rotate 3 steps to the right: [5,6,7,1,2,3,4] | |||
| * | |||
| * | |||
| * Example 2: | |||
| * | |||
| * | |||
| * Input: [-1,-100,3,99] and k = 2 | |||
| * Output: [3,99,-1,-100] | |||
| * Explanation: | |||
| * rotate 1 steps to the right: [99,-1,-100,3] | |||
| * rotate 2 steps to the right: [3,99,-1,-100] | |||
| * | |||
| * | |||
| * Note: | |||
| * | |||
| * | |||
| * Try to come up as many solutions as you can, there are at least 3 different | |||
| * ways to solve this problem. | |||
| * Could you do it in-place with O(1) extra space? | |||
| * | |||
| */ | |||
| /** | |||
| * @param {number[]} nums | |||
| * @param {number} k | |||
| * @return {void} Do not return anything, modify nums in-place instead. | |||
| */ | |||
| var rotate = function(nums, k) { | |||
| // 就像扩容一样操作 | |||
| k = k % nums.length; | |||
| const n = nums.length; | |||
|
|
|||
| for (let i = nums.length - 1; i >= 0; i--) { | |||
| nums[i + k] = nums[i]; | |||
| } | |||
|
|
|||
| for (let i = 0; i < k; i++) { | |||
| nums[i] = nums[n + i]; | |||
| } | |||
| nums.length = n; | |||
| }; | |||
| @@ -0,0 +1,55 @@ | |||
| /* | |||
| * @lc app=leetcode id=217 lang=javascript | |||
| * | |||
| * [217] Contains Duplicate | |||
| * | |||
| * https://leetcode.com/problems/contains-duplicate/description/ | |||
| * | |||
| * algorithms | |||
| * Easy (50.92%) | |||
| * Total Accepted: 324K | |||
| * Total Submissions: 628.5K | |||
| * Testcase Example: '[1,2,3,1]' | |||
| * | |||
| * Given an array of integers, find if the array contains any duplicates. | |||
| * | |||
| * Your function should return true if any value appears at least twice in the | |||
| * array, and it should return false if every element is distinct. | |||
| * | |||
| * Example 1: | |||
| * | |||
| * | |||
| * Input: [1,2,3,1] | |||
| * Output: true | |||
| * | |||
| * Example 2: | |||
| * | |||
| * | |||
| * Input: [1,2,3,4] | |||
| * Output: false | |||
| * | |||
| * Example 3: | |||
| * | |||
| * | |||
| * Input: [1,1,1,3,3,4,3,2,4,2] | |||
| * Output: true | |||
| * | |||
| */ | |||
| /** | |||
| * @param {number[]} nums | |||
| * @return {boolean} | |||
| */ | |||
| var containsDuplicate = function(nums) { | |||
| // 1. 暴力两层循环两两比较, 时间复杂度O(n^2) 空间复杂度O(1) | |||
|
|
|||
| // 2. 先排序,之后比较前后元素是否一致即可,一层循环即可,如果排序使用的比较排序的话时间复杂度O(nlogn) 空间复杂度O(1) | |||
|
|
|||
| // 3. 用hashmap ,时间复杂度O(n) 空间复杂度O(n) | |||
| const visited = {}; | |||
| for(let i = 0; i < nums.length; i++) { | |||
| if (visited[nums[i]]) return true; | |||
| visited[nums[i]] = true; | |||
| } | |||
| return false; | |||
| }; | |||
|
|
|||
| @@ -0,0 +1,85 @@ | |||
| /* | |||
| * @lc app=leetcode id=23 lang=javascript | |||
| * | |||
| * [23] Merge k Sorted Lists | |||
| * | |||
| * https://leetcode.com/problems/merge-k-sorted-lists/description/ | |||
| * | |||
| * algorithms | |||
| * Hard (33.14%) | |||
| * Total Accepted: 373.7K | |||
| * Total Submissions: 1.1M | |||
| * Testcase Example: '[[1,4,5],[1,3,4],[2,6]]' | |||
| * | |||
| * Merge k sorted linked lists and return it as one sorted list. Analyze and | |||
| * describe its complexity. | |||
| * | |||
| * Example: | |||
| * | |||
| * | |||
| * Input: | |||
| * [ | |||
| * 1->4->5, | |||
| * 1->3->4, | |||
| * 2->6 | |||
| * ] | |||
| * Output: 1->1->2->3->4->4->5->6 | |||
| * | |||
| * | |||
| */ | |||
| function mergeTwoLists(l1, l2) { | |||
| const dummyHead = {}; | |||
| let current = dummyHead; | |||
| // 1 -> 3 -> 5 | |||
| // 2 -> 4 -> 6 | |||
| while (l1 !== null && l2 !== null) { | |||
| if (l1.val < l2.val) { | |||
| current.next = l1; // 把小的添加到结果链表 | |||
| current = current.next; // 移动结果链表的指针 | |||
| l1 = l1.next; // 移动小的那个链表的指针 | |||
| } else { | |||
| current.next = l2; | |||
| current = current.next; | |||
| l2 = l2.next; | |||
| } | |||
| } | |||
|
|
|||
| if (l1 === null) { | |||
| current.next = l2; | |||
| } else { | |||
| current.next = l1; | |||
| } | |||
| return dummyHead.next; | |||
| } | |||
| /** | |||
| * Definition for singly-linked list. | |||
| * function ListNode(val) { | |||
| * this.val = val; | |||
| * this.next = null; | |||
| * } | |||
| */ | |||
| /** | |||
| * @param {ListNode[]} lists | |||
| * @return {ListNode} | |||
| */ | |||
| var mergeKLists = function(lists) { | |||
| // 图参考: https://zhuanlan.zhihu.com/p/61796021 | |||
| if (lists.length === 0) return null; | |||
| if (lists.length === 1) return lists[0]; | |||
| if (lists.length === 2) { | |||
| return mergeTwoLists(lists[0], lists[1]); | |||
| } | |||
|
|
|||
| const mid = lists.length >> 1; | |||
| const l1 = []; | |||
| for (let i = 0; i < mid; i++) { | |||
| l1[i] = lists[i]; | |||
| } | |||
|
|
|||
| const l2 = []; | |||
| for (let i = mid, j = 0; i < lists.length; i++, j++) { | |||
| l2[j] = lists[i]; | |||
| } | |||
|
|
|||
| return mergeTwoLists(mergeKLists(l1), mergeKLists(l2)); | |||
| }; | |||
| @@ -0,0 +1,72 @@ | |||
| /* | |||
| * @lc app=leetcode id=236 lang=javascript | |||
| * | |||
| * [236] Lowest Common Ancestor of a Binary Tree | |||
| * | |||
| * https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/ | |||
| * | |||
| * algorithms | |||
| * Medium (35.63%) | |||
| * Total Accepted: 267.3K | |||
| * Total Submissions: 729.2K | |||
| * Testcase Example: '[3,5,1,6,2,0,8,null,null,7,4]\n5\n1' | |||
| * | |||
| * Given a binary tree, find the lowest common ancestor (LCA) of two given | |||
| * nodes in the tree. | |||
| * | |||
| * According to the definition of LCA on Wikipedia: “The lowest common ancestor | |||
| * is defined between two nodes p and q as the lowest node in T that has both p | |||
| * and q as descendants (where we allow a node to be a descendant of itself).” | |||
| * | |||
| * Given the following binary tree: root = [3,5,1,6,2,0,8,null,null,7,4] | |||
| * | |||
| * | |||
| * | |||
| * Example 1: | |||
| * | |||
| * | |||
| * Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 | |||
| * Output: 3 | |||
| * Explanation: The LCA of nodes 5 and 1 is 3. | |||
| * | |||
| * | |||
| * Example 2: | |||
| * | |||
| * | |||
| * Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4 | |||
| * Output: 5 | |||
| * Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant | |||
| * of itself according to the LCA definition. | |||
| * | |||
| * | |||
| * | |||
| * | |||
| * Note: | |||
| * | |||
| * | |||
| * All of the nodes' values will be unique. | |||
| * p and q are different and both values will exist in the binary tree. | |||
| * | |||
| * | |||
| */ | |||
| /** | |||
| * Definition for a binary tree node. | |||
| * function TreeNode(val) { | |||
| * this.val = val; | |||
| * this.left = this.right = null; | |||
| * } | |||
| */ | |||
| /** | |||
| * @param {TreeNode} root | |||
| * @param {TreeNode} p | |||
| * @param {TreeNode} q | |||
| * @return {TreeNode} | |||
| */ | |||
| var lowestCommonAncestor = function(root, p, q) { | |||
| if (!root || root === p || root === q) return root; | |||
| const left = lowestCommonAncestor(root.left, p, q); | |||
| const right = lowestCommonAncestor(root.right, p, q); | |||
| if (!left) return right; // 左子树找不到,返回右子树 | |||
| if (!right) return left; // 右子树找不到,返回左子树 | |||
| return root; // 左右子树分别有一个,则返回root | |||
| }; | |||
| @@ -0,0 +1,56 @@ | |||
| /* | |||
| * @lc app=leetcode id=238 lang=javascript | |||
| * | |||
| * [238] Product of Array Except Self | |||
| * | |||
| * https://leetcode.com/problems/product-of-array-except-self/description/ | |||
| * | |||
| * algorithms | |||
| * Medium (53.97%) | |||
| * Total Accepted: 246.5K | |||
| * Total Submissions: 451.4K | |||
| * Testcase Example: '[1,2,3,4]' | |||
| * | |||
| * Given an array nums of n integers where n > 1, return an array output such | |||
| * that output[i] is equal to the product of all the elements of nums except | |||
| * nums[i]. | |||
| * | |||
| * Example: | |||
| * | |||
| * | |||
| * Input: [1,2,3,4] | |||
| * Output: [24,12,8,6] | |||
| * | |||
| * | |||
| * Note: Please solve it without division and in O(n). | |||
| * | |||
| * Follow up: | |||
| * Could you solve it with constant space complexity? (The output array does | |||
| * not count as extra space for the purpose of space complexity analysis.) | |||
| * | |||
| */ | |||
| /** | |||
| * @param {number[]} nums | |||
| * @return {number[]} | |||
| */ | |||
| var productExceptSelf = function(nums) { | |||
| const ret = []; | |||
|
|
|||
| for (let i = 0, temp = 1; i < nums.length; i++) { | |||
| ret[i] = temp; | |||
| temp *= nums[i]; | |||
| } | |||
| // 此时ret[i]存放的是前i个元素相乘的结果(不包含第i个) | |||
|
|
|||
| // 如果没有上面的循环的话, | |||
| // ret经过下面的循环会变成ret[i]存放的是后i个元素相乘的结果(不包含第i个) | |||
|
|
|||
| // 我们的目标是ret[i]存放的所有数字相乘的结果(不包含第i个) | |||
|
|
|||
| // 因此我们只需要对于上述的循环产生的ret[i]基础上运算即可 | |||
| for (let i = nums.length - 1, temp = 1; i >= 0; i--) { | |||
| ret[i] *= temp; | |||
| temp *= nums[i]; | |||
| } | |||
| return ret; | |||
| }; | |||
Oops, something went wrong.
0 comments on commit
1b5dc5f