题目:

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. c++中头文件include规则浅析[译]

    英文原文地址 在开发大型的软件项目时,头文件需要得到恰当的管理,甚至在c中也会面临这种问题,当我们用c++开发时,头文件的管理会变得更复杂,更加耗费我们的时间去管理,下面我将讲一些包含规则来简化这个苦 ...

  2. Android -- Drawable与Bitmap测试

    Drawable                                                                                 以下这个是测试加载10 ...

  3. 团队开发——SCRUM报告(一)

    一.成员介绍 队长:胡亚宝 PM:曹美娜 成员:焦燕.袁亚姣.黄亚萍 二.sprint会议 由于之前是一五一小长假,所以距离上次会议中间隔了很长时间,这里在对上次会议做一下简单的汇总 在上次会议上我们 ...

  4. 微软Hololens学院教程-Hologram 220-空间声音(Spatial sound )【本文是老版本,与最新的微软教程有出入】

    这是老版本的教程,为了不耽误大家的时间,请直接看原文,本文仅供参考哦! 原文链接https://developer.microsoft.com/EN-US/WINDOWS/HOLOGRAPHIC/ho ...

  5. Careercup - Microsoft面试题 - 6282862240202752

    2014-05-11 03:56 题目链接 原题: Given an integer array. Perform circular right shift by n. Give the best s ...

  6. Careercup - Microsoft面试题 - 6337018766295040

    2014-05-10 06:38 题目链接 原题: What do you think is the next big thing in technology? For example, search ...

  7. Eclipse的python插件安装

    网上找了一些资料都没有成功~~然后自己装的过程中编辑记录了一些 当然博客园里也有人用这一种方法也可以参考IBM中的 http://www.cnblogs.com/visec479/p/4139882. ...

  8. 【Subsets】cpp

    题目: Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset ...

  9. Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 模拟

    题目链接: http://codeforces.com/contest/670/problem/E 题解: 用STL的list和stack模拟的,没想到跑的还挺快. 代码: #include<i ...

  10. ios 5

    1.屏幕尺寸568×2/320×2  需要一张568h@2x.png的图片. 2.iOS5不支持udid,用uuid替代,取得uuid方法: -(NSString*) uuid { CFUUIDRef ...