当前位置:网站首页>241. Different Ways to Add Parentheses
241. Different Ways to Add Parentheses
2022-07-01 18:43:00 【SUNNY_CHANGQI】
The description of the problem
Given a string experssion of numbers and operators, return
all possible results from computing all the different
possible ways to group number and operators.
You may return the answer in any order.
The test cases are generated such that the output values fit in a 32 bit integer and the number of different results does not exceed $10^4$
an example
Input: expression = "2-1-1"
Output: [0,2]
Explanation:
((2-1)-1) = 0
(2-(1-1)) = 2
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/different-ways-to-add-parentheses
The codes for this
#include <vector>
#include <iostream>
#include <string>
using namespace std;
class Solution {
public:
vector<int> diffWaysToCompute(string expression) {
vector<int> res;
int n = expression.size();
for (int i = 0; i < n; i++) {
if (expression[i] == '+' || expression[i] == '-' || expression[i] == '*') {
vector<int> left = diffWaysToCompute(expression.substr(0,i));
vector<int> right = diffWaysToCompute(expression.substr(i + 1));
for (int l : left) {
for (int r : right) {
switch (expression[i]) {
case '+' : res.emplace_back(l + r); break;
case '-' : res.emplace_back(l - r); break;
case '*' : res.emplace_back(l * r); break;
}
}
}
}
}
if (res.empty()) {
res.emplace_back(stoi(expression));
}
return res;
}
};
int main()
{
Solution s;
string s1 = "2-1-1";
vector<int> res = s.diffWaysToCompute(s1);
for (int i : res) {
cout << i << " ";
}
cout << endl;
return 0;
}
The corresponding results
$ ./test
2 0
边栏推荐
- R language ggplot2 visualization: gganimate creates a dynamic histogram animation (GIF), and displays the histogram and enter step by step along a given dimension in the animation_ Growth function and
- 中英说明书丨人可溶性晚期糖基化终末产物受体(sRAGE)Elisa试剂盒
- Leetcode-128 longest continuous sequence
- 记一次 .NET 差旅管理后台 CPU 爆高分析
- Yyds dry inventory ravendb start client API (III)
- Supervarimag superconducting magnet system SVM series
- 【快应用】text组件里的文字很多,旁边的div样式会被拉伸如何解决
- Cdga | if you are engaged in the communication industry, you should get a data management certificate
- Lumiprobe 活性染料丨吲哚菁绿说明书
- Specification of lumiprobe reactive dye indocyanine green
猜你喜欢

Lake Shore—OptiMag 超导磁体系统 — OM 系列

水产行业智能供应链管理平台解决方案:支撑企业供应链数字化,提升企业管理效益

苹果产品在日本全面涨价,iPhone13涨19%

Is PMP cancelled??

毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?

Lake Shore—CRX-EM-HF 型低温探针台

PMP是被取消了吗??

Games202 operation 0 - environment building process & solving problems encountered

Viewing technological changes through Huawei Corps (VI): smart highway

11、用户、组和权限(1)
随机推荐
Viewing the whole ecology of Tiktok from a macro perspective
bean的生命周期核心步骤总结
Example explanation: move graph explorer to jupyterlab
11. Users, groups, and permissions (1)
寶,運維100+服務器很頭疼怎麼辦?用行雲管家!
docker 部署mysql8.0
一次SQL优化,数据库查询速度提升 60 倍
Huawei game failed to initialize init with error code 907135000
Lake shore optimag superconducting magnet system om series
6月刊 | AntDB数据库参与编写《数据库发展研究报告》 亮相信创产业榜单
苹果产品在日本全面涨价,iPhone13涨19%
小红书上的爱情买卖
kubernetes命令入门(namespaces,pods)
PriorityQueue的用法和底层实现原理
MySQL常用图形管理工具 | 黑马程序员
数据仓库(四)之ETL开发
太爱速M源码搭建,巅峰小店APP溢价寄卖源码分享
中英说明书丨人可溶性晚期糖基化终末产物受体(sRAGE)Elisa试剂盒
linux下清理系统缓存并释放内存
ACM mm 2022 video understanding challenge video classification track champion autox team technology sharing