Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
luzhipeng committed May 4, 2019
1 parent edf7916 commit a86931f
Show file tree
Hide file tree
Showing 25 changed files with 1,162 additions and 8 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,17 @@ leetcode 题解,记录自己的 leetcode 解题之路。
#### 简单难度

- [0020.Valid Parentheses](./problems/20.validParentheses.md) 🖊
- [0020.Valid Parentheses](./problems/20.validParentheses.md)
- [0026.remove-duplicates-from-sorted-array](./problems/26.remove-duplicates-from-sorted-array.md)
- [0088.merge-sorted-array](./problems/88.merge-sorted-array.md)
- [0121.best-time-to-buy-and-sell-stock](./problems/121.best-time-to-buy-and-sell-stock.md) 🆕
- [0122.best-time-to-buy-and-sell-stock-ii](./problems/122.best-time-to-buy-and-sell-stock-ii.md) 🆕
- [0136.single-number](./problems/136.single-number.md)
- [0167.two-sum-ii-input-array-is-sorted](./problems/167.two-sum-ii-input-array-is-sorted.md)
- [0169.majority-element](./problems/169.majority-element.md)
- [0190.reverse-bits](./problems/190.reverse-bits.md)
- [0191.number-of-1-bits](./problems/191.number-of-1-bits.md)
- [0198.house-robber.md](./problems/198.house-robber.md) 🆕
- [0203.remove-linked-list-elements](./problems/203.remove-linked-list-elements.md)
- [0206.reverse-linked-list](./problems/206.reverse-linked-list.md)
- [0219.contains-duplicate-ii](./problems/219.contains-duplicate-ii.md)
Expand All @@ -115,7 +118,8 @@ leetcode 题解,记录自己的 leetcode 解题之路。
- [0075.sort-colors](./problems/75.sort-colors.md)
- [0078.subsets](./problems/78.subsets.md) 🆕
- [0086.partition-list](./problems/86.partition-list.md)
- [0090.subsets-ii](./problems/90.subsets-ii.md) 🆕
- [0090.subsets-ii](./problems/90.subsets-ii.md)
- [0091.decode-ways](./problems/91.decode-ways.md) 🆕
- [0092.reverse-linked-list-ii](./problems/92.reverse-linked-list-ii.md)
- [0094.binary-tree-inorder-traversal](./problems/94.binary-tree-inorder-traversal.md)
- [0102.binary-tree-level-order-traversal](./problems/102.binary-tree-level-order-traversal.md)
Expand All @@ -131,13 +135,15 @@ leetcode 题解,记录自己的 leetcode 解题之路。
- [0236.lowest-common-ancestor-of-a-binary-tree](./problems/236.lowest-common-ancestor-of-a-binary-tree.md)🆕
- [0238.product-of-array-except-self](./problems/238.product-of-array-except-self.md) 🆕
- [0240.search-a-2-d-matrix-ii](./problems/240.search-a-2-d-matrix-ii.md)
- [0279.perfect-squares](./problems/279.perfect-squares.md)🖊
- [0279.perfect-squares](./problems/279.perfect-squares.md)
- [0309.best-time-to-buy-and-sell-stock-with-cooldown](./problems/309.best-time-to-buy-and-sell-stock-with-cooldown.md) 🆕
- [0322.coin-change](./problems/322.coin-change.md)
- [0334.increasing-triplet-subsequence](./problems/334.increasing-triplet-subsequence.md)
- [0328.odd-even-linked-list](./problems/328.odd-even-linked-list.md)
- [0416.partition-equal-subset-sum](./problems/416.partition-equal-subset-sum.md)
- [0445.add-two-numbers-ii](./problems/445.add-two-numbers-ii.md)
- [0454.4-sum-ii](./problems/454.4-sum-ii.md) 🆕
- [0494.target-sum](./problems/494.target-sum.md) 🆕
- [0518.coin-change-2](./problems/518.coin-change-2.md)
- [0875.koko-eating-bananas](./problems/875.koko-eating-bananas.md)
- [0877.stone-game](./problems/877.stone-game.md)
Expand Down
1 change: 1 addition & 0 deletions assets/drawio/121.best-time-to-buy-and-sell-stock.drawio
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<mxfile modified="2019-05-04T06:00:45.801Z" host="www.draw.io" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" etag="8lZpRP_onmLUlqddJt9q" version="10.6.6" type="device"><diagram id="YQNoRghu4fKXr0qcQk76" name="第 1 页">7ZjbjtsgEIafxpcr2cZ2ksvETbattlXVrNRrZIiNgk2KyalPX1iDT7SNo60S7cq5iOAHBphvxhrbAXF+euRwl31hCFPHd9HJAR8c3595U/mvhHMlBJFXCSknqJJawpr8wlp0tbonCJediYIxKsiuKyasKHAiOhrknB270zaMdnfdwRRbwjqB1FZ/ECSySp36k0b/iEmamZ29aFaN5NBM1jcpM4jYsSWBpQNizpioWvkpxlT5zvilWrf6y2h9MI4LMWTB82b76bMbfv+JHt3ZE5w+oYevD9rKAdK9vrA+rDgbD+ACzZUjZa9ghRQXmcip7HmyWc3GyHJicyqvvquMEcxyLPhZTjk23gy1h7KWI43GMYWCHLrmoYaa1ubqHb4xIjf2XR1/XqTt6PDzDA1jomR7nmC9qu29nqH6QNoQiHqGBOQpFpYh2Whdu5Fe4FwByn8lqFJwtsUxo4y/zAarVeT6UT1igjt4A1AvshgKFXhdQ757W6jgnWffRf8OBjXtGZreFlQwZt9wFkOhBkHv2XzjR2r4zrPvon+Hggr7aRzcFlRkgZpYpAQ+iX/lnMa3IZT2JEhJWshuIqlhqS8OmAsiK8C5HsgJQmqbxTEjAq93MFF7HmW5KzXO9gXC6vSuMs8KoWtY3/RbWe/KXxzXoaM2wqdrg8csAF0oJgtbsRX8Ibb69U87jDrcroXk2aWkN1Lye5TA5N6Y7EIyHDH5s96j8u6Y7NIQjJhA722uxnY3THZhGI2YAhO9JpvAvTHZpV4wYgp79WHt/7thsgs9x4+oooLIQTZT8XLzSip3sKi1cOHJ24ZyibvApapdn0mOHfWxUEl7dW1YIPm/rg6yFizZGlPytB1rjdzZ9y0GzGQ5j5bufyo5J92ACTw7YOoC55URI7vNp9HqRaL5vgyWvwE=</diagram></mxfile>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<mxfile modified="2019-05-04T06:06:00.266Z" host="www.draw.io" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" etag="emzrtnCmL0sRbl5TDGoR" version="10.6.6" type="device"><diagram id="SQUxga718U24Nse9CxR8" name="第 1 页">7ZhNj9sgEIZ/TY6VjLGd5BinSTeHSpVStVUvFTKzNlpsIkK++usLa7CNve1mtVWiXSUHB15ggHkGZ8IIz8vjJ0k2xWdBgY/CgB5H+OMoDKdoop9GONVClKBayCWjtdQR1uw3WDGw6o5R2HodlRBcsY0vZqKqIFOeRqQUB7/bveD+rBuSw0BYZ4QP1e+MqqJWJ+G41e+A5YWbGSXTuqUkrrPdybYgVBw6El6M8FwKoepSeZwDN75zfqnHLf/S2ixMQqXOGZCK5NdJTNEPsfqWrVLgh593H6yVPeE7u2G7WHVyHoCKzowjda0SlRbTQpVc15Au1r2BDpzYrgo1e9UxAqIEJU+6y6H1Zmw9VHQc6TQJnCi2980TCzVvzDUzfBFMTxwGNv5QYu3Y8EOOhjOxFTuZgR3V9V7PULMgawgnPUOKyBzUwJAudLbdSo9wXgAqfCWorZLiAeaCC/nYGy+XSRAmTYsL7ugNQH2WxblQMfINhcFloeJ3fvqe9e/ZoCY9Q5PLgopup+98FudCjaLeu/nCr9T4nZ++Z/17Lqi4f4yjy4JKBqDGA1IKjupfZ87iu2ec9yTCWV7paqapgdbTPUjFdAY4sw0lo9RMkx4KpmC9IZmZ86DTXa1JsasomNUHxryolM1hQ1fvnPpAf+bzJnTMRHB8afC4AdiH4k5hJ7aiJ2Krn/90w8jj9lJI4wEkdIMU9iDh8ZUpTQaU4hulcNp7UV6b0nRACd8o4d5fuYbatSi5X8UOpuSGKULYP0z42piGdxzRDVPcSw4b/18N01M3HAk3VCjb62KuHndeS9sNqRotTpHuHOshQQpbk7h+ZSWMzE2hkXZm26Si+rmuF7JWInvQ36uVs6eX7JlsZW/ytxg148UsWQT/Kekc+1EToWHUNDnOK8NGV9vL0fqvRHvDjBd/AA==</diagram></mxfile>
1 change: 1 addition & 0 deletions assets/drawio/198.house-robber.drawio
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<mxfile modified="2019-05-04T07:56:58.484Z" host="www.draw.io" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/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: 1 addition & 0 deletions assets/drawio/494.target-sum.drawio
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<mxfile modified="2019-05-04T08:50:46.975Z" host="www.draw.io" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" etag="EDJk86jsYYBlklMIT5Au" version="10.6.6" type="device"><diagram id="4IO8ltCWfnja1ZXnyxj4" name="第 1 页">7Vxrb6M4FP01kbYjNQLbQPKxeXRfM6NI1Wh3Pq0IOAlawCw4Tbq/fu3wtt1NJg0xmelIneILGDjncLjX4A7gNNr/nLrJ5hPxcTgAhr8fwNkAgLE5Yv/zwEseQLaZB9Zp4OehRuAp+BcXQaOIbgMfZ60NKSEhDZJ20CNxjD3airlpSnbtzVYkbB81cddYCjx5bihH/wh8usmjI+DU8V9wsN6URzbtcb4mcsuNiyvJNq5Pdo0QnA/gNCWE5kvRfopDjl2JS77f4ytrqxNLcUxP2eH3TfDr53Qx/WrOfnuwPnoJflrcF708u+G2fcEZfSkhSMk29jHvxRjAyW4TUPyUuB5fu2Ocs9iGRiFrmWyx6A+nFO9fPVGzunwmG0wiTNMXtkm5Q8l9IRmraO5q/MvQpgF9GXMLxtdVxzUobKHA5RswAhJGQD9Gdr8wghJGUDtGoGc6QhJGSD9GPdORJWFkaccI9kxH9g14tuloBmnUQ9MWDUk7SOX59Mq2xdtNP0pykqTfuFHvUJLTJP3WbfUOJTlRGoCJhBPriRU4+DhGbpbkVc8q2HNcJysS06KKAhfLNa02iMiSQEQKEFFnIMqZ1H2HEK6CMJySkKSHjuFq5GHPY/GMpuRv3FizHFnIMroBHRi6QZdTs26VqwV2IR3WL3U52etS6pdHUL9unR9Bt6B3Hi0n4D0XrgihfuWOby9XgDbqlw6BXOJ8d7mCCLp25QK5YvoOPRfZds+0LpdgPfdcEUL9ylXVZ28pYruoxywhxRrLRa0SNbsz1OSCjLrpGlMWOzQNeViJXT9tQ9W+WW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we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/problems/198.house-robber.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/problems/494.target-sum-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/problems/494.target-sum-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/problems/494.target-sum.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
60 changes: 60 additions & 0 deletions backlog/108.convert-sorted-array-to-binary-search-tree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* @lc app=leetcode id=108 lang=javascript
*
* [108] Convert Sorted Array to Binary Search Tree
*
* https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/
*
* algorithms
* Easy (49.37%)
* Total Accepted: 255.2K
* Total Submissions: 507.2K
* Testcase Example: '[-10,-3,0,5,9]'
*
* Given an array where elements are sorted in ascending order, convert it to a
* height balanced BST.
*
* For this problem, a height-balanced binary tree is defined as a binary tree
* in which the depth of the two subtrees of every node never differ by more
* than 1.
*
* Example:
*
*
* Given the sorted array: [-10,-3,0,5,9],
*
* One possible answer is: [0,-3,9,-10,null,5], which represents the following
* height balanced BST:
*
* ⁠ 0
* ⁠ / \
* ⁠ -3 9
* ⁠ / /
* ⁠-10 5
*
*
*/
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {number[]} nums
* @return {TreeNode}
*/
var sortedArrayToBST = function(nums) {
// 由于数组是排序好的,因此一个思路就是将数组分成两半,一半是左子树,另一半是右子树
// 然后运用“树的递归性质”递归完成操作即可。
if(nums.length === 0) return null;
const mid = nums.length >> 1;
const root = new TreeNode(nums[mid]);

root.left = sortedArrayToBST(nums.slice(0, mid));
root.right = sortedArrayToBST(nums.slice(mid + 1))
return root;
// 扩展: 这道题启示我们如果是一个非排序的数组,我们可以先进行排序然后再按上述思路进行。
};

