Skip to content

Commit db3438a

Browse files
author
Ruby Chi
committed
Updates hard/57.md
Auto commit by GitBook Editor
1 parent dfe5fb8 commit db3438a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+851
-157
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ Here illustrate the problems I solved using JavaScript on [LeetCode](https://lee
66

77
You can browse the entire document [here](https://rubychi1.gitbooks.io/leetcode/)
88

9-
## 73/656 Solved
9+
## 81/656 Solved
1010

11-
* [Easy \(71\)](/easy.md)
11+
* [Easy \(79\)](/easy.md)
1212

1313
* [Medium \(1\)](/medium.md)
1414

1515
* [Hard \(1\)](/hard.md)
1616

17-
<hr/>
17+
---
1818

19-
*Last updated on Fri Sep 22 2017 23:14:24 GMT+0800*
19+
*Last updated on Sun Sep 24 2017 00:44:37 GMT+0800*

SUMMARY.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
* [Introduction](README.md)
44
* [Easy](easy.md)
55
* [13. Roman to Integer](easy/13.md)
6+
* [70. Climbing Stairs](easy/70.md)
67
* [100. Same Tree](easy/100.md)
78
* [104. Maximum Depth of Binary Tree](easy/104.md)
9+
* [107. Binary Tree Level Order Traversal II ](easy/107.md)
810
* [108. Convert Sorted Array to Binary Search Tree](easy/108.md)
911
* [121. Best Time to Buy and Sell Stock](easy/121.md)
1012
* [122. Best Time to Buy and Sell Stock II](easy/122.md)
@@ -13,15 +15,18 @@
1315
* [168. Excel Sheet Column Title](easy/168.md)
1416
* [169. Majority Element](easy/169.md)
1517
* [171. Excel Sheet Column Number](easy/171.md)
18+
* [202. Happy Number](easy/202.md)
1619
* [206. Reverse Linked List](easy/206.md)
1720
* [217. Contains Duplicate](easy/217.md)
1821
* [226. Invert Binary Tree](easy/226.md)
22+
* [231. Power of Two](easy/231.md)
1923
* [237. Delete Node in a Linked List](easy/237.md)
2024
* [242. Valid Anagram](easy/242.md)
2125
* [258. Add Digits](easy/258.md)
2226
* [268. Missing Number](easy/268.md)
2327
* [283. Move Zeroes](easy/283.md)
2428
* [292. Nim Game](easy/292.md)
29+
* [326. Power of Three](easy/326.md)
2530
* [344. Reverse String](easy/344.md)
2631
* [349. Intersection of Two Arrays](easy/349.md)
2732
* [350. Intersection of Two Arrays II](easy/350.md)
@@ -60,13 +65,16 @@
6065
* [561. Array Partition I](easy/561.md)
6166
* [563. Binary Tree Tilt](easy/563.md)
6267
* [566. Reshape the Matrix](easy/566.md)
68+
* [572. Subtree of Another Tree](easy/572.md)
6369
* [575. Distribute Candies](easy/575.md)
70+
* [594. Longest Harmonious Subsequence](easy/594.md)
6471
* [598. Range Addition II](easy/598.md)
6572
* [599. Minimum Index Sum of Two Lists](easy/599.md)
6673
* [606. Construct String from Binary Tree](easy/606.md)
6774
* [617. Merge Two Binary Trees](easy/617.md)
6875
* [628. Maximum Product of Three Numbers](easy/628.md)
6976
* [637. Average of Levels in Binary Tree](easy/637.md)
77+
* [645. Set Mismatch](easy/645.md)
7078
* [653. Two Sum IV - Input is a BST](easy/653.md)
7179
* [657. Judge Route Circle](easy/657.md)
7280
* [669. Trim a Binary Search Tree](easy/669.md)

easy.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
| \# | Title | Acceptance |
66
| :--- | :--- | :--- |
77
| 13 | [Roman to Integer](/easy/13.md) | 45.9% |
8+
| 70 | [Climbing Stairs](/easy/70.md) | 40.2% |
89
| 100 | [Same Tree](/easy/100.md) | 46.6% |
910
| 104 | [Maximum Depth of Binary Tree](/easy/104.md) | 52.7% |
11+
| 107 | [Binary Tree Level Order Traversal II](/easy/107.md) | 40.6% |
1012
| 108 | [Convert Sorted Array to Binary Search Tree](/easy/108.md) | 42.4% |
1113
| 121 | [Best Time to Buy and Sell Stock](/easy/121.md) | 41.3% |
1214
| 122 | [Best Time to Buy and Sell Stock II](/easy/122.md) | 47.1% |
@@ -15,15 +17,18 @@
1517
| 168 | [Excel Sheet Column Title](/easy/168.md) | 26.1% |
1618
| 169 | [Majority Element](/easy/169.md) | 46.7% |
1719
| 171 | [Excel Sheet Column Number](/easy/171.md) | 47.2% |
20+
| 202 | [Happy Number](/easy/202.md) | 40.8% |
1821
| 206 | [Reverse Linked List](/easy/206.md) | 45.5% |
1922
| 217 | [Contains Duplicate](/easy/217.md) | 45.9% |
2023
| 226 | [Invert Binary Tree](/easy/226.md) | 51.9% |
24+
| 231 | [Power of Two](/easy/231.md) | 40.3% |
2125
| 237 | [Delete Node in a Linked List](/easy/237.md) | 46.6% |
2226
| 242 | [Valid Anagram](/easy/242.md) | 46.5% |
2327
| 258 | [Add Digits](/easy/258.md) | 51.2% |
2428
| 268 | [Missing Number](/easy/268.md) | 44.3% |
2529
| 283 | [Move Zeroes](/easy/283.md) | 50.0% |
2630
| 292 | [Nim Game](/easy/292.md) | 55.2% |
31+
| 326 | [Power of Three](/easy/326.md) | 40.3% |
2732
| 344 | [Reverse String](/easy/344.md) | 59.2% |
2833
| 349 | [Intersection of Two Arrays](/easy/349.md) | 47.3% |
2934
| 350 | [Intersection of Two Arrays II](/easy/350.md) | 44.7% |
@@ -62,13 +67,16 @@
6267
| 561 | [Array Partition I](/easy/561.md) | 67.1% |
6368
| 563 | [Binary Tree Tilt](/easy/563.md) | 46.8% |
6469
| 566 | [Reshape the Matrix](/easy/566.md) | 59.1% |
70+
| 572 | [Subtree of Another Tree](/easy/572.md) | 40.7% |
6571
| 575 | [Distribute Candies](/easy/575.md) | 59.2% |
72+
| 594 | [Longest Harmonious Subsequence](/easy/594.md) | 40.8% |
6673
| 598 | [Range Addition II](/easy/598.md) | 48.1% |
6774
| 599 | [Minimum Index Sum of Two Lists](/easy/599.md) | 46.8% |
6875
| 606 | [Construct String from Binary Tree](/easy/606.md) | 50.1% |
6976
| 617 | [Merge Two Binary Trees](/easy/617.md) | 68.3% |
7077
| 628 | [Maximum Product of Three Numbers](/easy/628.md) | 45.3% |
7178
| 637 | [Average of Levels in Binary Tree](/easy/637.md) | 55.8% |
79+
| 645 | [Set Mismatch](/easy/645.md) | 40.3% |
7280
| 653 | [Two Sum IV - Input is a BST](/easy/653.md) | 51.1% |
7381
| 657 | [Judge Route Circle](/easy/657.md) | 69.7% |
7482
| 661 | [Image Smoother](/easy/661.md) | 47.4% |

easy/100.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
# Description
1+
# 100. Same Tree
2+
3+
## Description
24

35
Given two binary trees, write a function to check if they are equal or not.
46

57
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
68

7-
## Solution
9+
### Solution
810
Solution 1
911
```javascript
1012
/**
@@ -21,6 +23,7 @@ Solution 1
2123
* @return {boolean}
2224
*/
2325
var isSameTree = function(p, q) {
26+
"use strict";
2427
let result = true;
2528
function dfs(node1, node2) {
2629
if (node1 && node2) {
@@ -54,6 +57,7 @@ Solution 2
5457
* @return {boolean}
5558
*/
5659
var isSameTree = function(p, q) {
60+
"use strict";
5761
if (p && q) {
5862
let stackP = [p], stackQ = [q];
5963
let pPtr = null, qPtr = null;
@@ -95,6 +99,7 @@ Solution 3
9599
* @return {boolean}
96100
*/
97101
var isSameTree = function(p, q) {
102+
"use strict";
98103
if (p && q) {
99104
let queue1 = [p], queue2 = [q];
100105
while (queue1.length && queue2.length) {

easy/104.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
# Description
1+
# 104. Maximum Depth of Binary Tree
2+
3+
## Description
24

35
Given a binary tree, find its maximum depth.
46

57
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
68

7-
## Solution
9+
### Solution
810
Solution 1
911
```javascript
1012
/**
@@ -20,6 +22,7 @@ Solution 1
2022
* @return {number}
2123
*/
2224
var maxDepth = function(root) {
25+
"use strict";
2326
let result = 0;
2427
let queue = root ? [root] : [];
2528
while(queue.length) {
@@ -53,6 +56,7 @@ Solution 2
5356
* @return {number}
5457
*/
5558
var maxDepth = function(root) {
59+
"use strict";
5660
function dfs(node, depth) {
5761
if (!node) {
5862
return depth-1;

easy/107.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# 107. Binary Tree Level Order Traversal II
2+
3+
## Description
4+
5+
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
6+
7+
For example:
8+
Given binary tree `[3,9,20,null,null,15,7]`,
9+
```
10+
3
11+
/ \
12+
9 20
13+
/ \
14+
15 7
15+
```
16+
return its bottom-up level order traversal as:
17+
```
18+
[
19+
[15,7],
20+
[9,20],
21+
[3]
22+
]
23+
```
24+
25+
### Solution
26+
```javascript
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+
* Traverse the binary tree with Breadth-First Search (BFS) algorithm
36+
* @param {TreeNode} root
37+
* @return {number[][]}
38+
*/
39+
var levelOrderBottom = function(root) {
40+
"use strict";
41+
if (!root) {
42+
return [];
43+
}
44+
let result = [[root.val]];
45+
let queue = [root];
46+
while (queue.length) {
47+
let level = [];
48+
let len = queue.length;
49+
for (let i = 0; i < len; i++) {
50+
let left = queue[i].left, right = queue[i].right;
51+
if (left) {
52+
queue.push(left);
53+
level.push(left.val);
54+
}
55+
if (right) {
56+
queue.push(right);
57+
level.push(right.val);
58+
}
59+
}
60+
level.length && result.push(level);
61+
queue = queue.slice(len);
62+
}
63+
return result.reverse();
64+
};
65+
```

easy/108.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
# Description
1+
# 108. Convert Sorted Array to Binary Search Tree
2+
3+
## Description
24

35
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
46

5-
## Solution
7+
### Solution
68
```javascript
79
/**
810
* Definition for a binary tree node.

easy/121.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# Description
1+
# 121. Best Time to Buy and Sell Stock
2+
3+
## Description
24

35
Say you have an array for which the *i*<sup>th</sup> element is the price of a given stock on day *i*.
46

@@ -18,13 +20,14 @@ Output: 0
1820
1921
In this case, no transaction is done, i.e. max profit = 0.
2022
```
21-
## Solution
23+
### Solution
2224
```javascript
2325
/**
2426
* @param {number[]} prices
2527
* @return {number}
2628
*/
2729
var maxProfit = function(prices) {
30+
"use strict";
2831
let profit = 0
2932
let min = prices[0], max = prices[0];
3033
for (let i = 1; i < prices.length; i++) {

easy/122.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
# Description
1+
# 122. Best Time to Buy and Sell Stock II
2+
3+
## Description
24

35
Say you have an array for which the *i*<sup>th</sup> element is the price of a given stock on day *i*.
46

57
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
68

7-
8-
9-
## Solution
9+
### Solution
1010
```javascript
1111
/**
1212
* Solved with two pointers
1313
* @param {number[]} prices
1414
* @return {number}
1515
*/
1616
var maxProfit = function(prices) {
17+
"use strict";
1718
let profit = 0;
1819
// Use pointer j to find ascending sequences and another pointer i for pointing the current lowest price
1920
for (let i = 0, j = 0; j < prices.length; j++) {

easy/13.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
# Description
1+
# 13. Roman to Integer
2+
3+
## Description
24

35
Given a roman numeral, convert it to an integer.
46

57
Input is guaranteed to be within the range from 1 to 3999.
68

7-
## Solution
9+
### Solution
810
```javascript
911
/**
1012
* Solved with hash table
1113
* @param {string} s
1214
* @return {number}
1315
*/
1416
var romanToInt = function(s) {
17+
"use strict";
1518
let table = {
1619
"IV": 4,
1720
"IX": 9,

0 commit comments

Comments
 (0)