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 ...
随机推荐
- PAT_A1016#Phone Bills
Source: PAT A1016 Phone Bills (25 分) Description: A long-distance telephone company charges its cust ...
- 拾遗:Go 单元测试
概念 回归测试:是指修改了旧代码之后,重新进行测试,以确保修改没有引入新的错误或导致其它代码产生错误: 单元测试:是指对软件中的最小可测试单元(单个函数或类)进行检查和验证 Test-Driven D ...
- hql 跟 sql 区别
hql 跟 sql 区别 1.hql与sql的区别 sql 面向数据库表查询 hql 面向对象查询 hql : from 后面跟的 类名+类对象 where 后 用对象的属性做条件 sql: fro ...
- Ubuntu12.04开机自动挂载windows分区
最近使用Ubuntu12.04时不知到怎么搞的原本能自动识别的Windows的C .D .E盘突然间无法识别了,于是上网搜了一下Ubuntu12.04下自动挂载Windows NTFS分区的方法. 还 ...
- zabbix4.0自动注册实践
共分为两个步骤: 1.主机zabbix_agent客户端的配置文件 2.主机zabbix_server网页端的自动注册配置 zabbix_agent配置文件 Server=192.168.100.15 ...
- 网址URL知识
URL由三部分组成:资源类型.存放资源的主机域名.资源文件名. URL的一般语法格式为: (带方括号[]的为可选项): protocol :// hostname[:port] / path / [; ...
- vue之事件处理
一.事件处理方法 1.格式 完整格式:v-on:事件名="函数名" 或 v-on:事件名="函数名(参数……)" 缩写格式:@事件名="函数名&qu ...
- docker一键部署zookeeper
version: '3.1' services: zoo1: image: zookeeper:3.4.11 restart: always hostname: zoo1 container_name ...
- Redis Desktop Manager可视化工具连接不上redis
1.在centos中启动redis之后,redis进程也是可查的,但是一连接可视化工具就报错: can't connect to redis-server 2.原因分析: ①首先redis是肯定已经开 ...
- leetcode-685-冗余连接②
题目描述: 参考后提交:并查集: class Solution: def findRedundantDirectedConnection(self, edges: List[List[int]]) - ...