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

    public static byte[] DESEncrypt(byte[] data, byte[] sKey) { return DESEncrypt(data, sKey, sKey); } / ...

  2. tomcat的文件路径 servelet的配置 以及maven中的WEB-INF的路径

    Tomcat JavaWeb应用的组成结构 开发JavaWeb应用时,不同类型的文件有严格的存放规则,否则不仅可能会使web应用无法访问,还会导致web服务器启动报错 WebRoot →Web应用所在 ...

  3. Yii2 打印sql语句和批量插入数据

    打印sql语句: $model->find()->createCommand()->getRawSql(); 批量插入 Yii::$app->db->createComm ...

  4. HDInsight 指定输出目录 insert overwrite

    基本语法 insert overwrite local directory '/example/demo/' select * from table; 可以格式化输出 insert overwrite ...

  5. char,short ,int ,long,long long,unsigned long long数据范围

    from:http://www.cnblogs.com/A123456A/archive/2013/01/25/2876634.html char,short ,int ,long,long long ...

  6. css3实现渐变的iPhone滑动解锁效果

    先贴代码 <!DOCTYPE html> <html> <head> <style> p{ width:50%; margin:0 auto; line ...

  7. hdu5024 Wang Xifeng's Little Plot (水

    http://acm.hdu.edu.cn/showproblem.php?pid=5024 网络赛 Wang Xifeng's Little Plot Time Limit: 2000/1000 M ...

  8. R 语言程序设计

    Data The zip file containing the data can be downloaded here: specdata.zip [2.4MB] The zip file cont ...

  9. 分享php中四种webservice实现的简单架构方法及实例

    一:PHP本身的SOAP所有的webservice都包括服务端(server)和客户端(client).要使用php本身的soap首先要把该拓展安装好并且启用.下面看具体的code首先这是服务端实现: ...

  10. HDOJ 4497 GCD and LCM

    组合数学 GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...