Leetcode - Different Ways to Add Parentheses

Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +, - and *.
Example 2
Input: "2*3-4*5"

(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
Output: [-34, -14, -10, -10, 10]

[分析] 这题参考[url]https://leetcode.com/discuss/48488/c-4ms-recursive-method[/url] 。 思路类似Unique Binary Search Trees II,以某个运算符为分界线,递归计算左右两边可能的值,然后根据当前运算符归并结果。纯粹的递归含有冗余计算,可同时保留中间结果来提高效率。


public class Solution {
// method 2: dp
public List<Integer> diffWaysToCompute(String input) {
HashMap<String, List<Integer>> dpMap = new HashMap<String, List<Integer>>();
return computeWithDP(input, dpMap);
}
public List<Integer> computeWithDP(String input, HashMap<String, List<Integer>> dpMap) {
List<Integer> result = new ArrayList<Integer>();
if (input == null || input.length() == 0) return result;
int N = input.length();
for (int i = 0; i < N; i++) {
char c = input.charAt(i);
if (c == '+' || c == '-' || c == '*') {
String leftSubStr = input.substring(0, i);
String rightSubStr = input.substring(i + 1, N);
List<Integer> left = dpMap.get(leftSubStr);
if (left == null)
left = computeWithDP(leftSubStr, dpMap);
List<Integer> right = dpMap.get(rightSubStr);
if (right == null)
right = computeWithDP(rightSubStr, dpMap);
for (int op1: left) {
for (int op2: right) {
if (c == '+')
result.add(op1 + op2);
else if (c == '-')
result.add(op1 - op2);
else
result.add(op1 * op2);
}
}
}
}
if (result.isEmpty())
result.add(Integer.parseInt(input));
dpMap.put(input, result);
return result;
}

// method 1: recursive
public List<Integer> diffWaysToCompute1(String input) {
List<Integer> result = new ArrayList<Integer>();
if (input == null || input.length() == 0) return result;
int N = input.length();
for (int i = 0; i < N; 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, N));
for (int op1 : left) {
for (int op2 : right) {
if (c == '+')
result.add(op1 + op2);
else if (c == '-')
result.add(op1 - op2);
else
result.add(op1 * op2);
}
}
}
}
// if the string contains only number
if (result.isEmpty())
result.add(Integer.parseInt(input));
return result;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值