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. 1307 - Counting Triangles

    1307 - Counting Triangles    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 ...

  2. Codeforces 1073C:Vasya and Robot(二分)

    C. Vasya and Robot time limit per test: 1 secondmemory limit per test: 256 megabytesinput: standard ...

  3. 13 个 C# 10 特性

    原文链接:https://blog.okyrylchuk.dev 原文作者:Oleg Kyrylchuk 译: 等天黑 常量的内插字符串 C# 10 允许使用在常量字符串初始化中使用插值, 如下 co ...

  4. BBN: Bilateral-Branch Network with Cumulative Learning for Long-Tailed Visual Recognition

    BBN: Bilateral-Branch Network with Cumulative Learning for Long-Tailed Visual Recognition 目录 BBN: Bi ...

  5. Mysql数据库服务端的安装

    一般提到Mysql数据库的安装在工作当中是说的安装数据库管理软件的服务端,服务端的安装可以安装在Windows环境,也可以安装在Linux环境. Windows环境安装:目前安装比较流行的是5.7,增 ...

  6. JavaScript交互式网页设计 • 【第6章 初识jQuery】

    全部章节   >>>> 本章目录 6.1 jQuery概述 6.1.1 初识 jQuery 6.1.2 jQuery 基本功能 6.1.3 搭建 jQuery 开发环境 6.1 ...

  7. Win10 开启 Hyper-V 及简单使用

    简介 Windows 10 上内置了 Hyper-V.Hyper-V 提供硬件虚拟化,每个虚拟机都在虚拟硬件上运行. 系统要求 Windows 10 企业版.专业版或教育版.家庭版.移动版.移动企业版 ...

  8. JavaScript 钩子

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <script s ...

  9. scp 文件传输

    1.推送 scp -r imageAPP/ root@ip:/data/soft/ 本地当前文件下的 imageAPP文件 推送到ip服务器 /data/soft/ 目录下 2.拉取 scp -r r ...

  10. [ bootstrap ] 实现卡片里面包含图片、内容、操作按钮,形成左中右的布局

    描述: 如图 实现: <div class="card border-0 mb-3 mt-3"> <!-- 列表头部 --> <div class=& ...