Leetcode282. Expression Add Operators
来源:互联网 发布:数据库关系模式的定义 编辑:程序博客网 时间:2024/06/06 03:08
Analysis
Oveiously, in order to solve this question, we have to iterate through all of the possible cases at least once. DFS can be used to solve the iteration process.
The optimization we could made it to prune the unnecessary branches, e.g. there is not need to try 0 sequences longer than 1, because this is not a natural number.
And also, like always, we have to take into consideration the overflow problem for a integer-related calculation.
In addition, we have to also deal the the multiplication case differently since multiplicaiton operation has a higher priority than plus and minus. Thus if a multiplication shows up latter, we have to change the calculation sequence in our result. E.g. when we get “2+3” in the previous search and we want to check “2+3*5” in the next step, we have to first subtract 3 from “2+3” and then add “3*5” to the result.
Time complexity:
Considering for every digit 0-9
in the number, we have 4 possible operator that can be assigned between them – +
, -
, ‘*and
join, which means the previous digit and current digit will combined to become 1 number, e.g.
2 join 3will be
23`.
Based on the fact that there are n
digits in total, there are O(4^n)
possible cases that we need to check. Hence the time complexity of the program is O(4^n)
.
Implementation in Java:
class Solution { public List<String> addOperators(String num, int target) { List<String> res = new ArrayList<>(); if (num == null || num.length() == 0) { return res; } addOperators(res, "", num, target, 0, 0, 0); return res; } private void addOperators(List<String> res, String path, String num, int target, int start, long eval, long nextMulted) { if (start == num.length()) { if (eval == target) { res.add(path); return; } } for (int i = start; i < num.length(); i++) { if (i != start && num.charAt(start) == '0') { break; } long curr = Long.parseLong(num.substring(start, i + 1)); if (start == 0) { addOperators(res, path + curr, num, target, i + 1, curr, curr); } else { addOperators(res, path + "+" + curr, num, target, i + 1, eval + curr, curr); addOperators(res, path + "-" + curr, num, target, i + 1, eval - curr, -curr); addOperators(res, path + "*" + curr, num, target, i + 1, eval - nextMulted + nextMulted * curr, nextMulted * curr); } } }}
- leetcode282 : Expression Add Operators
- Leetcode282. Expression Add Operators
- LeetCode:Expression Add Operators
- 【LeetCode】Expression Add Operators
- Expression Add Operators【282】
- Expression Add Operators
- Expression Add Operators
- Expression Add Operators
- Expression Add Operators -- leetcode
- Leetcode -- Expression Add Operators
- LeetCode -- Expression Add Operators
- LeetCode Expression Add Operators
- Expression Add Operators
- leetcode:Expression Add Operators
- 282. Expression Add Operators
- Expression Add Operators
- 282. Expression Add Operators
- Expression Add Operators
- ListView+优化
- ImageLoader的简单使用
- js中常用的事件,onclick 单击事件、onblur 失去焦点事件、onchange改变事件、onmouseover鼠标进入事件、onmouseout鼠标移除事件、onsubmit提交事件
- jmeter中利用BeanShell对时间进行复杂处理代码参考
- Ubuntu 16.04 LTS 成功编译 Android 6.0 源码教程
- Leetcode282. Expression Add Operators
- spring Quartz windows上执行 linux不执行原因
- Android媒体应用(一)
- java之ProcessBuilder执行系统命令
- python logging模块总结
- 向虚拟客服发脾气?你无意中推动了 AI 的进步
- 图像分析:投影曲线的波峰查找
- Java 常见异常种类
- matlab版本下载地址