题目:

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 -> []

链接: http://leetcode.com/problems/expression-add-operators/

题解:

给定字符串和target num,求添加不同的运算符,使得字符串经过计算的值等于target num,要求所有的结果。 这道题好像以前小时候玩的数字游戏啊, 现在能用程序解决,真的很高兴。 代码主要参考了discuss里czonzhu的。要处理好几种边界条件,并且这里是用String进行cache,换成StringBuilder的话可能要多写不少行。需要注意的地方是,当i != position,并且position这个char的值为0时,我们不考虑接下来的运算,直接break,这个处理了case: "2" + "05"之类的。还有当position == "0"时,我们不加任何运算符号。因为有乘法运算,所以我们要cache之前的数字, 在backtracking的时候可以方便计算。  这样算下来,是否用doubly linked list来回溯可以处理更多的case,二刷时要尝试。

Time Complexity - (4n), Space Complexity - (4n)

public 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 numString, int target, int position, long curNum, long lastVal) {
if(position > numString.length()) {
return;
}
if(position == numString.length() && curNum == target) {
res.add(path);
return;
} for(int i = position; i < numString.length(); i++) {
if(i != position && numString.charAt(position) == '0') { // pruning edge case: "2" + "05", wrong cut
break;
}
long curVal = Long.parseLong(numString.substring(position, i + 1));
if(position == 0) {
addOperators(res, path + curVal, numString, target, i + 1, curNum + curVal, curVal);
} else {
addOperators(res, path + "+" + curVal, numString, target, i + 1, curNum + curVal, curVal);
addOperators(res, path + "-" + curVal, numString, target, i + 1, curNum - curVal, -curVal);
addOperators(res, path + "*" + curVal, numString, target, i + 1, curNum - lastVal + lastVal * curVal, lastVal * curVal);
}
}
}
}

Reference:

https://leetcode.com/discuss/58614/java-standard-backtrace-ac-solutoin-short-and-clear

https://leetcode.com/discuss/58535/17-lines-solution-dfs-c

https://leetcode.com/discuss/58547/accepted-c-solution

https://leetcode.com/discuss/58876/ac-solution-c-short

https://leetcode.com/discuss/61975/elegant-java-solution

https://leetcode.com/discuss/70597/clean-python-dfs-with-comments

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 ...

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

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

  4. LeetCode 282. Expression Add Operators

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

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

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

  6. Expression Add Operators

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

  7. LeetCode Expression Add Operators

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

  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. LeetCode282. Expression Add Operators

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

随机推荐

  1. layoutSubviews 浅尝

    layoutSubviews是UIView中的属性方法,即只要继承于UIView,就可以使用这个方法,这个方法也很强大,以下是他的触发时机: 1.init初始化不会触发layoutSubviews 2 ...

  2. unity3d中的Viewport

    Camera属性中有个Viewport Rect,如下图: X.Y为(0, 0)代表左下角,(1, 1)代表右上角:W和H分别是Viewport的宽(Width)和高(Height),摄像机的Aspe ...

  3. 【Add binary】cpp

    题目: Given two binary strings, return their sum (also a binary string). For example,a = "11" ...

  4. Log4j2 配置笔记(Eclipse+maven+SpringMVC)

    Log4j2相关介绍可以百度看下,这里只注重配置Log4j2 能够马上跑起来: 1.pom.xml文件中添加Log4j2的相关Maven配置信息 <!-- log4j2 --> <d ...

  5. BCP command usage in SQL Server

    The bcp Command-Line Utility You use the bcp (bulk copy program) tool to address the bulk movement o ...

  6. 手机开发Android模拟器genymotion

    手机开发的时候总会碰到一个问题,eclipse插件自带的android模拟器太慢了. 根据网络上有人推荐使用genymotion模拟器.   主要的操作步骤如下: 1.在genymotion网站注册账 ...

  7. BZOJ3503: [Cqoi2014]和谐矩阵

    题解: 如果第一行的数知道了,我们就可以推出其他行的数. 那么如何判断第一行的数的一种填法是否合法呢?很简单,我们递推出m+1行的数,当且仅当这一行都是0时满足题意. 那么,我们就有了一种想法. 直接 ...

  8. Python爬取百度贴吧图片

    一.获取URL Urllib 模块提供了读取web页面数据的接口,我们可以像读取本地文件一样读取www和ftp上的数据.首先,我们定义了一个getHtml()函数: urllib.urlopen()方 ...

  9. 序列化Color对象

    如下: public class Class2 { [XmlIgnore] public Color Color1 { get { return color1; } set { color1 = va ...

  10. poi生成excel

    转自:http://www.cnblogs.com/bmbm/archive/2011/12/08/2342261.html 1.首先下载poi-3.6-20091214.jar,下载地址如下: ht ...