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 -> [] 其实这题是很简单的,就是用DFS找出所有可能性,只是最最麻烦的是怎么处理前面部分是加减,后面部分是乘的情况。我本来是用逆波兰表达式来计算的,但是下面的解法更方便。
From: https://segmentfault.com/a/1190000003797204
 public class Solution {
public List<String> addOperators(String num, int target) {
List<String> resulstsList = new ArrayList<>();
helper(num, target, "", , , resulstsList);
return resulstsList;
} private void helper(String num, int target, String tmp, long currRes, long prevNum, List<String> res) {
if (currRes == target && num.length() == ) {
res.add(tmp);
return;
}
// 搜索所有可能的拆分情况
for (int i = ; i <= num.length(); i++) {
String currStr = num.substring(, i);
// 对于前导为0的数予以排除
if (currStr.length() > && currStr.charAt() == '') {
break;
}
// 得到当前截出的数
long currNum = Long.parseLong(currStr);
// 去掉当前的数,得到下一轮搜索用的字符串
String next = num.substring(i);
// 如果不是第一个字母时,可以加运算符,否则只加数字
if (tmp.length() != ) {
// 乘法
helper(next, target, tmp + "*" + currNum, (currRes - prevNum) + prevNum * currNum, prevNum * currNum, res);
// 加法
helper(next, target, tmp + "+" + currNum, currRes + currNum, currNum, res);
// 减法
helper(next, target, tmp + "-" + currNum, currRes - currNum, -currNum, res);
} else {
// 第一个数
helper(next, target, currStr, currNum, currNum, res);
}
}
}
}

Expression Add Operators的更多相关文章

  1. LeetCode Expression Add Operators

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

  2. 282. Expression Add Operators

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

  3. [Swift]LeetCode282. 给表达式添加运算符 | 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 表达式添加运算符

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

  5. LeetCode282. Expression Add Operators

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

  6. 【LeetCode】282. Expression Add Operators

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

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

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

  8. LeetCode 282. Expression Add Operators

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

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

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

随机推荐

  1. Effective Objective-C 2.0 — 第11条:理解 objc_msgSend 的作用

    消息由接受者.选择子及参数构成.给某对象“发送消息” (invoke a message) 也就相当于在该对象上“调用方法”(call a method) 发给某对象的全部信息都要由“动态消息派发系统 ...

  2. Java并发编程核心方法与框架-Future和Callable的使用

    Callable接口与Runnable接口对比的主要优点是Callable接口可以通过Future获取返回值.但是Future接口调用get()方法取得结果时是阻塞的,如果调用Future对象的get ...

  3. Linux消息队列应用

    #include"sys/types.h" #include "sys/msg.h" #include "unistd.h" #includ ...

  4. Yii2 使用 Joins 查询

    Join() JOIN_TYPE = INNER JOIN, LEFT OUTER JOIN, RIGHT OUTER JOIN, FULL OUTER JOIN 等等 语法 $query = new ...

  5. CentOS 与 RedHat 关系和区别

    转自http://www.aixchina.net/club/archiver/tid-26784.html CentOS 发行版介绍 CentOS 是 Community ENTerprise Op ...

  6. 关于帧中继和ppp的补充笔记

    帧中继: · 两个设备都要启用 帧中继功能, 否则是不能 ping通的 · 两个设备上的接口serial要 no shutdown · · 一定要配置dlci地址(号). 否则就不能起来pvc 可以 ...

  7. [译]管理IIS日志的存储

    原文:http://www.iis.net/learn/manage/provisioning-and-managing-iis/managing-iis-log-file-storage Overv ...

  8. activiti数据库表结构全貌解析

    http://www.jianshu.com/p/e6971e8a8dad 下面本人介绍一些activiti这款开源流程设计引擎的数据库表结构,首先阐述:我们刚开始接触或者使用一个新的东西(技术)时我 ...

  9. Apple Watch已向微信开放WatchKit接口?

    Apple Watch在北京时间凌晨1点的苹果发布会上首次对外公布了,一时间applewatch占据了各大媒体.早上也早早地看了相关新闻,其中福布斯中文网的一则消息分外引起@隔壁W叔叔的注意,“苹果同 ...

  10. LUXURY 8

    A - Gargari and Bishops Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...