60 changes: 60 additions & 0 deletions backlog/202.happy-number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* @lc app=leetcode id=202 lang=javascript
*
* [202] Happy Number
*
* https://leetcode.com/problems/happy-number/description/
*
* algorithms
* Easy (44.36%)
* Total Accepted: 227.2K
* Total Submissions: 505.7K
* Testcase Example: '19'
*
* Write an algorithm to determine if a number is "happy".
*
* A happy number is a number defined by the following process: Starting with
* any positive integer, 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), or it loops endlessly in a cycle which does not include 1. Those
* numbers for which this process ends in 1 are happy numbers.
*
* Example: 
*
*
* Input: 19
* Output: true
* Explanation:
* 1^2 + 9^2 = 82
* 8^2 + 2^2 = 68
* 6^2 + 8^2 = 100
* 1^2 + 0^2 + 0^2 = 1
*
*/
function squareSum(n) {
let sum = 0, tmp;
while (n) {
tmp = n % 10;
sum += tmp * tmp;
n = Math.floor(n / 10);
}
return sum;
}

function isHappyWithMapper(n, visited) {
if (n === 1) return true;
if (visited[n]) return false;
visited[n] = true;

return isHappyWithMapper(squareSum(n), visited);
}
/**
* @param {number} n
* @return {boolean}
*/
var isHappy = function(n) {
const visited = {};

return isHappyWithMapper(n, visited);
};

