当前位置:网站首页>Leetcode-241: designing priorities for operational expressions
Leetcode-241: designing priorities for operational expressions
2022-07-03 00:47:00 【Chrysanthemum headed bat】
leetcode-241: Design priorities for operation expressions
subject
Give you a string of numbers and operators expression , Combine numbers and operators according to different priorities , Calculate and return the results of all possible combinations . You can In any order Return to the answer .
The generated test case meets its corresponding output value in line with 32 Bit integer range , The number of different results does not exceed 104 .
Example 1:
Input :expression = "2-1-1"
Output :[0,2]
explain :
((2-1)-1) = 0
(2-(1-1)) = 2
Example 2:
Input :expression = "2*3-4*5"
Output :[-34,-14,-10,-10,10]
explain :
(2*(3-(4*5))) = -34
((2*3)-(4*5)) = -14
((2*(3-4))*5) = -10
(2*((3-4)*5)) = -10
(((2*3)-4)*5) = 10
Problem solving
Method 1 :dfs( Divide and conquer )
class Solution {
public:
vector<int> diffWaysToCompute(string expression) {
return dfs(0,expression.size()-1,expression);
}
vector<int> dfs(int l,int r,string& s){
vector<int> res;
for(int i=l;i<=r;i++){
if(s[i]>='0'&&s[i]<='9') continue;
vector<int> l1=dfs(l,i-1,s);// Divide and conquer : Get the result on the left side of the operator
vector<int> l2=dfs(i+1,r,s);// Divide and conquer : Get the result on the right side of the operator
for(int a:l1){
for(int b:l2){
int cur=0;
if(s[i]=='+') cur=a+b;
else if(s[i]=='-') cur=a-b;
else cur=a*b;
res.push_back(cur);
}
}
}
if(res.empty()){
int num=0;
for(int i=l;i<=r;i++){
num=num*10+s[i]-'0';
}
res.push_back(num);
}
return res;
}
};
边栏推荐
- Rust ownership (very important)
- Multiprocess programming (II): Pipeline
- Problèmes de configuration lex & yacc & Bison & Flex
- node_modules删不掉
- Illustrated network: what is virtual router redundancy protocol VRRP?
- Multiprocess programming (4): shared memory
- Vulkan is not a "panacea"“
- Some introduction and precautions about XML
- 文件操作IO-Part2
- 2022上半年值得被看见的10条文案,每一句都能带给你力量!
猜你喜欢
![[shutter] image component (load network pictures | load static pictures | load local pictures | path | provider plug-in)](/img/7e/4f9d96edd04e9ffb26434baf34aa43.jpg)
[shutter] image component (load network pictures | load static pictures | load local pictures | path | provider plug-in)

Redis21 classic interview questions, extreme pull interviewer

MySQL 23 classic interview hanging interviewer

指针初阶(基础)

【AutoSAR 十一 通信相关机制】

【AutoSAR 十 IO架构】

1.11 - 总线

Rust string slicing, structs, and enumeration classes

An excellent orm in dotnet circle -- FreeSQL

1.12 - 指令
随机推荐
Automated defect analysis in electron microscopic images-论文阅读笔记
How SQLSEVER removes data with duplicate IDS
为什么网站打开速度慢?
2022 list of manufacturers of Chinese 3D vision enterprises (guided positioning and sorting scenes)
Shell implements basic file operations (cutting, sorting, and de duplication)
世平信息首席科学家吕喆:构建以数据和人员为中心的安全能力
【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(中)-- 搜索建议
Hdu3507 (slope DP entry)
【Pulsar文档】概念和架构/Concepts and Architecture
FAQ | FAQ for building applications for large screen devices
[IELTS reading] Wang Xiwei reading P2 (reading fill in the blank)
Why is the website slow to open?
字符设备注册常用的两种方法和步骤
【AutoSAR 十二 模式管理】
Vulkan practice first bullet
ftrace工具的介绍及使用
AEM: Nanlin fan Ben et al. - plant rhizosphere growth promoting bacteria control soybean blight
图解网络:什么是虚拟路由器冗余协议 VRRP?
AttributeError: ‘tuple‘ object has no attribute ‘layer‘问题解决
【AutoSAR 一 概述】