给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果。你需要给出所有可能的组合的结果。有效的运算符号包含 +, - 以及 * 。

示例 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为运算表达式设计优先级的更多相关文章

  1. LeetCode 241. Different Ways to Add Parentheses为运算表达式设计优先级 (C++)

    题目: Given a string of numbers and operators, return all possible results from computing all the diff ...

  2. Leetcode 241.为运算表达式设计优先级

    为运算表达式设计优先级 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 1: 输 ...

  3. Java实现 LeetCode 241 为运算表达式设计优先级

    241. 为运算表达式设计优先级 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 ...

  4. [Swift]LeetCode241. 为运算表达式设计优先级 | Different Ways to Add Parentheses

    Given a string of numbers and operators, return all possible results from computing all the differen ...

  5. LeetCode241——Different Ways to Add Parentheses

    Given a string of numbers and operators, return all possible results from computing all the differen ...

  6. leetcode241 为运算表达式设计优先级

    class Solution(object): def diffWaysToCompute(self, input): """ :type input: str :rty ...

  7. leetcode.分治.241为运算表达式设计优先级-Java

    1. 具体题目 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 1: 输入: & ...

  8. 【LeetCode】241. Different Ways to Add Parentheses

    Different Ways to Add Parentheses Given a string of numbers and operators, return all possible resul ...

  9. 241. Different Ways to Add Parentheses

    241. Different Ways to Add Parentheses https://leetcode.com/problems/different-ways-to-add-parenthes ...

随机推荐

  1. python TypeError: ‘encoding’ is an invalid keyword argument for this function

    shell调用python脚本出现了这个问题,查询原因得知,python脚本是python3.6写的,我们服务器上默认的python是python2.7.3,所以会出现编码问题. 解决思路: 1.安装 ...

  2. 用mybatis进行模糊查询总是查不到结果!

    //IStudentDao.xml @Override public List<Student> selectStudentByName(String name) { SqlSession ...

  3. mysql高效导入导出load data [infile][outfile]用法

    一.MySQL高效导入数据的方法load data infile load data infile语句从一个文本文件中以很高的速度读入一个表中.使用这个命令之前,mysqld进程(服务)必须已经在运行 ...

  4. .net core 根据环境变量区分正式、测试 配置文件

    新建测试.正式环境下所用的 配置信息文件 appsettings.Development.json 文件信息: { "Logging": { "LogLevel" ...

  5. AVR446步进电机算法推导及应用

    https://blog.csdn.net/Renjiankun/article/details/80513839?utm_source=copy

  6. 关于js 重载

    拜读js忍者修炼一书 读到关于js函数重载内容这个模块 主要是介绍通过js的访问argument这个参数来实现js函数的重载 通过在函数内部进行判断js argument参数的长度 代码如下所示 va ...

  7. mimikaz获取明文密码

    一.windows2008以上不保存明文密码解决办法 mimikatz.exe "privilege::debug" "sekurlsa::logonpasswords& ...

  8. u-boot 移植工作目录

    1. 添加工作用户 [root@localhost ~]#useradd -G root -g root -d/home/uboot uboot 2. 建立工作目录 [uboot@localhost ...

  9. Python自学:第四章 遍历切片

    # -*- coding: GBK -*- players = ['charles', 'martina', 'michael', 'florence', 'eli'] print("Her ...

  10. Ruby 环境

    Ruby 环境 本地环境设置 如果您想要设置 Ruby 编程语言的环境,请阅读本章节的内容.本章将向您讲解与环境设置有关的所有重要的主题.建议先学习下面几个主题,然后再进一步深入学习其他主题: Lin ...