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

示例 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. c程序查找字符出现次数

    #include <stdio.h> int main(){ char str[100],ch,M,Empty; int i, frequency = 0; fgets(str, (siz ...

  2. docker container 的操作

    删除所有退出的容器 docker container rm $(docker ps -aq)

  3. pytest-mark跳过

    import pytestimport sysenvironment='android' @pytest.mark.skipif(environment=="android",re ...

  4. winform textbox 不能唤醒windows 平板的软键盘解决办法

    这个问题,研究了将近两个小时,baidu,google,好大一会,windows 平板本身的应用程序,必须浏览器,notepad都是可以自动唤起软键盘的,但是我的winfrom 程序就是不可以,起先怀 ...

  5. TFS中的账号和GIT中的账号

    有些公司使用TFS来进行git的管理,这个时候很多人没有区分TFS中的账号和GIT的账号的区别:TFS的账号和GIT账号是两套不同的体系,使用TFS管理GIT GIT是不需要账号密码,但是必须在win ...

  6. Tools: CGAL 安装指南

    环境:Microsoft Win7 SP1 Visual Studio 2015 SP3 参考: https://blog.csdn.net/milkpku/article/details/50241 ...

  7. php array remove empty values

    print_r(array_filter($linksArray)); 參考 Remove empty array elements Remove Empty Array Elements In PH ...

  8. Python自学:第四章 复制列表(1)

    # -*- coding: GBK -*- my_foods = ['pizza', 'falafel', 'carrot cake'] friend_foods = my_foods[:] prin ...

  9. C++11中的技术剖析(萃取技术)

    从C++98开始萃取在泛型编程中用的特别多,最经典的莫过于STL.STL中的拷贝首先通过萃取技术识别是否是已知并且支持memcpy类型,如果是则直接通过内存拷贝提高效率,否则就通过类的重载=运算符,相 ...

  10. JavaScript中深拷贝实现

    JavaScript 中深拷贝实现   拷贝时候涉及到: 1.循环结构 2.判断数组 Array 还是对象 Object   函数实现 /** * 获取满足条件的数组中的第一个元素 * @param ...