当前位置:网站首页>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
边栏推荐
- 云服务器ECS夏日省钱秘籍,这次@老用户快来领走
- Taiaisu M source code construction, peak store app premium consignment source code sharing
- 数商云:从规划到落地,五矿集团如何快速构建数字化发展新格局?
- Bao, what if the O & M 100+ server is a headache? Use Xingyun housekeeper!
- 【快应用】text组件里的文字很多,旁边的div样式会被拉伸如何解决
- kubernetes命令入门(namespaces,pods)
- Huawei game failed to initialize init with error code 907135000
- 毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?
- C端梦难做,科大讯飞靠什么撑起10亿用户目标?
- Lumiprobe cell imaging study PKH26 cell membrane labeling kit
猜你喜欢

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

寶,運維100+服務器很頭疼怎麼辦?用行雲管家!

11. Users, groups, and permissions (1)

Altair HyperWorks 2022软件安装包和安装教程

How to realize the applet in its own app to realize continuous live broadcast

bean的生命周期核心步骤总结

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

Example explanation: move graph explorer to jupyterlab

Lumiprobe 自由基分析丨H2DCFDA说明书

3. "Create your own NFT collections and publish a Web3 application to show them" cast NFT locally
随机推荐
11. Users, groups, and permissions (1)
数商云:从规划到落地,五矿集团如何快速构建数字化发展新格局?
如何运营好技术相关的自媒体?
[quick application] win7 system cannot run and debug projects using Huawei ide
Example explanation: move graph explorer to jupyterlab
Appgallery connect scenario development practice - image storage and sharing
如何使用物联网低代码平台进行个人设置?
B2B e-commerce platform solution for fresh food industry to improve the standardization and transparency of enterprise transaction process
Lumiprobe 亚磷酰胺丨六甘醇亚磷酰胺说明书
Today, with the popularity of micro services, how does service mesh exist?
nacos配置文件发布失败,请检查参数是否正确的解决方案
Docker deploy mysql8.0
云服务器ECS夏日省钱秘籍,这次@老用户快来领走
Lumiprobe free radical analysis h2dcfda instructions
PostgreSQL varchar[] 数组类型操作
Love business in Little Red Book
MySQL常用图形管理工具 | 黑马程序员
Shell array
R language ggplot2 visualization: visualize the line graph and add customized Y-axis label information to the line graph using the labels function
Li Kou daily question - Day 32 -589 N × Preorder traversal of tree