题目:

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"
Output: [0, 2]
Explanation:
((2-1)-1) = 0
(2-(1-1)) = 2

Example 2:

Input: "2*3-4*5"
Output: [-34, -14, -10, -10, 10]
Explanation:
(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

分析:

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

以运算符号为界限来划分出左右两个子串,继续递归执行子串,直到只有一个元素为止。

左右两个子串的结果存进数组中,对其中的元素遍历组合得到结果。

diff(2*3-4*5) = { diff(2) * diff(3-4*5) } + { diff(2*3) - diff(4*5) } + { diff(2*3-4) * diff(5) }

其中diff(3-4*5) = {diff(3) - diff(4*5),diff(3-4) * diff(5)}={3-20,-1*5}={-17,-5}

diff(2*3-4) = {diff(2) * diff(3-4),diff(2*3) - diff(4)} = {2*-1,6-4} = {-2,2}

所以diff(2*3-4*5) = {2*{-17,-5}}+{6-20}+{{-2,2}*5}={-34,-10}+{-14}+{-10,10}={-34,-10,-14,-10,10}

程序:

class Solution {
public:
vector<int> diffWaysToCompute(string input) {
vector<int> res;
for(int i = ; i < input.size(); ++i){
if(input[i] == '+' || input[i] == '-' || input[i] == '*'){
vector<int> l = diffWaysToCompute(input.substr(, i));
vector<int> r = diffWaysToCompute(input.substr(i+, input.size()-i)); for(auto p:l)
for(auto q:r){
if(input[i] == '+')
res.push_back(p+q);
if(input[i] == '-')
res.push_back(p-q);
if(input[i] == '*')
res.push_back(p*q);
}
}
}
if(res.empty())
res.push_back(stoi(input));
return res;
}
};

LeetCode 241. Different Ways to Add Parentheses为运算表达式设计优先级 (C++)的更多相关文章

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

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

  2. 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 ...

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

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

  4. (medium)LeetCode 241.Different Ways to Add Parentheses

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

  5. leetcode@ [241] Different Ways to Add Parentheses (Divide and Conquer)

    https://leetcode.com/problems/different-ways-to-add-parentheses/ Given a string of numbers and opera ...

  6. [LeetCode#241]Different Ways to Add Parentheses

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

  7. 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]* ...

  8. 241. Different Ways to Add Parentheses

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

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

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

随机推荐

  1. 2019 SDN上机第四次作业

    2019 SDN上机第4次作业 1. 解压安装OpenDayLight控制器(本次实验统一使用Beryllium版本) 修改环境变量 2. 启动并安装插件 3. 用Python脚本搭建如下拓扑,连接O ...

  2. 【2019.8.12 慈溪模拟赛 T2】汪哥图(wang)(前缀和)

    森林 考虑到题目中给出条件两点间至多只有一条路径. 就可以发现,这是一个森林. 而森林有一个很有用的性质. 考虑对于一棵树,点数-边数=\(1\). 因此对于一个森林,点数-边数=连通块个数. 所以, ...

  3. x3

    #include<stdio.h> int main() { char ch; printf("输入一个字符:\n"); scanf("%c",&a ...

  4. Paper | Model-blind video denoising via frame-to-frame training

    目录 故事 本文方法 流程 训练 实验 发表在2019年CVPR. 核心内容:基于Noise2Noise思想,这篇文章致力于无监督的视频盲去噪:是的,连噪声样本都不需要了. 这篇文章写作和概括太棒了! ...

  5. 《一起学mysql》3

    索引和查询优化   为什么要索引? 想想我们上小学的时候是怎么查字典的,比方查 理想的 “理”,首先在索引里找到声母 “l”,再找到 “li” 找到 “li”所在的页数,   我们之前建的所有mysq ...

  6. Loj #2568. 「APIO2016」烟花表演

    Loj #2568. 「APIO2016」烟花表演 题目描述 烟花表演是最引人注目的节日活动之一.在表演中,所有的烟花必须同时爆炸.为了确保安全,烟花被安置在远离开关的位置上,通过一些导火索与开关相连 ...

  7. ECMAScript 6.0 简要学习

    由于在学习vue的时候有许多自己不懂的语法,于是简单的学习一下ES6. 1.ES简介 ES6, 全称 ECMAScript 6.0 ,是 JavaScript 的下一个版本标准,2015.06 发版. ...

  8. SpringCloud之Eureka详细的配置

    介绍 SpringCloud是一个完整的微服务治理框架,包括服务发现和注册,服务网关,熔断,限流,负载均衡和链路跟踪等组件. SpringCloud-Eureka主要提供服务注册和发现功能.本文提供了 ...

  9. 【题解】ADAGRAFT - Ada and Graft [SP33331]

    [题解]ADAGRAFT - Ada and Graft [SP33331] 传送门:\(\text{Ada and Graft}\) \(\text{[SP33331]}\) [题目描述] 给出一颗 ...

  10. Logstash:Data转换,分析,提取,丰富及核心操作

    Logstash:Data转换,分析,提取,丰富及核心操作 Logstash plugins Logstash是一个非常容易进行扩张的框架.它可以对各种的数据进行分析处理.这依赖于目前提供的超过200 ...