当前位置:网站首页>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 .
边栏推荐
- STM32F030F4驱动TIM1637数码管芯片
- 实在RPA:银行数字化,业务流程自动化“一小步”,贷款审核效率“一大步”
- 【必会】BM41 输出二叉树的右视图【中等+】
- Anomaly-Transformer (ICLR 2022 Spotlight)复现过程及问题
- De PIP. Interne. CLI. Main Import main modulenotfounderror: No module named 'PIP'
- typescript枚举
- 力扣今日题-241. 为运算表达式设计优先级
- Practical application and extension of plain framework
- MT manager test skiing Adventure
- [micro service sentinel] @sentinelresource details
猜你喜欢
Know --matplotlib
2021 RoboCom 世界机器人开发者大赛-高职组初赛
马赛克后挡板是什么?
De PIP. Interne. CLI. Main Import main modulenotfounderror: No module named 'PIP'
什么是马赛克?
2022 safety officer-c certificate examination question simulation examination question bank and simulation examination
物联网现状及未来发展趋势
What category does the Internet of things application technology major belong to
从第三次技术革命看企业应用三大开发趋势
[understanding of opportunity-35]: Guiguzi - flying clamp - the art of remote connection, remote control and remote testing
随机推荐
Zero foundation tutorial of Internet of things development
【微服务|Sentinel】sentinel整合openfeign
How to display real-time 2D map after rviz is opened
关于游戏性能优化的一些感想
2022-07-01:某公司年会上,大家要玩一食发奖金游戏,一共有n个员工, 每个员工都有建设积分和捣乱积分, 他们需要排成一队,在队伍最前面的一定是老板
【微服务|Sentinel】SentinelResourceAspect详解
共享电商的背后: 共创、共生、共享、共富,共赢的共富精神
y53.第三章 Kubernetes从入门到精通 -- ingress(二六)
小程序表单校验封装
硅谷产品实战学习感触
MT manager test skiing Adventure
Create Ca and issue certificate through go language
Development trend and future direction of neural network Internet of things
神经网络物联网的未来趋势与发展
有没有一段代码,让你为人类的智慧所折服
Istio, ebpf and rsocket Broker: in depth study of service grid
SWT / anr problem - SWT causes low memory killer (LMK)
认识--Matplotlib
Postgresql随手记(10)动态执行EXECUTING语法解析过程
MySQL -- convert rownum in Oracle to MySQL