Skip to content

Commit aaeec26

Browse files
committed
finish 91,92,94,95,96
1 parent 40cd5d1 commit aaeec26

5 files changed

+393
-0
lines changed

001-100/91. Decode Ways.md

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# 91. Decode Ways
2+
3+
- Difficulty: Medium.
4+
- Related Topics: String, Dynamic Programming.
5+
- Similar Questions: Decode Ways II.
6+
7+
## Problem
8+
9+
A message containing letters from ```A-Z``` is being encoded to numbers using the following mapping:
10+
11+
```
12+
'A' -> 1
13+
'B' -> 2
14+
...
15+
'Z' -> 26
16+
```
17+
18+
Given a **non-empty** string containing only digits, determine the total number of ways to decode it.
19+
20+
**Example 1:**
21+
22+
```
23+
Input: "12"
24+
Output: 2
25+
Explanation: It could be decoded as "AB" (1 2) or "L" (12).
26+
```
27+
28+
**Example 2:**
29+
30+
```
31+
Input: "226"
32+
Output: 3
33+
Explanation: It could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).
34+
```
35+
36+
## Solution
37+
38+
```javascript
39+
/**
40+
* @param {string} s
41+
* @return {number}
42+
*/
43+
var numDecodings = function(s) {
44+
var len = s.length;
45+
var tmp = 0;
46+
var tmp1 = 1;
47+
var tmp2 = 0;
48+
var num1 = 0;
49+
var num2 = 0;
50+
51+
if (s === '' || s[0] === '0') return 0;
52+
53+
for (var i = 1; i <= len; i++) {
54+
num1 = Number(s.substr(i - 1, 1));
55+
num2 = Number(s.substr(i - 2, 2));
56+
if (num1 !== 0) tmp += tmp1;
57+
if (10 <= num2 && num2 <= 26) tmp += tmp2;
58+
tmp2 = tmp1;
59+
tmp1 = tmp;
60+
tmp = 0;
61+
}
62+
63+
return tmp1;
64+
};
65+
```
66+
67+
**Explain:**
68+
69+
每个点可由前面一个点 decode 1 一个数字到达或前面两个点 decode 2 个数字到达。
70+
71+
**Complexity:**
72+
73+
* Time complexity : O(n).
74+
* Space complexity : O(1).

