当前位置:网站首页>力扣今日题-241. 为运算表达式设计优先级
力扣今日题-241. 为运算表达式设计优先级
2022-07-01 23:00:00 【抗争的小青年】
241. 为运算表达式设计优先级
这种应该算作分治吧,动态规划一般会有最优子结构的,二分治是将大问题分解成小问题,分而治之嘛!然后再将小问题的解合并成大问题的解。
class Solution {
HashMap<String, List<Integer>> memo = new HashMap<>();
public List<Integer> diffWaysToCompute(String input) {
// 避免重复计算
if (memo.containsKey(input)) {
return memo.get(input);
}
List<Integer> res = new LinkedList<>();
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
// 扫描算式 input 中的运算符
if (c == '-' || c == '*' || c == '+') {
/******分******/
// 以运算符为中心,分割成两个字符串,分别递归计算
List<Integer>
left = diffWaysToCompute(input.substring(0, i));
List<Integer>
right = diffWaysToCompute(input.substring(i + 1));
/******治******/
// 通过子问题的结果,合成原问题的结果
for (int a : left)
for (int b : right)
if (c == '+')
res.add(a + b);
else if (c == '-')
res.add(a - b);
else if (c == '*')
res.add(a * b);
}
}
// base case
// 如果 res 为空,说明算式是一个数字,没有运算符
if (res.isEmpty()) {
res.add(Integer.parseInt(input));
}
// 将结果添加进备忘录
memo.put(input, res);
return res;
}
}
重点不是去考虑整个问题怎么去做,而是从一个子问题去做,剩下的递归,遍历就好了,电脑肯定做的又快又好。
把昨天的题也补一下吧。
1175. 质数排列
这种解法。。就叫做查字典法吧,答案已经提前写上去了。
class Solution {
public int numPrimeArrangements(int n) {
int[] ans = new int[]{
1, 1, 1, 2, 4, 12, 36, 144, 576, 2880, 17280, 86400, 604800, 3628800, 29030400, 261273600, 612735986, 289151874, 180670593, 445364737, 344376809, 476898489, 676578804, 89209194, 338137903, 410206413, 973508979, 523161503, 940068494, 400684877, 13697484, 150672324, 164118783, 610613205, 44103617, 58486801, 462170018, 546040181, 197044608, 320204381, 965722612, 554393872, 77422176, 83910457, 517313696, 36724464, 175182841, 627742601, 715505693, 327193394, 451768713, 263673556, 755921509, 94744060, 600274259, 410695940, 427837488, 541336889, 736149184, 514536044, 125049738, 250895270, 39391803, 772631128, 541031643, 428487046, 567378068, 780183222, 228977612, 448880523, 892906519, 858130261, 622773264, 78238453, 146637981, 918450925, 514800525, 828829204, 243264299, 351814543, 405243354, 909357725, 561463122, 913651722, 732754657, 430788419, 139670208, 938893256, 28061213, 673469112, 448961084, 80392418, 466684389, 201222617, 85583092, 76399490, 500763245, 519081041, 892915734, 75763854, 682289015};
return ans[n];
}
}
这道题属实是属于投机取巧了。
边栏推荐
- Matplotlib常用設置
- [understanding of opportunity-35]: Guiguzi - flying clamp - the art of remote connection, remote control and remote testing
- [micro service sentinel] sentinel integrates openfeign
- Commemorate becoming the first dayus200 tripartite demo contributor
- MySQL -- convert rownum in Oracle to MySQL
- ShanDong Multi-University Training #3
- MT manager test skiing Adventure
- Microservice stability management
- Create Ca and issue certificate through go language
- mysql ---- Oracle中的rownum转换成MySQL
猜你喜欢
The digital summit is popular, and city chain technology has triggered a new round of business transformation
SWT/ANR问题--SWT 导致 kernel fuse deadlock
CADD course learning (3) -- target drug interaction
Jielizhi Bluetooth headset quality control and production skills [chapter]
认识--Matplotlib
Matplotlib common charts
2022年起重机司机(限桥式起重机)考试试题及模拟考试
微信个人小商店一键开通助手小程序开发
Use 3DMAX to make a chess piece
Redis数据类型和应用场景
随机推荐
想请教股票开户要认识谁?在线开户是安全么?
JS - use of arguments
Y53. Chapter III kubernetes from introduction to mastery -- ingress (26)
物联网开发零基础教程
纪念成为首个DAYUs200三方demo贡献者
Typescript enumeration
plain framework的实际应用和扩展
每日三题 6.28
Matplotlib常用图表
[MySQL] basic use of explain and the function of each column
Jerry's records are powered by Vbat with a power supply voltage of 4.2V [chapter]
Is it safe to choose mobile phone for stock trading account opening in Shanghai?
Some thoughts on game performance optimization
2022 R1 fast opening pressure vessel operation test questions and answers
What is the mosaic tailgate?
from pip._ internal. cli. main import main ModuleNotFoundError: No module named ‘pip‘
RPA: Bank digitalization, business process automation "a small step", and loan review efficiency "a big step"
数字峰会人气火爆,城链科技引发新一轮商业变革
flutter Unable to load asset: assets/images/888.png
Current situation and future development trend of Internet of things