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. SQL DatePart函数使用

    DATEPART 返回代表指定日期的指定日期部分的整数. 语法 DATEPART ( datepart ,date ) 参数 datepart 是指定应返回的日期部分的参数.下表列出了 Microso ...

  2. CSS学习点滴

    1.CSS :link 选择器 a:link { background-color:yellow;text-decoration:none } 参考:http://www.w3school.com.c ...

  3. 四种生成和解析XML文档的方法详解(介绍+优缺点比较+示例)

    众所周知,现在解析XML的方法越来越多,但主流的方法也就四种,即:DOM.SAX.JDOM和DOM4J 下面首先给出这四种方法的jar包下载地址 DOM:在现在的Java JDK里都自带了,在xml- ...

  4. [asp.net core]project.json(1)

    摘要 前面介绍了使用vs2015新建asp.net core web的内容,这篇文章学习下project.json文件的内容. project.json 原文:https://docs.microso ...

  5. FZU2169 shadow题解

    http://acm.fzu.edu.cn/problem.php?pid=2169 Problem Description YL 是shadow国的国王,shadow国有N个城市.为了节省开支,sh ...

  6. 导航菜单:jQuery粘性滚动导航栏效果

    粘性滚动是当导航在滚动过程中会占粘于浏览器上,达到方便网站页面浏览的效果,也是一种用户体验,下面我们看一下是怎么实现的: jQuery的 smint插件,也是一个导航菜单固定插件.当页滚动时,导航菜单 ...

  7. PHP中逻辑运算符and/or与||/&&的一个坑

    我原来以为PHP中的and和&&是一样的, 只是写法上为了可读性和美观, 事实上我错了. 这里面深藏了一个坑! 看以下代码: $bA = true; $bB = false; $b1  ...

  8. 透过统计力学,模拟软物质——EPJE专访2016年玻尔兹曼奖得主Daan Frenkel

    原文来源:Eur. Phys. J. E (2016) 39: 68 2016年玻尔兹曼奖得主Daan Frenkel接受欧洲物理学报E专访,畅谈统计物理在交叉科学研究中的前所未有的重要性. 统计物理 ...

  9. CHAP算法C++实现

    CHAP是一种挑战响应式协议. CHAP全称为:Challenge Handshake Authentication Protocol. CHAP密码 = 一个字节的标识符 + MD5(一个字节的标识 ...

  10. redis.1--SDS结构

    1. Redis 没有直接使用c语言的字符串(以空字符结尾的字符数组),而是自己构建了一 种名为简单动态字符串(Simple Dynamic String , SDS),并将SDS做为         ...