LeetCode 241. Different Ways to Add Parentheses为运算表达式设计优先级 (C++)
题目:
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 1:
Input:"2-1-1"
Output:[0, 2]
Explanation:
((2-1)-1) = 0
(2-(1-1)) = 2
Example 2:
Input:"2*3-4*5"
Output:[-34, -14, -10, -10, 10]
Explanation:
(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
分析:
给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果。你需要给出所有可能的组合的结果。有效的运算符号包含 +, - 以及 * 。
以运算符号为界限来划分出左右两个子串,继续递归执行子串,直到只有一个元素为止。
左右两个子串的结果存进数组中,对其中的元素遍历组合得到结果。
diff(2*3-4*5) = { diff(2) * diff(3-4*5) } + { diff(2*3) - diff(4*5) } + { diff(2*3-4) * diff(5) }
其中diff(3-4*5) = {diff(3) - diff(4*5),diff(3-4) * diff(5)}={3-20,-1*5}={-17,-5}
diff(2*3-4) = {diff(2) * diff(3-4),diff(2*3) - diff(4)} = {2*-1,6-4} = {-2,2}
所以diff(2*3-4*5) = {2*{-17,-5}}+{6-20}+{{-2,2}*5}={-34,-10}+{-14}+{-10,10}={-34,-10,-14,-10,10}
程序:
class Solution {
public:
vector<int> diffWaysToCompute(string input) {
vector<int> res;
for(int i = ; i < input.size(); ++i){
if(input[i] == '+' || input[i] == '-' || input[i] == '*'){
vector<int> l = diffWaysToCompute(input.substr(, i));
vector<int> r = diffWaysToCompute(input.substr(i+, input.size()-i));
for(auto p:l)
for(auto q:r){
if(input[i] == '+')
res.push_back(p+q);
if(input[i] == '-')
res.push_back(p-q);
if(input[i] == '*')
res.push_back(p*q);
}
}
}
if(res.empty())
res.push_back(stoi(input));
return res;
}
};
LeetCode 241. Different Ways to Add Parentheses为运算表达式设计优先级 (C++)的更多相关文章
- Leetcode241.Different Ways to Add Parentheses为运算表达式设计优先级
给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 1: 输入: "2-1 ...
- LN : leetcode 241 Different Ways to Add Parentheses
lc 241 Different Ways to Add Parentheses 241 Different Ways to Add Parentheses Given a string of num ...
- [LeetCode] 241. Different Ways to Add Parentheses 添加括号的不同方式
Given a string of numbers and operators, return all possible results from computing all the differen ...
- (medium)LeetCode 241.Different Ways to Add Parentheses
Given a string of numbers and operators, return all possible results from computing all the differen ...
- leetcode@ [241] Different Ways to Add Parentheses (Divide and Conquer)
https://leetcode.com/problems/different-ways-to-add-parentheses/ Given a string of numbers and opera ...
- [LeetCode#241]Different Ways to Add Parentheses
Problem: Given a string of numbers and operators, return all possible results from computing all the ...
- leetcode 96. Unique Binary Search Trees 、95. Unique Binary Search Trees II 、241. Different Ways to Add Parentheses
96. Unique Binary Search Trees https://www.cnblogs.com/grandyang/p/4299608.html 3由dp[1]*dp[1].dp[0]* ...
- 241. Different Ways to Add Parentheses
241. Different Ways to Add Parentheses https://leetcode.com/problems/different-ways-to-add-parenthes ...
- Java实现 LeetCode 241 为运算表达式设计优先级
241. 为运算表达式设计优先级 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 ...
随机推荐
- H5/纯JS实现:把网页中的文字复制到剪切板
copy =() => { const dom = document.getElementById(`collect-text-${t.Id}`) const selection = windo ...
- 为什么用时序电路实现CPU
时序电路 我们带着如下疑问来看时序电路: 1.为什么CPU要用时序电路,时序电路与普通逻辑电路有什么区别. 2.触发器.锁存器以及时钟脉冲对时序电路的作用是什么,它们是如何工作的. 带着这两个问题,我 ...
- ABP docker发布
环境:CentOS 7.6 64位 linux基本命令: cd:进入某个文件夹 mkdir:创建文件夹 ls:显示文件 ll:罗列出当前文件或目录的详细信息 判断是文件 还是文件夹: Linux系统中 ...
- 【LOJ2838】「JOISC 2018 Day 3」比太郎的聚会(设阈值预处理/分块)
点此看题面 大致题意: 给你一张\(DAG\),多组询问,每次问你在起点不为某些点的前提下,到达给定终点的最大距离是多少. 设阈值 由于限制点数总和与\(n\)同阶,因此容易想到去设阈值. 对于限制点 ...
- SSM配置动态数据源
多数据源配置主要涉及自定义类(DataSource注解类.DataSourceAspect切面类,动态数据源接口实现类.以及数据源字符串线程保存类),pom.xml文件.applicationCont ...
- C++ TCP客户端网络消息发送接收同步实现
废话不多说, 直入主题, 我们在写客户单的时候希望在哪里发消息出去,然后在哪里返回消息(同步), 然后继续往下运行-, 而不是在这里发送了一个消息给服务端, 在另一个地方接受消息(异步) , 也不知道 ...
- Spring Security OAuth2学习
什么是 oAuth oAuth 协议为用户资源的授权提供了一个安全的.开放而又简易的标准.与以往的授权方式不同之处是 oAuth 的授权不会使第三方触及到用户的帐号信息(如用户名与密码),即第三方无需 ...
- COMP222 - 2019
COMP222 - 2019 - Second CA AssignmentIndividual courseworkTrain Deep Learning AgentsAssessment Infor ...
- Java 银联云闪付对接记录
一开始盲目找资料走了弯路: 还是从银联给的官方文档入手最高效: 附件3:云闪付业务商户入网服务指引.pdf http://tomas.test.upcdn.net/pay/%E9%99%84%E4%B ...
- 在秉火STM32F429挑战者开发板上移植Huawei LiteOS指南
昨天在B站上突然看到了一个短视频,是在正点原子的战舰V3开发板上移植华为的Huawei LiteOS操作系统,就是这个视频:看完鸿蒙OS发布会,试用华为的物联网操作系统Lite OS(B站),于是呢, ...