2019-04-21 10:36:08 +08:00
|
|
|
|
<!-- GFM-TOC -->
|
2019-03-27 20:57:37 +08:00
|
|
|
|
* [1. 给表达式加括号](#1-给表达式加括号)
|
2019-04-20 13:14:16 +08:00
|
|
|
|
* [2. 不同的二叉搜索树](#2-不同的二叉搜索树)
|
2019-04-21 10:36:08 +08:00
|
|
|
|
<!-- GFM-TOC -->
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
|
|
|
|
|
# 1. 给表达式加括号
|
|
|
|
|
|
2019-10-27 00:52:52 +08:00
|
|
|
|
241\. Different Ways to Add Parentheses (Medium)
|
|
|
|
|
|
|
|
|
|
[Leetcode](https://leetcode.com/problems/different-ways-to-add-parentheses/description/) / [力扣](https://leetcode-cn.com/problems/different-ways-to-add-parentheses/description/)
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
```html
|
2019-03-27 20:57:37 +08:00
|
|
|
|
Input: "2-1-1".
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
((2-1)-1) = 0
|
|
|
|
|
(2-(1-1)) = 2
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
Output : [0, 2]
|
2019-03-08 20:31:07 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```java
|
2019-03-27 20:57:37 +08:00
|
|
|
|
public List<Integer> diffWaysToCompute(String input) {
|
|
|
|
|
List<Integer> ways = new ArrayList<>();
|
|
|
|
|
for (int i = 0; i < input.length(); i++) {
|
|
|
|
|
char c = input.charAt(i);
|
|
|
|
|
if (c == '+' || c == '-' || c == '*') {
|
|
|
|
|
List<Integer> left = diffWaysToCompute(input.substring(0, i));
|
|
|
|
|
List<Integer> right = diffWaysToCompute(input.substring(i + 1));
|
|
|
|
|
for (int l : left) {
|
|
|
|
|
for (int r : right) {
|
|
|
|
|
switch (c) {
|
|
|
|
|
case '+':
|
|
|
|
|
ways.add(l + r);
|
|
|
|
|
break;
|
|
|
|
|
case '-':
|
|
|
|
|
ways.add(l - r);
|
|
|
|
|
break;
|
|
|
|
|
case '*':
|
|
|
|
|
ways.add(l * r);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (ways.size() == 0) {
|
|
|
|
|
ways.add(Integer.valueOf(input));
|
|
|
|
|
}
|
|
|
|
|
return ways;
|
2019-03-08 20:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
2019-03-27 20:57:37 +08:00
|
|
|
|
|
2019-04-20 13:14:16 +08:00
|
|
|
|
# 2. 不同的二叉搜索树
|
2019-04-15 23:51:41 +08:00
|
|
|
|
|
2019-10-27 00:52:52 +08:00
|
|
|
|
95\. Unique Binary Search Trees II (Medium)
|
|
|
|
|
|
|
|
|
|
[Leetcode](https://leetcode.com/problems/unique-binary-search-trees-ii/description/) / [力扣](https://leetcode-cn.com/problems/unique-binary-search-trees-ii/description/)
|
2019-04-15 23:51:41 +08:00
|
|
|
|
|
2019-04-20 13:14:16 +08:00
|
|
|
|
给定一个数字 n,要求生成所有值为 1...n 的二叉搜索树。
|
|
|
|
|
|
2019-04-15 23:51:41 +08:00
|
|
|
|
```html
|
|
|
|
|
Input: 3
|
|
|
|
|
Output:
|
|
|
|
|
[
|
|
|
|
|
[1,null,3,2],
|
|
|
|
|
[3,2,null,1],
|
|
|
|
|
[3,1,null,null,2],
|
|
|
|
|
[2,1,3],
|
|
|
|
|
[1,null,2,null,3]
|
|
|
|
|
]
|
|
|
|
|
Explanation:
|
|
|
|
|
The above output corresponds to the 5 unique BST's shown below:
|
|
|
|
|
|
|
|
|
|
1 3 3 2 1
|
|
|
|
|
\ / / / \ \
|
|
|
|
|
3 2 1 1 3 2
|
|
|
|
|
/ / \ \
|
|
|
|
|
2 1 2 3
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
public List<TreeNode> generateTrees(int n) {
|
2019-05-14 17:05:21 +08:00
|
|
|
|
if (n < 1) {
|
|
|
|
|
return new LinkedList<TreeNode>();
|
|
|
|
|
}
|
2019-04-15 23:51:41 +08:00
|
|
|
|
return generateSubtrees(1, n);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<TreeNode> generateSubtrees(int s, int e) {
|
|
|
|
|
List<TreeNode> res = new LinkedList<TreeNode>();
|
|
|
|
|
if (s > e) {
|
|
|
|
|
res.add(null);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
for (int i = s; i <= e; ++i) {
|
|
|
|
|
List<TreeNode> leftSubtrees = generateSubtrees(s, i - 1);
|
|
|
|
|
List<TreeNode> rightSubtrees = generateSubtrees(i + 1, e);
|
|
|
|
|
for (TreeNode left : leftSubtrees) {
|
|
|
|
|
for (TreeNode right : rightSubtrees) {
|
|
|
|
|
TreeNode root = new TreeNode(i);
|
|
|
|
|
root.left = left;
|
|
|
|
|
root.right = right;
|
|
|
|
|
res.add(root);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
```
|
2019-03-27 20:57:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-04-20 13:14:16 +08:00
|
|
|
|
|
2019-10-28 00:20:30 +08:00
|
|
|
|
<div align="center"><img width="320px" src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/githubio/公众号二维码-1.png"></img></div>
|