当前位置:网站首页>241. Different Ways to Add Parentheses
241. Different Ways to Add Parentheses
2022-07-01 19:25: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
source : Power button (LeetCode)
link :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
边栏推荐
- Is PMP cancelled??
- Three ways for redis to realize current limiting
- Altair HyperWorks 2022 software installation package and installation tutorial
- 学习笔记-JDBC连接数据库操作的步骤
- 【pytorch记录】自动混合精度训练 torch.cuda.amp
- 241. Different Ways to Add Parentheses
- ACM mm 2022 video understanding challenge video classification track champion autox team technology sharing
- 论文阅读【Discriminative Latent Semantic Graph for Video Captioning】
- 中英说明书丨人可溶性晚期糖基化终末产物受体(sRAGE)Elisa试剂盒
- Love business in Little Red Book
猜你喜欢
Witness the times! "The future of Renji collaboration has come" 2022 Hongji ecological partnership conference opens live broadcast reservation
机械设备行业数字化供应链集采平台解决方案:优化资源配置,实现降本增效
Solidity - 算术运算的截断模式(unchecked)与检查模式(checked)- 0.8.0新特性
Enabling "new Chinese enterprises", SAP process automation landing in China
Specification of lumiprobe reactive dye indocyanine green
3. "Create your own NFT collections and publish a Web3 application to show them" cast NFT locally
案例分享:QinQ基本组网配置
Love business in Little Red Book
宝,运维100+服务器很头疼怎么办?用行云管家!
数商云:从规划到落地,五矿集团如何快速构建数字化发展新格局?
随机推荐
白盒加密技术浅理解
小红书上的爱情买卖
DTD建模
Lake Shore M91快速霍尔测量仪
Learn MySQL from scratch - database and data table operations
The former 4A executives engaged in agent operation and won an IPO
数商云:从规划到落地,五矿集团如何快速构建数字化发展新格局?
MFC中如何重绘CListCtrl的表头
Lake Shore continuous flow cryostat transmission line
Go语言高级
Learning notes - steps of JDBC connection database operation
Chaos engineering platform chaosblade box new heavy release
[AGC] how to solve the problem that the local display of event analysis data is inconsistent with that in AGC panel?
M91 fast hall measuring instrument - better measurement in a shorter time
MATLAB中subplot函数的使用
How to use the low code platform of the Internet of things for personal settings?
June issue | antdb database participated in the preparation of the "Database Development Research Report" and appeared on the list of information technology and entrepreneurship industries
The best landing practice of cave state in an Internet ⽹⾦ financial technology enterprise
Cdga | if you are engaged in the communication industry, you should get a data management certificate
nacos配置文件发布失败,请检查参数是否正确的解决方案