当前位置:网站首页>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 continuous flow cryostat transmission line
- PMP是被取消了吗??
- ECS summer money saving secret, this time @ old users come and take it away
- Dlib+opencv library for fatigue detection
- 3. "Create your own NFT collections and publish a Web3 application to show them" cast NFT locally
- 实现一个Prometheus exporter
- 机械设备行业数字化供应链集采平台解决方案:优化资源配置,实现降本增效
- indexof和includes的区别
- 生鲜行业B2B电商平台解决方案,提高企业交易流程标准化和透明度
- 苹果产品在日本全面涨价,iPhone13涨19%
猜你喜欢
Love business in Little Red Book
Enabling "new Chinese enterprises", SAP process automation landing in China
使用环信提供的uni-app Demo,快速实现一对一单聊
Viewing technological changes through Huawei Corps (VI): smart highway
案例分享:QinQ基本组网配置
Manufacturing SRM management system supplier all-round closed-loop management, to achieve procurement sourcing and process efficient collaboration
Supervarimag superconducting magnet system SVM series
Lake Shore M91快速霍尔测量仪
Example explanation: move graph explorer to jupyterlab
Lake Shore低温恒温器的氦气传输线
随机推荐
Appgallery connect scenario development practice - image storage and sharing
Lumiprobe 自由基分析丨H2DCFDA说明书
AI training speed breaks Moore's law; Song shuran's team won the RSS 2022 Best Paper Award
【6.24-7.1】写作社区精彩技术博文回顾
M91快速霍尔测量仪—在更短的时间内进行更好的测量
Example explanation: move graph explorer to jupyterlab
Learn MySQL from scratch - database and data table operations
Huawei game failed to initialize init with error code 907135000
助力数字经济发展,夯实数字人才底座—数字人才大赛在昆成功举办
MySQL常用图形管理工具 | 黑马程序员
宝,运维100+服务器很头疼怎么办?用行云管家!
前4A高管搞代运营,拿下一个IPO
transform + asm资料
【Go ~ 0到1 】 第五天 7月1 类型别名,自定义类型,接口,包与初始化函数
Solution of intelligent supply chain management platform in aquatic industry: support the digitalization of enterprise supply chain and improve enterprise management efficiency
Lumiprobe cell imaging study PKH26 cell membrane labeling kit
11. Users, groups, and permissions (1)
白盒加密技术浅理解
Lean thinking: source, pillar, landing. I understand it after reading this article
How to use the low code platform of the Internet of things for personal settings?