题目:

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. 零基础入门 实战mpvue2.0多端小程序框架

    第1章 课程快速预览(必看!!!)在这一章节中,老师讲带领你快速预览课程整体.其中,涉及到为什么要做这么一门实战课程.制作一个小程序的完整流程是怎么样的,以及如何做项目的技术选型. 第2章 30 分钟 ...

  2. IDEA取消SVN关联 , 在重新分享项目

    IDEA取消SVN关联,在重新分享项目     安装插件 1.打开Intellij中工具栏File的setting(ctrl+alt+s),选择plugins,在右边搜索框输入“SVN”,搜索.选择“ ...

  3. 【CometOJ】Comet OJ - Contest #8 解题报告

    点此进入比赛 \(A\):杀手皇后(点此看题面) 大致题意: 求字典序最小的字符串. 一场比赛总有送分题... #include<bits/stdc++.h> #define Tp tem ...

  4. windows端口转发工具(LCX)

    端口转发(Port forwarding),有时被叫做隧道,是安全壳(SSH) 为网络安全通信使用的一种方法.端口转发是转发一个网络端口从一个网络节点到另一个网络节点的行为,其使一个外部用户从外部经过 ...

  5. centos7下安装php-memcached扩展

    -> https://blog.csdn.net/sinat_35861664/article/details/72831556 安装扩展进行编译时如果报错,则将 ./configure --w ...

  6. QAxBase: Error calling IDispatch member LineStyle: Unknown error

    word/Excel版本2007.2010.  wps也适用. //borders->dynamicCall("SetLineStyle(int,int,int)", 0, ...

  7. 不支持中国移动的N79频段,红米K30是假5G手机么?影响有多大?

    原文:https://mparticle.uc.cn/article.html?uc_param_str=frdnsnpfvecpntnwprdssskt&btifl=100&app= ...

  8. redis之HyperLogLog

    HyperLogLog 提供不精确的去重计数方案,虽然不精确但是也不是非常不精确,标准误差是 0.81%. 使用方法 HyperLogLog 提供了两个指令 pfadd 和 pfcount,根据字面意 ...

  9. Date以及LocalDateTime格式化

    public static void main(String[] args) { LocalDateTime local = LocalDateTime.now(); Date date = new ...

  10. MongoDB官方下载安装设置配置文件指定端口号

    1.)下载 官网(https://www.mongodb.com/)右上角try free  进入下载中心,下载指定版本 ZIP和MSI随便 如果浏览器下载的慢,可以直接使用下载地址,然后迅雷下 操作 ...