当前位置:网站首页>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 .
边栏推荐
- from pip._internal.cli.main import main ModuleNotFoundError: No module named ‘pip‘
- 转行软件测试,知道这四点就够了!
- Linux基础 —— CentOS7 离线安装 MySQL
- openresty 负载均衡
- Three development trends of enterprise application from the perspective of the third technological revolution
- Linux foundation - centos7 offline installation of MySQL
- STM32F030F4驱动TIM1637数码管芯片
- 建模和影视后期有什么关联?
- Leetcode (34) -- find the first and last positions of elements in a sorted array
- SWT / anr problem - SWT causes kernel fuse deadlock
猜你喜欢

2022 examination questions and online simulation examination for safety management personnel of hazardous chemical business units

from pip._ internal. cli. main import main ModuleNotFoundError: No module named ‘pip‘

Future trend and development of neural network Internet of things

问题随记 —— /usr/bin/perl is needed by MySQL-server-5.1.73-1.glibc23.x86_64

马赛克后挡板是什么?

物联网应用技术专业是属于什么类

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

CKS CKA CKAD 将终端更改为远程桌面

【小程序】通过scroll-view组件实现左右【滑动】列表

【必会】BM41 输出二叉树的右视图【中等+】
随机推荐
2021 RoboCom 世界机器人开发者大赛-高职组初赛
plain framework的实际应用和扩展
Aaai22 | structural tagging and interaction modeling: a "slim" network for graph classification
Yunxin small class | common cognitive misunderstandings in IM and audio and video
Practical application and extension of plain framework
Future trend and development of neural network Internet of things
通过Go语言创建CA与签发证书
Matplotlib common charts
mt管理器测试滑雪大冒险
[LeetCode] 最后一个单词的长度【58】
软件架构的本质
【小程序】通过scroll-view组件实现左右【滑动】列表
内存泄露和内存溢出的区别是什么?
CKS CKA CKAD 将终端更改为远程桌面
CKS CKA CKAD 将终端更改为远程桌面
flutter Unable to load asset: assets/images/888. png
ShanDong Multi-University Training #3
Daily three questions 6.30 (2)
SWT / anr problem - SWT causes kernel fuse deadlock
Distance measurement - Hamming distance