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

示例 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. REST Client实际应用记录

    请求Content-Type为application/x-www-form-urlencoded 先来看一个完整示例: ############## ### qa问答 @msg="糖尿病患者 ...

  2. 【HDUOJ】几道递推DP

    就不写题解了.很基础的递推. HDU2084数塔 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2084 代码: #include <iostre ...

  3. redis数据结构之SDS

    简介 redis源码虽然是C语言实现的,但是Redis没有直接采用C语言传统的字符串表示,而是构建了一种名叫简单动态字符串(simple dynamic string,SDS)的抽象类型,并将SDS用 ...

  4. python中 try、except、finally执行顺序

    我们虽然经常用到try...except 作为异常补货,但是其实很少去研究try源码和机制,也许点进去看过,也是看不出个所以然来 class Exception(BaseException): &qu ...

  5. Ubuntu Server 19配置静态IP

    这个/etc/netplan下默认有个文件50-cloud-init.yaml,直接修改它就行了 sudo vim /etc/netplan/50-cloud-init.yaml 网口名字ens33可 ...

  6. *args和**kwargs用法

    ''' @Date: 2019-10-10 21:14:56 @LastEditors: 冯浩 @LastEditTime: 2019-10-20 22:38:16 ''' def func(*arg ...

  7. NX二次开发-UFUN更改图纸页比例UF_DRAW_set_drawing_info

    #include <uf.h> #include <uf_draw.h> #include <uf_part.h> UF_initialize(); //获得当前图 ...

  8. 知识整理:字符串hash

    字符串hash唯一用途是快速判断两字符串是否相等,但存在极小概率假阳性(本来不相等,但算法返回相等). 根本思想是把一个字符串转换为一个整数,要求相同的字符串,对应的这个整数相同,不同的字符串,对应的 ...

  9. hdu多校第八场 1009 (hdu6665) Calabash and Landlord 计算几何/dfs

    题意: 给定两个矩形,输出这两个矩形把平面分成了多少块. 题解: 本来是道计算几何的大讨论,被我生生写成了bfs. 离散化边,注意不重合的边中间要空出来一格,四周也要空出来一圈,然后暴力bfs计算一共 ...

  10. Matlab求三重积分

    Matlab求三重积分 求 \(\int_0^1 \int_0^1 \int_0^1 sin(\pi x_1 x_2 x_3) dx_1 dx_2 dx_3\) 代码是: triplequad(@(x ...