58 changes: 58 additions & 0 deletions backlog/204.count-primes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* @lc app=leetcode id=204 lang=javascript
*
* [204] Count Primes
*
* https://leetcode.com/problems/count-primes/description/
*
* algorithms
* Easy (28.33%)
* Total Accepted: 229.8K
* Total Submissions: 798.7K
* Testcase Example: '10'
*
* Count the number of prime numbers less than a non-negative number, n.
*
* Example:
*
*
* Input: 10
* Output: 4
* Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
*
*
*/
/**
* @param {number} n
* @return {number}
*/
var countPrimes = function(n) {
// tag: 数论
// if (n <= 2) return 0;
// let compositionCount = 0;
// for(let i = 3; i < n; i++) {
// for(let j = i - 1; j > 1 ; j--) {
// if (i % j === 0) {
// compositionCount++;
// break; // 找到一个就可以证明它不是质数了
// }
// }
// }
// return n - compositionCount - 2; // 需要减去1和n这两个数字


// 上面的方法会超时,因此我们需要进行优化
// 数学角度来看,如果一个数字可以分解为两个数字相乘(这两个数字不包括0和它本身),那么它就是合数
const compositions = []; // compositions[i] 表示i是否是合数
let count = 0;
for(let i = 2; i < n; i++) {
if (!compositions[i]) count++;
for(let j = 2; i * j < n; j++) {
compositions[i * j] = true;
}
}

return count;

};

