241 为运算表达式设计优先级
给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果。你需要给出所有可能的组合的结果。有效的运算符号包含+
,-
以及*
。
示例 1:
输入: "2-1-1"
输出: [0, 2]
解释:
((2-1)-1) = 0
(2-(1-1)) = 2
示例 2:
输入: "2*3-4*5"
输出: [-34, -14, -10, -10, 10]
解释: (2*(3-(4*5))) = -34
((2*3)-(4*5)) = -14
((2*(3-4))*5) = -10
(2*((3-4)*5)) = -10
(((2*3)-4)*5) = 10
题解:分治
class Solution {
public List diffWaysToCompute(String input) {
List res = new ArrayList<>();
if (input == null || input.length() == 0){
return res;
}
// 字符串全为数字时转化为数字并返回
int num = 0;
// 考虑是全数字的情况
int index = 0;
while (index < input.length() && !isOperation(input.charAt(index))) {
num = num * 10 + input.charAt(index) - '0';
index ++;
}
// 将全数字的情况直接返回
if (index == input.length()) {
res.add(num);
return res;
}
for(int i=0; i res1 = diffWaysToCompute(input.substring(0,i));
List res2 = diffWaysToCompute(input.substring(i+1));
for (int j = 0; j < res1.size(); j++){
for (int k = 0; k < res2.size(); k++){
num = calculate(res1.get(j), input.charAt(i), res2.get(k));
res.add(num);
}
}
}
}
return res;
}
// 计算两个数的运算值
public int calculate(int num1, char op, int num2){
switch(op){
case '+':
return num1 + num2;
case '-':
return num1 - num2;
case '*':
return num1 * num2;
}
return -1;
}
public boolean isOperation(char ch){
return ch == '+' || ch == '-' || ch == '*';
}
}
递归分治优化:Map保存计算过的结果
Solution {
public List diffWaysToCompute(String input) {
List res = new ArrayList<>();
Map map = new HashMap();
// 如果map已经存放input对应的结果,直接将值添加到res,并返回。
if (map.containsKey(input)){
res.add(map.get(input));
return res;
}
if (input == null || input.length() == 0){
return res;
}
// 字符串全为数字时转化为数字并返回
int num = 0;
// 考虑是全数字的情况
int index = 0;
while (index < input.length() && !isOperation(input.charAt(index))) {
num = num * 10 + input.charAt(index) - '0';
index ++;
}
// 将全数字的情况直接返回
if (index == input.length()) {
res.add(num);
return res;
}
for(int i=0; i res1 = diffWaysToCompute(input.substring(0,i));
List res2 = diffWaysToCompute(input.substring(i+1));
for (int j = 0; j < res1.size(); j++){
for (int k = 0; k < res2.size(); k++){
num = calculate(res1.get(j), input.charAt(i), res2.get(k));
res.add(num);
map.put(input, num);
}
}
}
}
return res;
}
// 计算两个数的运算值
public int calculate(int num1, char op, int num2){
switch(op){
case '+':
return num1 + num2;
case '-':
return num1 - num2;
case '*':
return num1 * num2;
}
return -1;
}
public boolean isOperation(char ch){
return ch == '+' || ch == '-' || ch == '*';
}
}
95. 不同的二叉搜索树 II
给定一个整数 n,生成所有由 1 ... n 为节点所组成的 二叉搜索树 。
示例:
输入:3
输出:
[
[1,null,3,2],
[3,2,null,1],
[3,1,null,null,2],
[2,1,3],
[1,null,2,null,3]
]
解释:
以上的输出对应以下 5 种不同结构的二叉搜索树:
1 3 3 2 1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3
提示:
0 <= n <= 8
题解:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List generateTrees(int n) {
List res = new ArrayList<>();
if (n<=0){
return res;
}
return help(1, n);
}
public List help(int start, int end){
List list = new ArrayList<>();
if (start > end){
list.add(null);
return list;
}
if (start == end){
list.add(new TreeNode(start));
}
else if (start < end){
for (int i = start; i<=end; i++){
List leftTrees = help(start, i-1);
List rightTrees = help(i+1, end);
for (TreeNode left: leftTrees){
for (TreeNode right: rightTrees){
TreeNode root = new TreeNode(i, left, right);
list.add(root);
}
}
}
}
return list;
}
}