Leetcode241.Different Ways to Add Parentheses为运算表达式设计优先级
给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果。你需要给出所有可能的组合的结果。有效的运算符号包含 +, - 以及 * 。
示例 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
采用了分治的思想和方法。
分治法就是将一个大规模的问题分成n个小规模的问题。
这些问题相互独立(小问题之间如何解决不会相互影响),且小问题的问题性质与大问题的性质相同。
通过小问题的解,得出大问题的解。
该问题可以将长的字符串分成短的字符串,以运算符为分界,将字符串一分为二,以只含单个整数的字符作为分界的终点。
因为运算符不止一个,所以若干(1,2,3...)个运算符顺序确定的情况下,剩下的运算符会有多解的情况。
class Solution {
public:
vector<int> diffWaysToCompute(string input)
{
vector<int> res;
for (int i = 0; i < input.size(); i++)
{
if (input[i] == '+' || input[i] == '-' || input[i] == '*')
{
vector<int> left = diffWaysToCompute(input.substr(0, i));
vector<int> right = diffWaysToCompute(input.substr(i + 1));
for (int a : left)
{
for (int b : right)
{
switch (input[i])
{
case '+':
res.push_back(a + b);
break;
case '-':
res.push_back(a - b);
break;
default:
res.push_back(a * b);
break;
}
}
}
}
}
/*if (input.size() == 1)
res.push_back((int)(input[0] - '0'));*/
if (res.empty())
{
res.push_back(atoi(input.c_str()));
}
return res;
}
};
附:
string sub = s.substr(5); //只有一个数字5表示从下标为5开始一直到结尾;
string sub = s.substr(5, 3); //从下标为5开始截取长度为3位;
const char *c_str();
c_str()函数返回一个指向正规C字符串的指针常量, 内容与本string串相同.
这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。
int atoi(const char* str)
参数str是要转换的字符串,返回值是转换后的整数。
Leetcode241.Different Ways to Add Parentheses为运算表达式设计优先级的更多相关文章
- LeetCode 241. Different Ways to Add Parentheses为运算表达式设计优先级 (C++)
题目: Given a string of numbers and operators, return all possible results from computing all the diff ...
- Leetcode 241.为运算表达式设计优先级
为运算表达式设计优先级 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 1: 输 ...
- Java实现 LeetCode 241 为运算表达式设计优先级
241. 为运算表达式设计优先级 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 ...
- [Swift]LeetCode241. 为运算表达式设计优先级 | Different Ways to Add Parentheses
Given a string of numbers and operators, return all possible results from computing all the differen ...
- LeetCode241——Different Ways to Add Parentheses
Given a string of numbers and operators, return all possible results from computing all the differen ...
- leetcode241 为运算表达式设计优先级
class Solution(object): def diffWaysToCompute(self, input): """ :type input: str :rty ...
- leetcode.分治.241为运算表达式设计优先级-Java
1. 具体题目 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 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 ...
随机推荐
- vue 笔记,ref 及 $event 事件对象
本文仅用作简单记录 ref : 在标签上添加 ref = “name” ,表示获取当前元素节点 <input type="text" ref="info" ...
- JAR API
JAR API包括使用 manifest 文件的类.Manifest类的一个对象表示一个manifest文件. 在代码中创建一个Manifest对象,如下所示: 1 Manifest manifest ...
- 【Linux】- Systemd 实战篇
转自:阮一峰的网络日志 一.开机启动 对于那些支持 Systemd 的软件,安装的时候,会自动在/usr/lib/systemd/system目录添加一个配置文件. 如果你想让该软件开机启动,就执行下 ...
- leetcode.数组.283移动零-Java
1. 具体题目 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须 ...
- MacOS安装npm全局包的权限问题
MacOS,安装npm全局包提示没有写入权限: npm WARN checkPermissions Missing write access to /usr/local/lib/node_module ...
- Java类与类的关系、继承与多态、重写与重载
Java类与类的关系 (1)is-a包括了 继承,实现关系 (2)has-a包括了 关联,聚合,组合关系 (3)use-a包括了 依赖关系 实现关系: 实现指的是一个class类实现interface ...
- 随时更新web html 项目页面,查看手机等其他移动设备的几种方法?
想一想自己一边写着代码,一边随时看着手机更新页面,对于前端开发者来说是不是爽歪歪: 实现以上效果只需要几个方法: 1) 安装虚拟机 Oracle VM VirtualBox (安装过程省略) 2) 安 ...
- 微信小程序picker下拉绑定数据
页面部分 <picker mode = "selector" bindchange="bindPickerChange" value="{{pr ...
- runtime和runloop问答
Runtime 01 问题: objc在向一个对象发送消息时,发生了什么? 解答: 根据对象的 isa 指针找到类对象 id,在查询类对象里面的 methodLists 方法函数列表,如果没有在好到, ...
- vim 中 ctags的应用
为了方便查询代码段中代码的最终的定义 在linux的vim便以其中可以使用ctags 使用步骤: 1. 安装 ctags : sudo apt-get install ctags 2. 生 ...