当前位置:网站首页>Force deduction solution summary 241- design priority for operation expression
Force deduction solution summary 241- design priority for operation expression
2022-07-01 13:58:00 【Lost summer】
Directory links :
Force buckle programming problem - The solution sums up _ Share + Record -CSDN Blog
GitHub Synchronous question brushing items :
https://github.com/September26/java-algorithms
Original link : Power button
describe :
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
Tips :
1 <= expression.length <= 20
expression By numbers and operators '+'、'-' and '*' form .
All integer values in the input expression are in the range [0, 99]
source : Power button (LeetCode)
link :https://leetcode.cn/problems/different-ways-to-add-parentheses
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking :
* Their thinking : * My thinking efficiency is still relatively low . * This problem has nothing to do with symbols , It's the process of pairing each other . * So my idea is to split values and symbols first , Divided into two sets , Then start recursive calculation . * Each time we traverse the set of values , Then select two adjacent ones for calculation , Then form a new set to enter the next cycle . In this way, each traversal is less than one value , When set length 1 when , Indicates the end of the traversal .
Code :
public class Solution241 {
Map<String, Integer> map = new HashMap<>();
public List<Integer> diffWaysToCompute(String expression) {
List<String> valueList = new ArrayList<>();
List<String> symbolList = new ArrayList<>();
char[] chars = expression.toCharArray();
StringBuilder builder = new StringBuilder();
for (int i = 0; i <= chars.length; i++) {
if (i == chars.length) {
valueList.add(builder.toString());
break;
}
char aChar = chars[i];
if (aChar == '+' || aChar == '-' || aChar == '*') {
valueList.add(builder.toString());
symbolList.add(String.valueOf(aChar));
builder.setLength(0);
} else {
builder.append(aChar);
}
}
List<Integer> sumList = new ArrayList<>();
for (String s : valueList) {
sumList.add(Integer.parseInt(s));
}
search(sumList, valueList, symbolList);
return new ArrayList<>(map.values());
}
private void search(List<Integer> sumList, List<String> valueList, List<String> symbolList) {
if (valueList.size() == 1) {
String s = valueList.get(0);
// The result of the calculation is
map.put(s, sumList.get(0));
return;
}
for (int i = 0; i < valueList.size() - 1; i++) {
ArrayList<String> newList = new ArrayList<>(valueList);
ArrayList<Integer> newNumList = new ArrayList<>(sumList);
String remove = newList.remove(i);
Integer leftValue = newNumList.remove(i);
String s = newList.get(i);
Integer rightValue = newNumList.get(i);
String symbol = symbolList.get(i);
newList.set(i, "(" + remove + symbol + s + ")");
if (symbol.equals("+")) {
newNumList.set(i, leftValue + rightValue);
} else if (symbol.equals("-")) {
newNumList.set(i, leftValue - rightValue);
} else {
newNumList.set(i, leftValue * rightValue);
}
symbolList.remove(i);
search(newNumList, newList, symbolList);
symbolList.add(i, symbol);
}
}
}边栏推荐
- 小程序- view中多个text换行
- Texstudio tutorial
- El form item regular verification
- App自动化测试开元平台Appium-runner
- [IOT completion. Part 2] stm32+ smart cloud aiot+ laboratory security monitoring system
- Several models of IO blocking, non blocking, IO multiplexing, signal driven and asynchronous IO
- C language course design topic
- Explain IO multiplexing, select, poll, epoll in detail
- 运行游戏时出现0xc000007b错误的解决方法[通俗易懂]
- ArrayList capacity expansion mechanism and thread safety
猜你喜欢

What "hard core innovations" does Intel have in the first half of 2022? Just look at this picture!
![[machine learning] VAE variational self encoder learning notes](/img/38/3eb8d9078b2dcbe780430abb15edcb.png)
[machine learning] VAE variational self encoder learning notes

研发效能度量框架解读

日志中打印统计信息的方案

Build your own website (21)

The best landing practice of cave state in an Internet ⽹⾦ financial technology enterprise

Oracle-数据库对象的使用

清华章毓晋老师新书:2D视觉系统和图像技术(文末送5本)

洞态在某互联⽹⾦融科技企业的最佳落地实践
![[安网杯 2021] REV WP](/img/98/ea5c241e2b8f3ae4c76e1c75c9e0d1.png)
[安网杯 2021] REV WP
随机推荐
【剑指 Offer】55 - II. 平衡二叉树
GET请求如何传递数组参数
QT社团管理系统
MySQL日志
龙蜥社区开源 coolbpf,BPF 程序开发效率提升百倍
Error:Kotlin: Module was compiled with an incompatible version of Kotlin. The binary version of its
Fiori 应用通过 Adaptation Project 的增强方式分享
我们该如何保护自己的密码?
详细讲解面试的 IO多路复用,select,poll,epoll
Detailed explanation of leetcode reconstruction binary tree [easy to understand]
Summary of 20 practical typescript single line codes
What "hard core innovations" does Intel have in the first half of 2022? Just look at this picture!
开源实习经验分享:openEuler软件包加固测试
Explain IO multiplexing, select, poll, epoll in detail
日志中打印统计信息的方案
被裁三個月,面試到處碰壁,心態已經開始崩了
【241. 为运算表达式设计优先级】
About fossage 2.0 "meta force meta universe system development logic scheme (details)
玩转MongoDB—搭建MongoDB集群
Machine learning summary (I): linear regression, ridge regression, Lasso regression