当前位置:网站首页>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 .
边栏推荐
- 2021 RoboCom 世界机器人开发者大赛-高职组初赛
- [micro service sentinel] @sentinelresource details
- Is it safe to choose mobile phone for stock trading account opening in Shanghai?
- 神经网络物联网的发展趋势和未来方向
- Notes on problems - /usr/bin/perl is needed by mysql-server-5.1.73-1 glibc23.x86_ sixty-four
- The online beggar function of Japanese shopping websites
- 2022-07-01:某公司年会上,大家要玩一食发奖金游戏,一共有n个员工, 每个员工都有建设积分和捣乱积分, 他们需要排成一队,在队伍最前面的一定是老板
- Three development trends of enterprise application from the perspective of the third technological revolution
- js——arguments的使用
- CADD课程学习(3)-- 靶点药物相互作用
猜你喜欢

Yoga27 multidimensional all-in-one computer with excellent appearance and high-end configuration

Matplotlib common settings

The digital summit is popular, and city chain technology has triggered a new round of business transformation

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

Notes on problems - /usr/bin/perl is needed by mysql-server-5.1.73-1 glibc23.x86_ sixty-four

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

Linux foundation - centos7 offline installation of MySQL

Zhao Fuquan: to ensure supply in the short term, we should build a safe, efficient and resilient supply chain in the long term

Istio、eBPF 和 RSocket Broker:深入研究服务网格

from pip._ internal. cli. main import main ModuleNotFoundError: No module named ‘pip‘
随机推荐
Some thoughts on game performance optimization
每日三题 6.28
关于游戏性能优化的一些感想
Stm32f030f4 drives tim1637 nixie tube chip
2021 RoboCom 世界机器人开发者大赛-高职组复赛
2022 R1 fast opening pressure vessel operation test questions and answers
What category does the Internet of things application technology major belong to
2022安全员-C证考试题模拟考试题库及模拟考试
Practical application and extension of plain framework
CADD课程学习(3)-- 靶点药物相互作用
Microservice stability management
2021 RoboCom 世界机器人开发者大赛-高职组初赛
Create Ca and issue certificate through go language
图的遍历之深度优先搜索和广度优先搜索
What is the relationship between modeling and later film and television?
Typescript enumeration
为什么PHP叫超文本预处理器
2022年危险化学品经营单位安全管理人员考试题及在线模拟考试
Daily three questions 6.30 (2)
The digital summit is popular, and city chain technology has triggered a new round of business transformation