当前位置:网站首页>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;
}
};
边栏推荐
- Some introduction and precautions about XML
- 【AutoSAR 一 概述】
- 利亚德:Micro LED 产品消费端首先针对 100 英寸以上电视,现阶段进入更小尺寸还有难度
- Machine learning: numpy version linear regression predicts Boston house prices
- 2022中国3D视觉企业(引导定位、分拣场景)厂商名单
- The most painful programming problem in 2021, adventure of code 2021 Day24
- There is an unknown problem in inserting data into the database
- Multiprocess programming (V): semaphores
- Preview word documents online
- 腾讯云免费SSL证书扩展文件含义
猜你喜欢

Two common methods and steps of character device registration

University of Toronto: Anthony coach | the conditions of deep reinforcement learning can induce dynamic risk measurement

Introduction of UART, RS232, RS485, I2C and SPI

指针初阶(基础)

1.11 - bus

Rust string slicing, structs, and enumeration classes

Introduction and use of ftrace tool

antv x6节点拖拽到画布上后的回调事件(踩大坑记录)

Vulkan-性能及精细化

为什么网站打开速度慢?
随机推荐
logback配置文件
Pageoffice - bug modification journey
[daily training] 871 Minimum refueling times
leetcode-241:为运算表达式设计优先级
【AutoSAR 四 BSW概述】
lex && yacc && bison && flex 配置的問題
【AutoSAR 六 描述文件】
【AutoSAR 五 方法论】
cordova-plugin-device获取设备信息插件导致华为审核不通过
【AutoSAR 十二 模式管理】
Introduction and use of ftrace tool
One of the reasons why setinterval timer does not take effect in ie: the callback is the arrow function
百度智能云牵头打造智能云综合标准化平台
Shell implements basic file operations (cutting, sorting, and de duplication)
关于QByteArray存储十六进制 与十六进制互转
Automated defect analysis in electron microscopic images-论文阅读笔记
Sentry developer contribution Guide - configure pycharm
Thread start and priority
Unity learns from spaceshooter to record the difference between fixedupdate and update in unity for the second time
1.12 - 指令