LeetCode241——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 1
Input: "2-1-1"
.
((2-1)-1) = 0
(2-(1-1)) = 2
Output: [0, 2]
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]
实现;
class Solution {
public:
vector<int> diffWaysToCompute(string input) {
vector<int> result;
for (int i = 0; i < input.size(); i++) {
char ch = input[i];
if (ch == '+' || ch == '-' || ch == '*') {
vector<int> lv = diffWaysToCompute(input.substr(0, i));
vector<int> rv = diffWaysToCompute(input.substr(i+1));
for (auto x : lv) {
for (auto y : rv) {
if (ch == '+') {
result.push_back(x+y);
} else if (ch == '*') {
result.push_back(x*y);
} else if (ch == '-') {
result.push_back(x-y);
}
}
}
}
}
if (result.empty()) {
result.push_back(atoi(input.c_str()));
}
return result;
}
};
LeetCode241——Different Ways to Add Parentheses的更多相关文章
- Leetcode241.Different Ways to Add Parentheses为运算表达式设计优先级
给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 1: 输入: "2-1 ...
- 【LeetCode】241. Different Ways to Add Parentheses
Different Ways to Add Parentheses Given a string of numbers and operators, return all possible resul ...
- 241. Different Ways to Add Parentheses
241. Different Ways to Add Parentheses https://leetcode.com/problems/different-ways-to-add-parenthes ...
- 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 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]* ...
- LC 241. Different Ways to Add Parentheses
Given a string of numbers and operators, return all possible results from computing all the differen ...
- [Swift]LeetCode241. 为运算表达式设计优先级 | Different Ways to Add Parentheses
Given a string of numbers and operators, return all possible results from computing all the differen ...
- [LeetCode] 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 添加括号的不同方式
Given a string of numbers and operators, return all possible results from computing all the differen ...
随机推荐
- ubuntu16.04安装teamviewer12
安装teamviewer下载地址:http://www.teamviewer.com/en/download/linux/ 下载的是:teamviewer_12.0.76279_i386.deb ...
- Spring框架系列(四)--IOC控制反转和DI依赖注入
背景: 如果对象的引用或者依赖关系的管理由具体对象完成,代码的耦合性就会很高,代码测试也变得困难.而IOC可以很好的解决这个问题,把这 些依赖关系交给框架或者IOC容器进行管理,简化了开发. IOC是 ...
- 【转】Go语言入门教程(一)Linux下安装Go
说明 系统是Ubuntu. 关于安装 下载安装包 当前官方下载地址是https://golang.org/dl/,如果不能访问,请自行FQ,FQ是技术工作者的必备技能. 安装 tar -xzvf go ...
- maven入门链接
http://www.cnblogs.com/now-fighting/p/4857625.html
- C++ string 是否以‘\0’结尾 讨论
转载https://blog.csdn.net/qq_31930499/article/details/80374310 之前在某篇文章中看到,C语言字符串是以’\0’结尾的,但是C++string类 ...
- 关于 CMSIS 标准 及 STM32F10x的固件库
CMSIS 标准英文全称是Cortex MicroController Software Interface Standard,翻译为中文意思就是 ARM Cortex 微控制器软件接口标准. 由于基 ...
- java 反射运用
一,获取私有的属性,方法,构造器(俗名:暴力反射) 现有一个类,属性,方法,构造器均为私有的,如何创建实例对象,更该属性值,调用方法? public class Student { private S ...
- 模拟退火算fa
转载:http://www.cnblogs.com/heaad/archive/2010/12/20/1911614.html 优化算法入门系列文章目录(更新中): 1. 模拟退火算法 2. 遗传算法 ...
- C语言试题
C语言试题 [说明]: 1.本试题中不考虑头文件引用问题(假定已经包含正确的头文件),C语言的标准函数都可用: 2.如果不特别说明,假定程序运行环境为:操作系统Windows 2000, VC6.0编 ...
- 校长的收藏(洛谷 U4534)
题目背景 XS中学的校长喜欢收集手办,家里面都是价值不菲的手办. 校长喜欢给手办们排队并且对于某些些区间内的手办喜爱有加. 现在,校长外出散步(找乐子),你潜入他的房间打算借(偷走)他的手办炫耀一下. ...