当前位置:网站首页>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
边栏推荐
- ETL development of data warehouse (IV)
- 数商云:从规划到落地,五矿集团如何快速构建数字化发展新格局?
- Stanford, salesforce|maskvit: masked vision pre training for video prediction
- Lean thinking: source, pillar, landing. I understand it after reading this article
- MySQL常用图形管理工具 | 黑马程序员
- Technical secrets of ByteDance data platform: implementation and optimization of complex query based on Clickhouse
- MySQL common graphics management tools | dark horse programmers
- Lake Shore低温恒温器的氦气传输线
- 云服务器ECS夏日省钱秘籍,这次@老用户快来领走
- app发版后的缓存问题
猜你喜欢

M91快速霍尔测量仪—在更短的时间内进行更好的测量

CDGA|从事通信行业,那你应该考个数据管理证书

【直播预约】数据库OBCP认证全面升级公开课

Create your own NFT collections and publish a Web3 application to show them (Introduction)

Is PMP cancelled??

3. "Create your own NFT collections and publish a Web3 application to show them" cast NFT locally

一次SQL优化,数据库查询速度提升 60 倍

The best landing practice of cave state in an Internet ⽹⾦ financial technology enterprise

Summary of the core steps in the life cycle of beans

微服务大行其道的今天,Service Mesh是怎样一种存在?
随机推荐
M91快速霍尔测量仪—在更短的时间内进行更好的测量
[6.24-7.1] review of wonderful technical blog posts in the writing community
华为云专家详解GaussDB(for MySQL)新特性
网易游戏,激进出海
Bao, what if the O & M 100+ server is a headache? Use Xingyun housekeeper!
Create your own NFT collections and publish a Web3 application to show them (Introduction)
Stanford, salesforce|maskvit: masked vision pre training for video prediction
【快应用】Win7系统使用华为IDE无法运行和调试项目
Viewing the whole ecology of Tiktok from a macro perspective
Shell array
kubernetes命令入门(namespaces,pods)
ES6数组去重的三个简单办法
前4A高管搞代运营,拿下一个IPO
Li Kou daily question - Day 32 -589 N × Preorder traversal of tree
Mipi interface, DVP interface and CSI interface of camera [easy to understand]
PostgreSQL varchar[] 数组类型操作
从零开始学 MySQL —数据库和数据表操作
制造业SRM管理系统供应商全方位闭环管理,实现采购寻源与流程高效协同
Golang error handling
Yyds dry inventory ravendb start client API (III)