当前位置:网站首页>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 :
expression
By 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
边栏推荐
- Dameng data rushes to the scientific innovation board: it plans to raise 2.4 billion yuan. Feng Yucai was once a professor of Huake
- Wonderful! MarkBERT
- Network security learning notes 01 network security foundation
- Spam filtering challenges
- About keil compiler, "file has been changed outside the editor, reload?" Solutions for
- Xiaomi mobile phone unlocking BL tutorial
- 索引失效的几种情况
- ES6 Promise用法小结
- kafuka学习之路(一)kafuka安装和简单使用
- epoll介绍
猜你喜欢
Unittest 框架介绍及第一个demo
Skip the test cases to be executed in the unittest framework
名创拟7月13日上市:最高发行价22.1港元 单季净利下降19%
图的理论基础
Software project management 9.2 Software project configuration management process
Introduction to unittest framework and the first demo
如何看懂开发的查询语句
CPI教程-异步接口创建及使用
Learning summary on June 28, 2022
Neo4j 中文开发者月刊 - 202206期
随机推荐
Skip the test cases to be executed in the unittest framework
kafuka学习之路(一)kafuka安装和简单使用
京东与腾讯续签合作:向腾讯发行A类股 价值最高达2.2亿美元
CPI tutorial - asynchronous interface creation and use
Raspberry pie 4B installation tensorflow2.0[easy to understand]
VScode快捷键(最全)[通俗易懂]
博途V15添加GSD文件
证券账户销户后果 开户安全吗
金鱼哥RHCA回忆录:DO447使用Ansible与API通信--使用Ansible Tower API启动作业
No statements may be issued when any streaming result sets are open and in use on a given connection
田溯宁投的天润云上市:市值22亿港元 年利润下降75%
Face detection and recognition system based on mtcnn+facenet
Exploration and practice of inress in kubernetes
MySQL in and not in() empty list error
树莓派4B安装tensorflow2.0[通俗易懂]
Learning summary on June 28, 2022
redis配置环境变量
Activity workflow engine
自定义 grpc 插件
Value/set in redis