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

  1. "123", 6 -> ["1+2+3", "1*2*3"]
  2. "232", 8 -> ["2*3+2", "2+3*2"]
  3. "105", 5 -> ["1*0+5","10-5"]
  4. "00", 0 -> ["0+0", "0-0", "0*0"]
  5. "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:

  1. public class Solution {
  2. public List<String> addOperators(String num, int target) {
  3. List<String> res = new ArrayList<String>();
  4. dfs(num, target, 0, 0, "", res);
  5. return res;
  6. }
  7.  
  8. private void dfs(String num, int target, long cur, long preCurDiff, String item, List<String> res){
  9. if(cur == target && num.length() == 0){ //cur加到了target 并且没有剩余的num string
  10. res.add(item);
  11. return;
  12. }
  13.  
  14. //从头开始,每次从头取不同长度的string 作为curStr, 作为首个数字
  15. for(int i = 1; i<=num.length(); i++){
  16. String curStr = num.substring(0,i);
  17. if(curStr.length() > 1 && curStr.charAt(0) == '0'){ //去掉corner case 1*05
  18. break;
  19. }
  20. String nextStr = num.substring(i);
  21. if(item.length() == 0){ //当前item为空,说明第一个数字
  22. dfs(nextStr, target, Long.valueOf(curStr), Long.valueOf(curStr), curStr, res);
  23. }else{
  24. dfs(nextStr, target, cur + Long.valueOf(curStr), Long.valueOf(curStr), item + "+" + curStr, res);
  25. dfs(nextStr, target, cur - Long.valueOf(curStr), -Long.valueOf(curStr), item + "-" + curStr, res);
  26. dfs(nextStr, target, cur-preCurDiff + preCurDiff*Long.valueOf(curStr), preCurDiff*Long.valueOf(curStr), item + "*" + curStr, res);
  27. }
  28. }
  29. }
  30. }

LeetCode 282. Expression Add Operators的更多相关文章

  1. [LeetCode] 282. Expression Add Operators 表达式增加操作符

    Given a string that contains only digits 0-9 and a target value, return all possibilities to add bin ...

  2. [leetcode]282. Expression Add Operators 表达式添加运算符

    Given a string that contains only digits 0-9 and a target value, return all possibilities to add bin ...

  3. 【LeetCode】282. Expression Add Operators

    题目: Given a string that contains only digits 0-9 and a target value, return all possibilities to add ...

  4. 282. Expression Add Operators

    题目: Given a string that contains only digits 0-9 and a target value, return all possibilities to add ...

  5. 282 Expression Add Operators 给表达式添加运算符

    给定一个仅包含0-9的字符串和一个目标值,返回在数字之间添加了二元运算符(不是一元的) +.-或*之后所有能得到目标值的情况.例如:"123", 6 -> ["1+ ...

  6. LeetCode Expression Add Operators

    原题链接在这里:https://leetcode.com/problems/expression-add-operators/ 题目: Given a string that contains onl ...

  7. [LeetCode] Expression Add Operators 表达式增加操作符

    Given a string that contains only digits 0-9 and a target value, return all possibilities to add ope ...

  8. [Swift]LeetCode282. 给表达式添加运算符 | Expression Add Operators

    Given a string that contains only digits 0-9 and a target value, return all possibilities to add bin ...

  9. Expression Add Operators

    Given a string that contains only digits 0-9 and a target value, return all possibilities to add bin ...

随机推荐

  1. DCEP究竟是什么?

    DCEP (Digital Currency Electronic Payment) 数字货币电子支付工具 DCEP将由中国人民银行推出,推出时间待定. DCEP是使用区块链技术的一种联盟链,为全新的 ...

  2. Python3中真真假假True、False、None等含义详解

    概述 在Python中,不仅仅和类C一样的真假类似,比如1代表真,0代表假.Python中的真假有着更加广阔的含义范围,Python会把所有的空数据结构视为假,比如[](空列表).{}(空集合).'' ...

  3. awk简单使用

    1.awk格式 awk  [ 切割符号 ]  ' [ / pattern/ ]  函数语句 '   [ 文件名 ] 2.输出对应列 $0  全部 , $1 第一列  ,$2 第二列  ...... a ...

  4. qt 旧项目编译运行提示 “启动程序失败,路径或者权限错误?” 原因及解决方法

    qt 旧项目编译运行提示 "启动程序失败,路径或者权限错误?" 原因及解决方法 原因 Qt Creator在打开项目文件的同时会生成.pro.user文件,.pro.user文件叫 ...

  5. sublime3 安装markdown插件

    sublime3 安装markdown插件 第一步安装package control 自动安装package control 手动安装package control 安装具体的markdown相关的p ...

  6. Scratch编程:绘制七色花(七)

    “ 上节课的内容全部掌握了吗?反复练习了没有,编程最好的学习方法就是练习.练习.再练习.一定要记得多动手.多动脑筋哦~~” 01 — 游戏介绍 绘制一朵美丽的七色花. 02 — 设计思路 使用画笔功能 ...

  7. linux 设置用户组共享文件

    1.首先建立一个名为workgroup的用户组,然后向用户组里面添加两名用户 tom 和liming,并为其设置密码 sudo groupadd workgroup [sudo] lcy 的密码: s ...

  8. 在论坛中出现的比较难的sql问题:11(字符分拆 多关键字匹配问题)

    原文:在论坛中出现的比较难的sql问题:11(字符分拆 多关键字匹配问题) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉 ...

  9. 数据结构与算法(周测3-Huffman树)

    判断题 1.Given a Huffman tree for N (≥2) characters, all with different weights. The weight of any non- ...

  10. Scrapy 安装与使用

    Scrapy的安装: 当前环境win10,python_3.6.4,64bit.在命令提示符窗口运行pip install Scrapy,出现以下结果: building 'twisted.test. ...