当前位置:网站首页>力扣今日题-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];
}
}
这道题属实是属于投机取巧了。
边栏推荐
- js——arguments的使用
- 共享电商的背后: 共创、共生、共享、共富,共赢的共富精神
- [micro service sentinel] sentinelresourceaspect details
- CKS CKA CKAD 将终端更改为远程桌面
- 【小程序】通过scroll-view组件实现左右【滑动】列表
- [understanding of opportunity-35]: Guiguzi - flying clamp - the art of remote connection, remote control and remote testing
- Advanced skills of testers: a guide to the application of unit test reports
- Experience of practical learning of Silicon Valley products
- from pip._internal.cli.main import main ModuleNotFoundError: No module named ‘pip‘
- 距离度量 —— 汉明距离(Hamming Distance)
猜你喜欢

AirServer最新Win64位个人版投屏软件

Redis~02 缓存:更新数据时如何保证MySQL和Redis中的数据一致性?

2022年起重机司机(限桥式起重机)考试试题及模拟考试

De PIP. Interne. CLI. Main Import main modulenotfounderror: No module named 'PIP'

2022 crane driver (limited to bridge crane) examination questions and simulation examination

会声会影2022智能、快速、简单的视频剪辑软件
![[applet] realize the left and right [sliding] list through the scroll view component](/img/18/b1b4e9923782856143721dad84cbab.png)
[applet] realize the left and right [sliding] list through the scroll view component

"35 years old, the boss of the company, with a monthly salary of 20000, give away takeout": the times abandoned you, not even saying goodbye

Win 10 mstsc connect RemoteApp

SWT / anr problem - SWT causes kernel fuse deadlock
随机推荐
数字峰会人气火爆,城链科技引发新一轮商业变革
Create Ca and issue certificate through go language
Is it safe to choose mobile phone for stock trading account opening in Shanghai?
2022安全员-C证考试题模拟考试题库及模拟考试
Yunxin small class | common cognitive misunderstandings in IM and audio and video
Jerry's question about long press boot detection [chapter]
dat.GUI
Jerry's records are powered by Vbat with a power supply voltage of 4.2V [chapter]
MySQL binlog cleanup
Win 10 mstsc connect RemoteApp
每日三题 6.29
MySQL -- convert rownum in Oracle to MySQL
2022 examination questions and online simulation examination for safety management personnel of hazardous chemical business units
CKS CKA CKAD 将终端更改为远程桌面
flutter Unable to load asset: assets/images/888. png
Notes on problems - /usr/bin/perl is needed by mysql-server-5.1.73-1 glibc23.x86_ sixty-four
从第三次技术革命看企业应用三大开发趋势
转行软件测试,知道这四点就够了!
【Swoole系列1】在Swoole的世界中,你将学习到什么?
flutter Unable to load asset: assets/images/888.png