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. Functions of the call stack

    https://en.wikipedia.org/wiki/Call_stack#STACK-FRAME As noted above, the primary purpose of a call s ...

  2. Xcode5编译ffmpeg

    命令行安装FFmpeg:git clone git://source.ffmpeg.org/ffmpeg.git ffmpeg(或:到https://github.com/gabriel/ffmpeg ...

  3. STL中栈stack的用法

    头文件: #include <stack> 建立一个栈stack < 类型 > s //例如 stack<int> s 加入一个新的元素s.push( a ) 询问 ...

  4. restful风格url Get请求查询所有和根据id查询的合并成一个controller

    restful风格url Get请求查询所有和根据id查询的合并成一个controller的方法 原代码 // 127.0.0.1:8080/dep/s @ApiOperation(value=&qu ...

  5. LINUX-JPS工具

    JPS工具 jps(Java Virtual Machine Process Status Tool)是JDK 1.5提供的一个显示当前所有java进程pid的命令,简单实用,非常适合在linux/u ...

  6. SQL Server 机考,用T-SQL编写 简单实例

    使用T-SQL实现以下要求: 要求如下: 1,添加数据库:MySchool 2,添加学生基础表:Student 3,添加学生成绩表:ScoreInfo 4,两张表结构分别如下 Student表结构:( ...

  7. 【模板】51nod 1051 最大子矩阵和

    [题解] 二重循环枚举起始列和终止列,竖着往下加,转化为一个最大子段和问题,逐行累加即可. #include<cstdio> #include<cstring> #includ ...

  8. Stones HDU 1896

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1896 题目大意: 有n个石头,每个石头有:p  它所在的位置 ,d  它能扔多远 从0 开始,遇到第奇 ...

  9. 别墅房间 CodeVS原创

    时间限制: 1 s 空间限制: 64000 KB 题目等级 : 青铜 Bronze 题目描述 Description 小浣熊松松到他的朋友家别墅去玩,发现他朋友的家非常大,而且布局很奇怪.具体来说,朋 ...

  10. So easy

    Problem Description Small W gets two files. There are n integers in each file. Small W wants to know ...