当前位置:网站首页>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
边栏推荐
- 记一次 .NET 差旅管理后台 CPU 爆高分析
- Yyds dry inventory ravendb start client API (III)
- Mipi interface, DVP interface and CSI interface of camera [easy to understand]
- AI training speed breaks Moore's law; Song shuran's team won the RSS 2022 Best Paper Award
- Leetcode-160 intersecting linked list
- AppGallery Connect场景化开发实战—图片存储分享
- Openai video pre training (VPT): action learning based on watching unmarked online videos
- Lumiprobe 自由基分析丨H2DCFDA说明书
- 数商云:从规划到落地,五矿集团如何快速构建数字化发展新格局?
- 混沌工程平台 ChaosBlade-Box 新版重磅发布
猜你喜欢
云服务器ECS夏日省钱秘籍,这次@老用户快来领走
Li Kou daily question - Day 32 -589 N × Preorder traversal of tree
Getting started with kubernetes command (namespaces, pods)
寶,運維100+服務器很頭疼怎麼辦?用行雲管家!
Leetcode203 remove linked list elements
Lumiprobe free radical analysis h2dcfda instructions
Games202 operation 0 - environment building process & solving problems encountered
ACM mm 2022 video understanding challenge video classification track champion autox team technology sharing
Example explanation: move graph explorer to jupyterlab
bean的生命周期核心步骤总结
随机推荐
Prices of Apple products rose across the board in Japan, with iphone13 up 19%
Qfile read / write file operation in QT
Database foundation: select basic query statement
Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
Huawei cloud experts explain the new features of gaussdb (for MySQL)
Lake Shore低温恒温器的氦气传输线
Technical secrets of ByteDance data platform: implementation and optimization of complex query based on Clickhouse
[AGC] how to solve the problem that the local display of event analysis data is inconsistent with that in AGC panel?
Once the SQL is optimized, the database query speed is increased by 60 times
Lake Shore - crx-em-hf low temperature probe station
Lumiprobe free radical analysis h2dcfda instructions
太爱速M源码搭建,巅峰小店APP溢价寄卖源码分享
AI training speed breaks Moore's law; Song shuran's team won the RSS 2022 Best Paper Award
2. Create your own NFT collections and publish a Web3 application to show them start and run your local environment
ES6 summary "suggestions collection" of array methods find(), findindex()
linux下清理系统缓存并释放内存
Lake Shore—OptiMag 超导磁体系统 — OM 系列
Improve yolov5 with gsconv+slim neck to maximize performance!
Create your own NFT collections and publish a Web3 application to show them (Introduction)
The former 4A executives engaged in agent operation and won an IPO