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. 1092 - Lighted Panels

    1092 - Lighted Panels    PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 32 MB ...

  2. 用C++创建Https客户端,用Mingw编译

  3. 【C\C++笔记】register寄存器关键字

    使用寄存器变量提高运行速度 1未使用寄存器组 #include<stdio.h> int main(){ unsigned long a=0; for(int i=0;i<10000 ...

  4. 第三十七个知识点: The Number Field Sieve

    第三十七个知识点: The Number Field Sieve 数域筛法(The Number Field Sieve ,NFS)是已知的分解算法中最有效率的.它的运行时间取决于被分解的数的大小而不 ...

  5. HITCON 2019 Lost Modular again writeup

    HITCON 2019 Lost Modular again writeup 算是基础题,有很多之前题的影子,做不出来纯属菜. 题目 加密脚本 from Crypto.Util.number impo ...

  6. Centos 切换中文输入法

    切换输入法看起来是一个非常简单的操作,但是对于初学者来说,也并非那么简单,开始会发现按Ctrl+space无法切换中文输入法,原因是系统没有安装中文输入法,运行以下命令可以安装中文输入法: yum i ...

  7. Kernel Methods for Deep Learning

    目录 引 主要内容 与深度学习的联系 实验 Cho Y, Saul L K. Kernel Methods for Deep Learning[C]. neural information proce ...

  8. [数据结构]链表LinkList

    目录 1.3 链表 1.3.1 头插法建立单链表 1.3.2 限制链表长度建立单链表 1.3.3 尾插法建立单链表 1.3.4 按序号查找单链表 1.3.5 按值查找单链表 1.3.6 链表的插入 1 ...

  9. Java初学者作业——分别计算两个整数加、减、乘、除的结果并显示,要求除法保留两位小数。

    返回本章节 返回作业目录 需求说明: 分别计算两个整数加.减.乘.除的结果并显示,要求除法保留两位小数. 实现思路: 接收用户控制台输入的两个整数. 实现两个整数的加.减.乘.除的运算并输出结果. 除 ...

  10. Linux 中安装、升级、配置 Swoole 扩展

    从源码编译安装 # 下载Swoole wget http://pecl.php.net/get/swoole-4.5.2.tgz tar -zxvf swoole-4.5.2.tgz cd swool ...