71 changes: 71 additions & 0 deletions backlog/21.merge-two-sorted-lists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* @lc app=leetcode id=21 lang=javascript
*
* [21] Merge Two Sorted Lists
*
* https://leetcode.com/problems/merge-two-sorted-lists/description/
*
* algorithms
* Easy (46.02%)
* Total Accepted: 562.7K
* Total Submissions: 1.2M
* Testcase Example: '[1,2,4]\n[1,3,4]'
*
* Merge two sorted linked lists and return it as a new list. The new list
* should be made by splicing together the nodes of the first two lists.
*
* Example:
*
* Input: 1->2->4, 1->3->4
* Output: 1->1->2->3->4->4
*
*
*/
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
var mergeTwoLists = function(l1, l2) {
let current = new ListNode();
const dummy = current;

while (l1 || l2) {
if (!l1) {
current.next = l2;
return dummy.next;
} else if (!l2) {
current.next = l1;
return dummy.next;
}

if (l1.val <= l2.val) {
current.next = l1;
l1 = l1.next;
} else {
current.next = l2;
l2 = l2.next;
}

current = current.next;
}

return dummy.next;

// if (l1 === null) return l2;
// if (l2 === null) return l1;
// if (l1.val < l2.val) {
// l1.next = mergeTwoLists(l1.next, l2);
// return l1;
// } else {
// l2.next = mergeTwoLists(l1, l2.next);
// return l2;
// }
};
50 changes: 50 additions & 0 deletions backlog/268.missing-number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* @lc app=leetcode id=268 lang=javascript
*
* [268] Missing Number
*
* https://leetcode.com/problems/missing-number/description/
*
* algorithms
* Easy (47.60%)
* Total Accepted: 267.7K
* Total Submissions: 556.2K
* Testcase Example: '[3,0,1]'
*
* Given an array containing n distinct numbers taken from 0, 1, 2, ..., n,
* find the one that is missing from the array.
*
* Example 1:
*
*
* Input: [3,0,1]
* Output: 2
*
*
* Example 2:
*
*
* Input: [9,6,4,2,3,5,7,0,1]
* Output: 8
*
*
* Note:
* Your algorithm should run in linear runtime complexity. Could you implement
* it using only constant extra space complexity?
*/
/**
* @param {number[]} nums
* @return {number}
*/
var missingNumber = function(nums) {
// 缺失的数字一定是 0 到 n 之间的一个数字

// 这是一道数论的题目
// 这里用到了一条性质: sum([1,n]) = n * (n+1) / 2
let sum = 0;
for(let num of nums)
sum += num;

return (nums.length * (nums.length + 1) )/ 2 - sum;
};

Loading

0 comments on commit a86931f

Please sign in to comment.