LeetCode 282. Expression Add Operators
原题链接在这里:https://leetcode.com/problems/expression-add-operators/
题目:
Given a string that contains only digits 0-9 and a target value, return all possibilities to add binary operators (not unary) +, -, or * between the digits so they evaluate to the target value.
Examples:
"123", 6 -> ["1+2+3", "1*2*3"]
"232", 8 -> ["2*3+2", "2+3*2"]
"105", 5 -> ["1*0+5","10-5"]
"00", 0 -> ["0+0", "0-0", "0*0"]
"3456237490", 9191 -> []
题解:
It needs all the possible combinations. Thus it needs to use DFS.
DFS states need rest string, target, current accumlated value, the diff added last time, current combination path and res.
每次把新的num.substring按照三种方式添加到当前结果cur中,若是cur==target时, num string 也走到尾部,就把这个item结果加到res中.
从num string 中依次取出长度大于1的string来生成一个数字, cur 记录当前运算后的结果,preCurDiff用来记录最后变化. e.g. 2+3*2,即将要运算到乘以2的时候,上次循环的cur = 5, 是2 加上 3 得到的,所以preCurDiff = 3, 而要算这个乘2的时候,需要把preCurDiff先从cur中减掉,在加上新的diff. 新的 diff 是3*2=6.
数字是可以合并出现的,比如"123", 15能返回"12+3".
若是出现 2*05 这种情况,要排除.
用Long型是防止溢出.
Time Complexity: exponential.
Space: O(n). n是num长度.
AC Java:
public class Solution {
public List<String> addOperators(String num, int target) {
List<String> res = new ArrayList<String>();
dfs(num, target, 0, 0, "", res);
return res;
}
private void dfs(String num, int target, long cur, long preCurDiff, String item, List<String> res){
if(cur == target && num.length() == 0){ //cur加到了target 并且没有剩余的num string
res.add(item);
return;
}
//从头开始,每次从头取不同长度的string 作为curStr, 作为首个数字
for(int i = 1; i<=num.length(); i++){
String curStr = num.substring(0,i);
if(curStr.length() > 1 && curStr.charAt(0) == '0'){ //去掉corner case 1*05
break;
}
String nextStr = num.substring(i);
if(item.length() == 0){ //当前item为空,说明第一个数字
dfs(nextStr, target, Long.valueOf(curStr), Long.valueOf(curStr), curStr, res);
}else{
dfs(nextStr, target, cur + Long.valueOf(curStr), Long.valueOf(curStr), item + "+" + curStr, res);
dfs(nextStr, target, cur - Long.valueOf(curStr), -Long.valueOf(curStr), item + "-" + curStr, res);
dfs(nextStr, target, cur-preCurDiff + preCurDiff*Long.valueOf(curStr), preCurDiff*Long.valueOf(curStr), item + "*" + curStr, res);
}
}
}
}
LeetCode 282. Expression Add Operators的更多相关文章
- [LeetCode] 282. Expression Add Operators 表达式增加操作符
Given a string that contains only digits 0-9 and a target value, return all possibilities to add bin ...
- [leetcode]282. Expression Add Operators 表达式添加运算符
Given a string that contains only digits 0-9 and a target value, return all possibilities to add bin ...
- 【LeetCode】282. Expression Add Operators
题目: Given a string that contains only digits 0-9 and a target value, return all possibilities to add ...
- 282. Expression Add Operators
题目: Given a string that contains only digits 0-9 and a target value, return all possibilities to add ...
- 282 Expression Add Operators 给表达式添加运算符
给定一个仅包含0-9的字符串和一个目标值,返回在数字之间添加了二元运算符(不是一元的) +.-或*之后所有能得到目标值的情况.例如:"123", 6 -> ["1+ ...
- LeetCode Expression Add Operators
原题链接在这里:https://leetcode.com/problems/expression-add-operators/ 题目: Given a string that contains onl ...
- [LeetCode] Expression Add Operators 表达式增加操作符
Given a string that contains only digits 0-9 and a target value, return all possibilities to add ope ...
- [Swift]LeetCode282. 给表达式添加运算符 | Expression Add Operators
Given a string that contains only digits 0-9 and a target value, return all possibilities to add bin ...
- Expression Add Operators
Given a string that contains only digits 0-9 and a target value, return all possibilities to add bin ...
随机推荐
- [DevExpress] - 使得 XtraEditors.TextEdit 失去焦点(LostFocus)的方法
场景 WinForm 应用,使用了 DevExpress.XtraEditors.TextEdit 控件的 KeyPress 和 Leave 事件.期望在 TextEdit 上按下回车键或者当 Tex ...
- 解决windows 激活问题
解决windows 激活问题 下载 然后 搞定 重启
- echarts饼状图位置设置
series: { name: "流量占比分布", type: "pie", radius: ["40%", "60%" ...
- Singer House CodeForces - 830D (组合计数,dp)
大意: 一个$k$层完全二叉树, 每个节点向它祖先连边, 就得到一个$k$房子, 求$k$房子的所有简单路径数. $DP$好题. 首先设$dp_{i,j}$表示$i$房子, 分出$j$条简单路径的方案 ...
- K-th occurrence HDU - 6704 (SA, 主席树)
大意: 给定串$s$, $q$个询问$(l,r,k)$, 求子串$s[l,r]$的第$k$次出现位置. 本来是个简单签到题, 可惜比赛的时候还没学$SA$...... 好亏啊 相同的子串在$SA$中是 ...
- Angular 学习笔记 ( 我追的 feature 和 bug )
Angular 有许多功能还不齐全,也有不少 bug 让人很头疼,所以这里做一些记入 Angular Bug 1.input type="number", valueChanges ...
- core直接获取报异常数据
报异常直接跳转到/Home/Error [ResponseCache(Duration = , Location = ResponseCacheLocation.None, NoStore = tru ...
- Self寄宿
static void Main(string[] args) { //Assembly.Load("WebApplication1, Version=1.0.0.0, Culture=ne ...
- Python 练习汇总
1. Python练习_Python初识_day1 2. Python练习_Python初识_day2 3. Python练习_初识数据类型_day3 4. Python练习_数据类型_day4 5. ...
- java 原子操作(1) CAS
在 java 多线程编程中经常说的就是:“原子操作(atomic operation) 不需要 synchronized”. 原子操作指的是不会被线程调度机制打断的操作:这种操作一旦开始,就一直运行到 ...