原题链接在这里: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);
}
}
}
}

最新文章

  1. Flapper Bird的学习笔记(三)
  2. 跟着官网的例子学Reacjs (一)FilterableProductTable
  3. 基本hibernate DEMO
  4. bootstrap dialog自行控制窗口的关闭
  5. 【转】linux设备驱动程序中的阻塞机制
  6. 基于visual Studio2013解决C语言竞赛题之1024求和
  7. hdu 4911 Inversion(找到的倒数)
  8. 老调重弹--面向对象设计原则--GRASP设计原则
  9. 【转载】如何让Chrome浏览器允许本地环境支持Ajax
  10. 1.2 eclipse使用 :working set
  11. Linux学习总结(十一)—— Linux常用命令:版本信息查看(RedHat、CentOS、Debian、Ubuntu、Fedora、Oracle)
  12. Redis命令与配置
  13. 深度学习中 Batch Normalization
  14. 8、Python-函数
  15. 【代码笔记】iOS-在Block中修改外部变量值的
  16. zabbix items 配置
  17. SP_OACreate提权经验
  18. 纸壳CMS替换默认实现
  19. Think in Java笔记——Java与对象
  20. JAVA消息 JMS 很重要

热门文章

  1. Spring 中的统一异常处理
  2. c#序列化基类(包含派生类继承DynamicObject和 IXmlSerializable)对象
  3. [C++] 习题 2.14 用队列实现桶排序
  4. 字典的学习2——参考Python编程从入门到实践
  5. Redis 的基本操作、Key的操作及命名规范
  6. css 点击样式,水波纹(记录备用)
  7. Dijkstra算法正确性证明
  8. HTTP协议超级详解
  9. php协议任意文件读取
  10. win10 下的 CUDA10.0 +CUDNN + tensorflow + opencv 环境部署