当前位置:网站首页>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
边栏推荐
- Lake shore M91 fast hall measuring instrument
- Lake Shore M91快速霍尔测量仪
- 【6.24-7.1】写作社区精彩技术博文回顾
- Lumiprobe 自由基分析丨H2DCFDA说明书
- CDGA|从事通信行业,那你应该考个数据管理证书
- 有关 M91 快速霍尔测量仪的更多信息
- C-end dream is difficult to achieve. What does iFLYTEK rely on to support the goal of 1billion users?
- 宝,运维100+服务器很头疼怎么办?用行云管家!
- web开发常用的开源框架的开源协议整理
- 前4A高管搞代运营,拿下一个IPO
猜你喜欢

Altair HyperWorks 2022 software installation package and installation tutorial

白盒加密技术浅理解

Solidity - 算术运算的截断模式(unchecked)与检查模式(checked)- 0.8.0新特性

论文阅读【Learning to Discretely Compose Reasoning Module Networks for Video Captioning】

How to use the low code platform of the Internet of things for personal settings?

智慧防疫系统为建筑工地复工复产提供安全保障

Lake Shore低温恒温器的氦气传输线

Cdga | if you are engaged in the communication industry, you should get a data management certificate

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

【森城市】GIS数据漫谈(一)
随机推荐
市值蒸发740亿,这位大佬转身杀入预制菜
华为联机对战服务玩家掉线重连案例总结
Contos 7 set up SFTP to create users, user groups, and delete users
见证时代!“人玑协同 未来已来”2022弘玑生态伙伴大会开启直播预约
Prices of Apple products rose across the board in Japan, with iphone13 up 19%
The former 4A executives engaged in agent operation and won an IPO
M91快速霍尔测量仪—在更短的时间内进行更好的测量
[6.24-7.1] review of wonderful technical blog posts in the writing community
中英说明书丨人可溶性晚期糖基化终末产物受体(sRAGE)Elisa试剂盒
AI training speed breaks Moore's law; Song shuran's team won the RSS 2022 Best Paper Award
助力数字经济发展,夯实数字人才底座—数字人才大赛在昆成功举办
Viewing technological changes through Huawei Corps (VI): smart highway
Huawei cloud experts explain the new features of gaussdb (for MySQL)
线程的并行、并发、生命周期
Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
Dlib+opencv library for fatigue detection
Clean up system cache and free memory under Linux
Improve yolov5 with gsconv+slim neck to maximize performance!
The difference between indexof and includes
Nacos configuration file publishing failed, please check whether the parameters are correct solution