当前位置:网站首页>Li Kou today's question -241 Design priorities for operational expressions
Li Kou today's question -241 Design priorities for operational expressions
2022-07-01 23:26:00 【Struggling young man】
241. Design priorities for operation expressions
This should be regarded as divide and conquer , Dynamic programming usually has an optimal substructure , Dichotomy is the decomposition of big problems into small problems , Divide and rule ! Then combine the solutions of small problems into the solutions of large problems .
class Solution {
HashMap<String, List<Integer>> memo = new HashMap<>();
public List<Integer> diffWaysToCompute(String input) {
// Avoid double counting
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);
// Scanning formula input Operator in
if (c == '-' || c == '*' || c == '+') {
/****** branch ******/
// Operator centric , Split into two strings , Recursive calculation respectively
List<Integer>
left = diffWaysToCompute(input.substring(0, i));
List<Integer>
right = diffWaysToCompute(input.substring(i + 1));
/****** cure ******/
// Through the result of the subproblem , Synthesize the result of the original problem
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
// If res It's empty , Explain that the formula is a number , There are no operators
if (res.isEmpty()) {
res.add(Integer.parseInt(input));
}
// Add results to memo
memo.put(input, res);
return res;
}
}
The point is not to think about how to do the whole problem , But do it from a sub problem , The rest of recursion , Just traverse , The computer must be fast and good .
Fill in yesterday's question .
1175. Permutation of prime numbers
This solution .. It's called dictionary lookup , The answer has been written in advance .
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];
}
}
This question really belongs to opportunism .
边栏推荐
- flutter Unable to load asset: assets/images/888. png
- 建模和影视后期有什么关联?
- The third part of the construction of the defense system of offensive and defensive exercises is the establishment of a practical security system
- ShanDong Multi-University Training #3
- 2022 R1 fast opening pressure vessel operation test questions and answers
- 小程序表单校验封装
- plain framework的实际应用和扩展
- What professional classification does the application of Internet of things technology belong to
- 马赛克后挡板是什么?
- 力扣今日题-241. 为运算表达式设计优先级
猜你喜欢

CADD课程学习(3)-- 靶点药物相互作用

Redis数据类型和应用场景

问题随记 —— file /usr/share/mysql/charsets/README from install of MySQL-server-5.1.73-1.glibc23.x86_64 c

CKS CKA ckad change terminal to remote desktop

Practical application and extension of plain framework
![[understanding of opportunity-35]: Guiguzi - flying clamp - the art of remote connection, remote control and remote testing](/img/08/9ecfd53a04e147022dde3449aec132.png)
[understanding of opportunity-35]: Guiguzi - flying clamp - the art of remote connection, remote control and remote testing

Redis RDB快照

Matplotlib常用設置

Zhongang Mining: it has inherent advantages to develop the characteristic chemical industry dominated by fluorine chemical industry

MT manager test skiing Adventure
随机推荐
Understanding threads
建模和影视后期有什么关联?
flutter Unable to load asset: assets/images/888.png
【微服务|Sentinel】@SentinelResource详解
Three development trends of enterprise application from the perspective of the third technological revolution
SWT / anr problem - SWT causes kernel fuse deadlock
What professional classification does the application of Internet of things technology belong to
What category does the Internet of things application technology major belong to
What is the difference between memory leak and memory overflow?
ShanDong Multi-University Training #3
日本购物网站的网络乞丐功能
共享电商的背后: 共创、共生、共享、共富,共赢的共富精神
Development trend and future direction of neural network Internet of things
The digital summit is popular, and city chain technology has triggered a new round of business transformation
每日三题 6.29
每日三题 6.30
Notes to problems - file /usr/share/mysql/charsets/readme from install of mysql-server-5.1.73-1 glibc23.x86_ 64 c
云信小课堂 | IM及音视频中常见的认知误区
Some thoughts on game performance optimization
微信个人小商店一键开通助手小程序开发