当前位置:网站首页>241. Design priority for operational expressions: DFS application questions
241. Design priority for operational expressions: DFS application questions
2022-07-01 11:36:00 【Gong Shui Sanye's Diary of writing questions】
Title Description
This is a LeetCode Upper 241. Design priorities for operation expressions , The difficulty is secondary .
Tag : 「DFS」、「 Explosive search 」
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 Bit integer range , The number of different results does not exceed .
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
Tips :
expressionBy numbers and operators'+'、'-'and'*'form .All integer values in the input expression are in the range
DFS
For convenience , We make expression by s.
The data range is , And all the calculation results should be counted , We can use DFS Search all plans .
A given s Only numbers and operators , We can divide the formula into left and right parts according to the operator , Design recursive functions List<Integer> dfs(int l, int r), It means to search the substring All operation results of .
The final answer is dfs(0,n-1), among Is the length of the input parameter string , At the same time, we have an obvious recursive exit : When given When there are no operators , The search result is The number itself .
Consider how to treat any Calculate : We can enumerate All operator positions in the range are searched , Suppose the currently enumerated For operator , We can recurse to the left of the operator dfs(l,i-1) Get all the results on the left , To the right of the recursive operator dfs(i+1,r) Get all the results on the right , combination 「 Multiplication principle 」 You can know that with the current operator All schemes of expressions for split points .
It's not hard to find out , All the above processes are made by 「 Small expression 」 The results are derived 「 Large expression 」 Result , So it can also be used 「 Section DP」 Method to solve , Complexity and DFS Agreement .
Code :
class Solution {
char[] cs;
public List<Integer> diffWaysToCompute(String s) {
cs = s.toCharArray();
return dfs(0, cs.length - 1);
}
List<Integer> dfs(int l, int r) {
List<Integer> ans = new ArrayList<>();
for (int i = l; i <= r; i++) {
if (cs[i] >= '0' && cs[i] <= '9') continue;
List<Integer> l1 = dfs(l, i - 1), l2 = dfs(i + 1, r);
for (int a : l1) {
for (int b : l2) {
int cur = 0;
if (cs[i] == '+') cur = a + b;
else if (cs[i] == '-') cur = a - b;
else cur = a * b;
ans.add(cur);
}
}
}
if (ans.isEmpty()) {
int cur = 0;
for (int i = l; i <= r; i++) cur = cur * 10 + (cs[i] - '0');
ans.add(cur);
}
return ans;
}
}
Time complexity : The complexity is related to the number of final results , The number of final results is 「 Carter LAN number 」, The complexity is Spatial complexity : The complexity is related to the number of final results , The number of final results is 「 Carter LAN number 」, The complexity is
Last
This is us. 「 Brush through LeetCode」 The first of the series No.241 piece , The series begins with 2021/01/01, As of the start date LeetCode I have in common 1916 questions , Part of it is a locked question , We will finish all the questions without lock first .
In this series , In addition to explaining the idea of solving problems , And give the most concise code possible . If the general solution is involved, there will be corresponding code templates .
In order to facilitate the students to debug and submit code on the computer , I've built a warehouse :https://github.com/SharingSource/LogicStack-LeetCode .
In the warehouse address , You can see the links to the series 、 The corresponding code of the series 、LeetCode Links to the original problem and other preferred solutions .
More, more, more popular 「 written examination / interview 」 Relevant information can be found in the beautifully arranged Gather new bases
边栏推荐
- 对于mvvm和mvc的理解
- 8款最佳实践,保护你的 IaC 安全!
- 为什么一定要从DevOps走向BizDevOps?
- 指纹浏览器工作原理、使用场景以及重要性简单讲解
- Skip the test cases to be executed in the unittest framework
- Kafuka learning path (I) Kafuka installation and simple use
- ES6 Promise用法小结
- 软件项目管理 9.2.软件项目配置管理过程
- How to make the development of liquidity pledge mining system, case analysis and source code of DAPP defi NFT LP liquidity pledge mining system development
- 京东与腾讯续签合作:向腾讯发行A类股 价值最高达2.2亿美元
猜你喜欢

redis配置环境变量

图的理论基础

名创拟7月13日上市:最高发行价22.1港元 单季净利下降19%

Oneconnect plans to be listed in Hong Kong on July 4: a loss of nearly 3 billion in two years, with a market capitalization evaporation of more than 90%

Explore the contour detection function findcontours() of OpenCV in detail with practical examples, and thoroughly understand the real role and meaning of each parameter and mode

达梦数据冲刺科创板:拟募资24亿 冯裕才曾为华科教授

Unittest 框架介绍及第一个demo

CAD如何设置标注小数位

商汤进入解禁期:核心管理层自愿禁售 强化公司长期价值信心

Comment Cao définit la décimale de dimension
随机推荐
Impressive bug summary (continuously updated)
Value/string in redis
Brief analysis of edgedb architecture
耐克如何常年霸榜第一名?最新财报答案来了
Redis启动与库进入
TMUX usage
树莓派4B安装tensorflow2.0[通俗易懂]
指纹浏览器工作原理、使用场景以及重要性简单讲解
Oneconnect plans to be listed in Hong Kong on July 4: a loss of nearly 3 billion in two years, with a market capitalization evaporation of more than 90%
Brief explanation of the working principle, usage scenarios and importance of fingerprint browser
Value/set in redis
二叉堆(一) - 原理与C实现
Ultra detailed black apple installation graphic tutorial sent to EFI configuration collection and system
[buuctf.reverse] 144_ [xman2018 qualifying]easyvm
优雅地翻转数组
openinstall:微信小程序跳转H5配置业务域名教程
软件项目管理 9.2.软件项目配置管理过程
MySQL in and not in() empty list error
证券账户销户后果 开户安全吗
Compile and debug net6 source code