题目:

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. Xcode 添加代码块

    我们经常会定义一些retain的property,而且大概每次我们都会像这样写: @property (nonatomic, retain) Type *name;  每次都要老老实实的把“@prop ...

  2. The underlying JVM is how to realize the synchronized

    http://www.programering.com/a/MjN0IjMwATg.html

  3. Word图片版式设置问题

    word里面插入图片,版式设置为嵌入式,又显示不完整:设置上下,图片又跑到页面上方空白处.无论怎么设置,都不满意. 以为是word的问题,后来网络搜索才发现,如果段落行距为固定值的话,图片改为嵌入型后 ...

  4. windows phone和android,ios的touch事件兼容

    1.开发背景 最近用html5写了个小游戏,中间踩过无数坑,有很多甚至百度都百度不到答案,可见html5还真是不成熟,兼容性的复杂度比ie6有过之而无不及,性能那个渣简直无力吐槽.. 好了,吐槽结束, ...

  5. openstack与VMware workStation的区别

    免责声明:     本文中使用的部分图片来自于网络,如有侵权,请联系博主进行删除 最近一直在研究云计算,恰好有个同事问了我一个问题:你们研究的openstack到底是什么东西?跟VMware Work ...

  6. 设计模式之职责链模式(Chain of Responsibility)

    职责链模式原理: 职责链模式和装饰模式以及组合模式类似的地方是都维持着指向父类的指针, 不同点是职责链模式每个子类都继承父类的指针及每个之类都维持着指向父类的指针,而组合模式与装饰模式是组合类鱼装饰类 ...

  7. Somebody That I Used to Know

    一.查看歌词 http://baike.baidu.com/view/7925491.htm 二.考量歌词 1.我告诉自己你就是我的挚爱 但你的陪伴却让我倍感孤单 但那就是爱 让我刻骨铭心难以忘怀 既 ...

  8. ios后台下载

    http://www.cocoachina.com/industry/20131106/7304.html

  9. Itunes connect State: Developer Action Needed

    In-App Purchases have been returned and are highlighted in the table below. Your In-App Purchase has ...

  10. Sqli-labs less 52

    Less-52 和less50是一样的,只是这里的mysql错误不会在前台显示,但是对于stacked injection是一样的利用方式 http://127.0.0.1/sqli-labs/Les ...