241. 为运算表达式设计优先级

题目大意

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

思路: 分治

我们对于每一个运算符可以考虑它左边可以运算出的值 和 右边可以运算出的值,他们两个再进行运算的值就是结果的可能值

class Solution {
public:
int stoi(string s) {
int cnt = 0;
for(char c: s) {
cnt = cnt * 10 + c - '0';
}
return cnt;
}
vector<int> diffWaysToCompute(string expression) {
vector<int> count;
int len = expression.size();
cout << expression << endl;
for(int i = 0; i < expression.size(); i ++ ) { if(expression[i] == '+' || expression[i] == '-' || expression[i] == '*') {
char c = expression[i];
vector<int> left = diffWaysToCompute(expression.substr(0, i));
vector<int> right = diffWaysToCompute(expression.substr(i + 1));
for(auto l : left)
for(auto r: right) {
if(c == '+')
count.push_back(l + r);
else if(c == '-')
count.push_back(l - r);
else
count.push_back(l * r);
}
}
}
if(count.empty()) {
count.push_back(stoi(expression));
}
return count;
}
};

思路来自:HERODing

LeetCode.241的更多相关文章

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

  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. Java实现 LeetCode 241 为运算表达式设计优先级

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

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

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

  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 241.为运算表达式设计优先级

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

  8. LeetCode 241. Different Ways to Add Parentheses为运算表达式设计优先级 (C++)

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

  9. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

随机推荐

  1. 【剑指Offer】翻转单词顺序列 解题报告(Python)

    [剑指Offer]翻转单词顺序列 解题报告(Python) 标签(空格分隔): 剑指Offer 题目地址:https://www.nowcoder.com/ta/coding-interviews 题 ...

  2. Another kind of Fibonacci(hdu3306)

    Another kind of Fibonacci Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  3. Codeforces A. Orchestra

    A. Orchestra time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  4. 移动端H5-iPhone安全距离适配

    安全区域? 安全区域指的是一个可视窗口范围,处于安全区域的内容不受圆角(corners).齐刘海(sensor housing).小黑条(Home Indicator)影响,如下图蓝色区域: 也就是说 ...

  5. Python 英语单词本

    python pymysql re requests socket库的简单运用 要考试了,这里用所学的知识做一个实例 pymysql库 这个库是用来连接数据库的,使用数据库语句在python里创建表和 ...

  6. 洛谷——P1980 [NOIP2013 普及组] 计数问题

    题目描述 试计算在区间 11 到 nn的所有整数中,数字x(0 ≤ x ≤ 9)x(0≤x≤9)共出现了多少次?例如,在 11到1111中,即在 1,2,3,4,5,6,7,8,9,10,111,2, ...

  7. <数据结构>XDOJ314.完全二叉树的子树

    问题与解答 问题描述 对一棵完全二叉树,采用自上而下.自左往右的方式从1开始编号,我们已知这个二叉树的最后一个结点是n,现在的问题是结点m所在的子树一共包括多少个结点? 输入格式 输入数据包括多行,每 ...

  8. Vue.js高效前端开发 • 【Vue列表渲染】

    全部章节 >>>> 文章目录 一.v-for指令 1.v-for指令使用 2.实践练习(待更新) 二.计算属性 1.计算属性创建和使用 2.实践练习(待更新) 三.侦听属性 1 ...

  9. winform 自定义自动完成控件

    做过前端的朋友肯定很清楚自动完成控件,很多优秀的前端框架都会带有自动完成控件,同样的,winform也有,在我们的TextBox和ComboBox中,只需要设置AutoCompleteSource属性 ...

  10. JMeter_调试取样器(Debug Sampler)

    大家在调试 JMeter 脚本时有没有如下几种需求: 我想知道参数化的变量取值是否正确! 我想知道正则表达式提取器(或json提取器)提取的值是否正确! 我想知道 JMeter 属性! 调试时服务器返 ...