001-100/92. Reverse Linked List II.md

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# 92. Reverse Linked List II
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Linked List.
5+
- Similar Questions: Reverse Linked List.
6+
7+
## Problem
8+
9+
Reverse a linked list from position **m** to **n**. Do it in one-pass.
10+
11+
**Note: **1 ≤ **m****n** ≤ length of list.
12+
13+
**Example:**
14+
15+
```
16+
Input: 1->2->3->4->5->NULL, m = 2, n = 4
17+
Output: 1->4->3->2->5->NULL
18+
```
19+
20+
## Solution
21+
22+
```javascript
23+
/**
24+
* Definition for singly-linked list.
25+
* function ListNode(val) {
26+
* this.val = val;
27+
* this.next = null;
28+
* }
29+
*/
30+
/**
31+
* @param {ListNode} head
32+
* @param {number} m
33+
* @param {number} n
34+
* @return {ListNode}
35+
*/
36+
var reverseBetween = function(head, m, n) {
37+
var newHead = new ListNode(0);
38+
var now = newHead;
39+
var tmp = null;
40+
var reverseLast = null;
41+
var reverseHead = null;
42+
var reverseNow = null;
43+
var i = 0;
44+
45+
newHead.next = head;
46+
47+
while (now) {
48+
tmp = now.next;
49+
50+
if (i === m - 1) {
51+
reverseHead = now;
52+
}
53+
54+
if (i === m) {
55+
reverseLast = now;
56+
reverseNow = now;
57+
}
58+
59+
if (i > m && i <= n) {
60+
now.next = reverseNow;
61+
reverseNow = now;
62+
}
63+
64+
if (i === n) {
65+
reverseLast.next = tmp;
66+
reverseHead.next = reverseNow;
67+
}
68+
69+
now = tmp;
70+
i++;
71+
}
72+
73+
return newHead.next;
74+
};
75+
```
76+
77+
**Explain:**
78+
79+
nope.
80+
81+
**Complexity:**
82+
83+
* Time complexity : O(n).
84+
* Space complexity : O(1).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# 94. Binary Tree Inorder Traversal
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Hash Table, Stack, Tree.
5+
- Similar Questions: Validate Binary Search Tree, Binary Tree Preorder Traversal, Binary Tree Postorder Traversal, Binary Search Tree Iterator, Kth Smallest Element in a BST, Closest Binary Search Tree Value II, Inorder Successor in BST, Minimum Distance Between BST Nodes.
6+
7+
## Problem
8+
9+
Given a binary tree, return the **inorder** traversal of its nodes' values.
10+
11+
**Example:**
12+
13+
```
14+
Input: [1,null,2,3]
15+
1
16+
\
17+
2
18+
/
19+
3
20+
21+
Output: [1,3,2]
22+
```
23+
24+
**Follow up:** Recursive solution is trivial, could you do it iteratively?
25+
26+
## Solution 1
27+
28+
```javascript
29+
/**
30+
* Definition for a binary tree node.
31+
* function TreeNode(val) {
32+
* this.val = val;
33+
* this.left = this.right = null;
34+
* }
35+
*/
36+
/**
37+
* @param {TreeNode} root
38+
* @return {number[]}
39+
*/
40+
var inorderTraversal = function(root) {
41+
var res = [];
42+
helper(root, res);
43+
return res;
44+
};
45+
46+
var helper = function (root, res) {
47+
if (!root) return;
48+
if (root.left) helper(root.left, res);
49+
res.push(root.val);
50+
if (root.right) helper(root.right, res);
51+
};
52+
```
53+
54+
**Explain:**
55+
56+
nope.
57+
58+
**Complexity:**
59+
60+
* Time complexity : O(n).
61+
* Space complexity : O(n).
62+
63+
## Solution 2
64+
65+
```javascript
66+
/**
67+
* Definition for a binary tree node.
68+
* function TreeNode(val) {
69+
* this.val = val;
70+
* this.left = this.right = null;
71+
* }
72+
*/
73+
/**
74+
* @param {TreeNode} root
75+
* @return {number[]}
76+
*/
77+
var inorderTraversal = function(root) {
78+
var stack = [];
79+
var now = root;
80+
var res = [];
81+
82+
while (now || stack.length) {
83+
while (now) {
84+
stack.push(now);
85+
now = now.left;
86+
}
87+
now = stack.pop();
88+
res.push(now.val);
89+
now = now.right;
90+
}
91+
92+
return res;
93+
};
94+
```
95+
96+
**Explain:**
97+
98+
nope.
99+
100+
**Complexity:**
101+
102+
* Time complexity : O(n).
103+
* Space complexity : O(n).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# 95. Unique Binary Search Trees II
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Dynamic Programming, Tree.
5+
- Similar Questions: Unique Binary Search Trees, Different Ways to Add Parentheses.
6+
7+
## Problem
8+
9+
Given an integer **n**, generate all structurally unique **BST's** (binary search trees) that store values 1 ... **n**.
10+
11+
**Example:**
12+
13+
```
14+
Input: 3
15+
Output:
16+
[
17+
  [1,null,3,2],
18+
  [3,2,null,1],
19+
  [3,1,null,null,2],
20+
  [2,1,3],
21+
  [1,null,2,null,3]
22+
]
23+
Explanation:
24+
The above output corresponds to the 5 unique BST's shown below:
25+
26+
1 3 3 2 1
27+
\ / / / \ \
28+
3 2 1 1 3 2
29+
/ / \ \
30+
2 1 2 3
31+
```
32+
33+
## Solution
34+
35+
```javascript
36+
/**
37+
* Definition for a binary tree node.
38+
* function TreeNode(val) {
39+
* this.val = val;
40+
* this.left = this.right = null;
41+
* }
42+
*/
43+
/**
44+
* @param {number} n
45+
* @return {TreeNode[]}
46+
*/
47+
var generateTrees = function(n) {
48+
if (n < 1) return [];
49+
return generate(1, n);
50+
};
51+
52+
var generate = function (l, r) {
53+
var nodes = [];
54+
var node = null;
55+
var left = [];
56+
var right = [];
57+
for (var i = l; i <= r; i++) {
58+
left = generate(l, i - 1);
59+
right = generate(i + 1, r);
60+
for (var j = 0; j < left.length; j++) {
61+
for (var k = 0; k < right.length; k++) {
62+
node = new TreeNode(i);
63+
node.left = left[j];
64+
node.right = right[k];
65+
nodes.push(node);
66+
}
67+
}
68+
}
69+
return nodes.length ? nodes : [null];
70+
};
71+
```
72+
73+
**Explain:**
74+
75+
例如:有 `5` 个节点,选取其中 `1` 个作为 `root`,还剩 `4` 个。则左右可分为 `(0, 4), (1, 3), (2, 2), (3, 1), (4, 0)` 等几种情况,左右的分配用递归得到即可。
76+
77+
**Complexity:**
78+
79+
* Time complexity :
80+
* Space complexity :
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# 96. Unique Binary Search Trees
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Dynamic Programming, Tree.
5+
- Similar Questions: Unique Binary Search Trees II.
6+
7+
## Problem
8+
9+
Given **n**, how many structurally unique **BST's** (binary search trees) that store values 1 ... **n**?
10+
11+
**Example:**
12+
13+
```
14+
Input: 3
15+
Output: 5
16+
Explanation:
17+
Given n = 3, there are a total of 5 unique BST's:
18+
19+
1 3 3 2 1
20+
\ / / / \ \
21+
3 2 1 1 3 2
22+
/ / \ \
23+
2 1 2 3
24+
```
25+
26+
## Solution
27+
28+
```javascript
29+
/**
30+
* @param {number} n
31+
* @return {number}
32+
*/
33+
var numTrees = function(n) {
34+
var dp = [1, 1];
35+
for (i = 2; i <= n; i++) {
36+
dp[i] = 0;
37+
for (j = 1; j <= i; j++) {
38+
dp[i] += dp[i - j] * dp[j - 1];
39+
}
40+
}
41+
return dp[n];
42+
};
43+
```
44+
45+
**Explain:**
46+
47+
例如:有 `3` 个节点,取 `1` 个作为 `root`,还剩 `2` 个。可以左边 `0` 个,右边 `2` 个;左边 `1` 个,右边 `1` 个;左边 `2` 个,右边 `0` 个。即:`F(3) = F(0) * F(2) + F(1) * F(1) + F(2) * F(0)`。其中 `F(0) = F(1) = 1`
48+
49+
**Complexity:**
50+
51+
* Time complexity :
52+
* Space complexity :

0 commit comments

Comments
 (0)