LeetCode 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 Expression Add Operators的更多相关文章
- [LeetCode] Expression Add Operators 表达式增加操作符
Given a string that contains only digits 0-9 and a target value, return all possibilities to add ope ...
- [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
原题链接在这里:https://leetcode.com/problems/expression-add-operators/ 题目: Given a string that contains onl ...
- [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 ...
- [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 ...
- LeetCode282. Expression Add Operators
Given a string that contains only digits 0-9 and a target value, return all possibilities to add bin ...
随机推荐
- CMYK印刷色
一,介绍 CMYK也称作印刷色彩模式,顾名思义就是用来印刷的. 它和RGB相比有一个很大的不同:RGB模式是一种发光的色彩模式,CMYK是一种依靠反光的色彩模式. CMYK——即青.洋红(品红).黄. ...
- 从JAVA客户端访问Redis示例(入门)
转自:http://blog.csdn.net/kkdelta/article/details/7217761 本文记录了安装Redis和从JAVA端访问Redis的步骤 从http://downlo ...
- 【C语言】10-字符和字符串常用处理函数
一.字符处理函数 下面介绍的两个字符处理函数都是在stdio.h头文件中声明的. 1.字符输出函数putchar putchar(65); // A putchar('A'); // A int a ...
- CvMat and cv::Mat
CvMat: typedef struct CvMat { int type; int step; /* for internal use only */ int* refcount; int hdr ...
- sql2005 全文索引
1.打开全文索引 一.检查服务里面带有Full-text字样的服务是否存在并开启! [](http://images2015.cnblogs.com/blog/23017/201611/23017-2 ...
- Java下利用Jackson进行JSON解析和序列化
Java下利用Jackson进行JSON解析和序列化 Java下常见的Json类库有Gson.JSON-lib和Jackson等,Jackson相对来说比较高效,在项目中主要使用Jackson进行 ...
- sqlserver ldf过大处理方法
第一步 DUMP TRANSACTION databasename WITH NO_LOG -- 第二步:截断事务日志 BACKUP LOG databasename WITH ...
- Scrum会议3(Beta版本)
组名:天天向上 组长:王森 组员:张政.张金生.林莉.胡丽娜 代码地址:HTTPS:https://git.coding.net/jx8zjs/llk.git SSH:git@git.coding.n ...
- PHP生成随机密码的4种方法及性能对比
PHP生成随机密码的4种方法及性能对比 http://www.php100.com/html/it/biancheng/2015/0422/8926.html 来源:露兜博客 时间:2015-04 ...
- PROTEL99SE封装说明
PROTEL99SE封装说明 路板上的焊盘间的距离也就是300mil(因为在电机领域里,是以英制单位为主的.同样的, 对于无极性的电容,RAD0.1-RAD0.4 也是一样:对有极性的电容如电解电容 ...