Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are+- and *.

Example 1

Input: "2-1-1".

((2-1)-1) = 0
(2-(1-1)) = 2

Output: [0, 2]

Example 2

Input: "2*3-4*5"

(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

Output: [-34, -14, -10, -10, 10]

实现;

class Solution {

public:

    vector<int> diffWaysToCompute(string input) {

        vector<int> result;

        for (int i = 0; i < input.size(); i++) {

            char ch = input[i];

            if (ch == '+' || ch == '-' || ch == '*') {

                vector<int> lv = diffWaysToCompute(input.substr(0, i));

                vector<int> rv = diffWaysToCompute(input.substr(i+1));

                for (auto x : lv) {

                    for (auto y : rv) {

                        if (ch == '+') {

                            result.push_back(x+y);

                        } else if (ch == '*') {

                            result.push_back(x*y);

                        } else if (ch == '-') {

                            result.push_back(x-y);

                        }

                    }

                }

            }

        }

        if (result.empty()) {

            result.push_back(atoi(input.c_str()));

        }

        return result;

    }

};

LeetCode241——Different Ways to Add Parentheses的更多相关文章

  1. Leetcode241.Different Ways to Add Parentheses为运算表达式设计优先级

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

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

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

  3. 241. Different Ways to Add Parentheses

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

  4. LN : leetcode 241 Different Ways to Add Parentheses

    lc 241 Different Ways to Add Parentheses 241 Different Ways to Add Parentheses Given a string of num ...

  5. leetcode 96. Unique Binary Search Trees 、95. Unique Binary Search Trees II 、241. Different Ways to Add Parentheses

    96. Unique Binary Search Trees https://www.cnblogs.com/grandyang/p/4299608.html 3由dp[1]*dp[1].dp[0]* ...

  6. LC 241. Different Ways to Add Parentheses

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

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

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

  8. [LeetCode] Different Ways to Add Parentheses 添加括号的不同方式

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

  9. [LeetCode] 241. Different Ways to Add Parentheses 添加括号的不同方式

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

随机推荐

  1. 通过Maven将指定Jar包下载到指定的本地目录

    现在大家大部分都通过Maven等工具来管理包,但是特殊情况下还是需要将包下载到本地.我们可以通过maven命令来完成这个需求.创建一个pom.xml文件,文件内容如下: <?xml versio ...

  2. 网络编程基础_3.APC队列

    APC队列 #include <stdio.h> #include <windows.h> // 保存 IO 操作的结果 CHAR Buffer1[] = { }; CHAR ...

  3. Jmeter之JDBC请求参数化(一)

    一.环境准备 a.jmeter5.1.1版本最新版本,可以去网页下载:https://jmeter.apache.org/download_jmeter.cgi b.jdbc驱动:链接:https:/ ...

  4. nfs服务权限配置

    nfs服务权限配置 1. 查看系统是否已经安装了服务Rpm -qa | grep nfs 2. 启动服务,并且开机自动运行Systemctl start nfsSystemctl enabled nf ...

  5. selenium爬虫设置headers,代理IP等方法

    https://blog.csdn.net/xc_zhou/article/details/80823855

  6. [转]SSD固态存储大观(一)

    From: http://blog.51cto.com/alanwu/1405874 Contents 1.概述... 1 2.FusionIO:Pcie SSD的始作俑者... 2 3.Intel ...

  7. Xmind的使用

    Xmind是用来学习整理思维的工具

  8. UVA - 1374 Power Calculus (dfs迭代加深搜索)

    题目: 输入正整数n(1≤n≤1000),问最少需要几次乘除法可以从x得到xn ?在计算过程中x的指数应当总是正整数. 思路: dfs枚举次数深搜 注意: 1.指数如果小于0,就退出当前的搜索 2.n ...

  9. Linux之iptables(五、firewall命令及配置)

    firewalld服务 firewalld是CentOS 7.0新推出的管理netfilter的工具 firewalld是配置和监控防火墙规则的系统守护进程.可以实现iptables,ip6table ...

  10. python3中post请求里带list报错

    这个post请求的数据太长,一般data=,json=就够了. 但是今天这个一直报错,用json吧,报缺少参数,用data吧,报多余[. 后来改成data=,并把数据中的[] 用引号括起来," ...