当前位置:网站首页>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);
}
}
}边栏推荐
- This paper introduces an implementation scheme to enhance the favorite transaction code management tool in SAP GUI
- [NLP] pre training model - gpt1
- 【IoT毕设.上】STM32+机智云AIoT+实验室安全监控系统
- 龙蜥社区开源 coolbpf,BPF 程序开发效率提升百倍
- 微机原理与接口技术知识点整理复习–纯手打
- 被裁三个月,面试到处碰壁,心态已经开始崩了
- [flask] flask starts and implements a minimal application based on flask
- 6年技术迭代,阿里全球化出海&合规的挑战和探索
- Après avoir été licencié pendant trois mois, l'entrevue s'est effondrée et l'état d'esprit a commencé à s'effondrer.
- 机器学习总结(一):线性回归、岭回归、Lasso回归
猜你喜欢

分布式事务简介(seata)

逻辑是个好东西

玩转MongoDB—搭建MongoDB集群

Error:Kotlin: Module was compiled with an incompatible version of Kotlin. The binary version of its

SAP intelligent robot process automation (IRPA) solution sharing
![[安网杯 2021] REV WP](/img/98/ea5c241e2b8f3ae4c76e1c75c9e0d1.png)
[安网杯 2021] REV WP

SAP 智能机器人流程自动化(iRPA)解决方案分享

使用 Lambda 函数URL + CloudFront 实现S3镜像回源

TexStudio使用教程

2022. Let me take you from getting started to mastering jetpack architecture components - lifecycle
随机推荐
6年技术迭代,阿里全球化出海&合规的挑战和探索
[Jianzhi offer] 54 The k-th node of binary search tree
[anwangbei 2021] Rev WP
Fiori applications are shared through the enhancement of adaptation project
Build a vc2010 development environment and create a tutorial of "realizing Tetris game in C language"
【 剑指 Offer】55 - I. 二叉树的深度
C language ordering management system
当你真的学会DataBinding后,你会发现“这玩意真香”!
What class loading mechanisms does the JVM have?
【IoT毕设.下】STM32+机智云AIoT+实验室安全监控系统
机器学习总结(一):线性回归、岭回归、Lasso回归
使用net core 6 c# 的 NPOI 包,读取excel..xlsx单元格内的图片,并存储到指定服务器
Simplex, half duplex, full duplex, TDD and FDD
微机原理与接口技术知识点整理复习–纯手打
ArrayList capacity expansion mechanism and thread safety
Solution to 0xc000007b error when running the game [easy to understand]
leetcode622. Design cycle queue (C language)
SAP intelligent robot process automation (IRPA) solution sharing
[241. Design priority for operation expression]
2022年PMP项目管理考试敏捷知识点(6)