[LeetCode] Different Ways to Add Parentheses

简介: Well, this problem seems to be a little tricky at first glance. However, the idea (taken from this link) is really simple.

Well, this problem seems to be a little tricky at first glance. However, the idea (taken from this link) is really simple. Let's take the equation 2*3-4*5 in the problem statement as an example to illustrate the idea. 

The idea is fairly simple: each time we encounter an operator, split the input string into two parts, one left to the operator and the other right to it. For example, when we reach -, we split the string into 2*3 and 4*5. Then we recursively (yeah, this is the biggest simplification) compute all possible values of the left and right parts and operate on all the possible pairs of them. The idea will become much more obvious if you read the following code.

 1 class Solution {
 2 public:
 3     vector<int> diffWaysToCompute(string input) {
 4         vector<int> outputs;
 5         int n = input.length();
 6         for (int i = 0; i < n; i++) {
 7             if (input[i] == '+' || input[i] == '-' || input[i] == '*') {
 8                 string left = input.substr(0, i);
 9                 string right = input.substr(i + 1, n - i - 1);
10                 vector<int> lval = diffWaysToCompute(left);
11                 vector<int> rval = diffWaysToCompute(right);
12                 for (int l : lval) {
13                     for (int r : rval) {
14                         switch (input[i]) {
15                             case '+':
16                                 outputs.push_back(l + r);
17                                 break;
18                             case '-':
19                                 outputs.push_back(l - r);
20                                 break;
21                             default:
22                                 outputs.push_back(l * r);
23                         }
24                     }
25                 }
26             }
27         }
28         if (outputs.empty())
29             outputs.push_back(stoi(input));
30         return outputs;
31     }
32 };

 

目录
相关文章
|
6月前
Leetcode 623. Add One Row to Tree
题目很简单,在树的第d层加一层,值为v。递归增加一层就好了。代码如下
26 0
|
8月前
|
存储 C++ Python
LeetCode刷题---Add Two Numbers(一)
LeetCode刷题---Add Two Numbers(一)
|
11月前
|
存储 算法 安全
LeetCode - #2 Add Two Numbers
我们社区从本期开始会将顾毅(Netflix 增长黑客,《iOS 面试之道》作者,ACE 职业健身教练。)的 Swift 算法题题解整理为文字版以方便大家学习与阅读。 不积跬步,无以至千里;不积小流,无以成江海,Swift社区 伴你前行。
LeetCode - #2 Add Two Numbers
LeetCode 415. Add Strings
给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和。
66 0
LeetCode 415. Add Strings
LeetCode 301. Remove Invalid Parentheses
删除最小数量的无效括号,使得输入的字符串有效,返回所有可能的结果。 说明: 输入可能包含了除 ( 和 ) 以外的字符。
50 0
LeetCode 301. Remove Invalid Parentheses
LeetCode 258. Add Digits
给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数。
52 0
LeetCode 258. Add Digits
LeetCode 241. Different Ways to Add Parentheses
给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果。你需要给出所有可能的组合的结果。有效的运算符号包含 +, - 以及 * 。
54 0
LeetCode 241. Different Ways to Add Parentheses
LeetCode 67. Add Binary
给定两个二进制字符串,返回它们的总和(也是二进制字符串)。 输入字符串都是非空的,只包含字符1或0。
54 0
LeetCode 67. Add Binary
|
存储
Leetcode-Medium 2. Add Two Numbers
Leetcode-Medium 2. Add Two Numbers
52 0
|
Python
Leetcode-Easy 989. Add to Array-Form of Integer
Leetcode-Easy 989. Add to Array-Form of Integer
112 0