zoukankan      html  css  js  c++  java
  • [LeetCode] 282. Expression Add Operators

    Given a string num that contains only digits and an integer target, return all possibilities to add the binary operators '+''-'or '*' between the digits of num so that the resultant expression evaluates to the target value.

    Example 1:

    Input: num = "123", target = 6
    Output: ["1*2*3","1+2+3"]
    

    Example 2:

    Input: num = "232", target = 8
    Output: ["2*3+2","2+3*2"]
    

    Example 3:

    Input: num = "105", target = 5
    Output: ["1*0+5","10-5"]
    

    Example 4:

    Input: num = "00", target = 0
    Output: ["0*0","0+0","0-0"]
    

    Example 5:

    Input: num = "3456237490", target = 9191
    Output: []

    Constraints:

    • 1 <= num.length <= 10
    • num consists of only digits.
    • -231 <= target <= 231 - 1

    给表达式添加运算符。

    题意是给定一个仅包含数字 0-9 的字符串和一个目标值,在数字之间添加二元运算符(不是一元)+、- 或 * ,返回所有能够得到目标值的表达式。

    思路是DFS backtracking,一点点去尝试。helper函数里包括了如下几个参数

    res - 最终的结果

    path - 当前被构建的运算表达式

    curNum - 当前被构建的数字

    preNum - 之前一个被构建的数字

    加减法比较好理解,这里乘法的部分稍微有点绕,我参考了这个帖子,帮助理解。乘法的结果 = curNum - preNum + preNum * curNum,乘法操作完毕之后,之前一个被构建的数字也就变成了preNum * curNum。

    时间 - not sure

    空间 - O(n)

    Java实现

     1 class Solution {
     2     public List<String> addOperators(String num, int target) {
     3         List<String> res = new ArrayList<>();
     4         // corner case
     5         if (num == null || num.length() == 0) {
     6             return res;
     7         }
     8         helper(res, "", num, target, 0, 0, 0);
     9         return res;
    10     }
    11 
    12     private void helper(List<String> res, String path, String num, int target, int pos, long curRes, long preNum) {
    13         // corner case
    14         if (pos == num.length()) {
    15             if (target == curRes) {
    16                 res.add(path);
    17                 return;
    18             }
    19         }
    20         for (int i = pos; i < num.length(); i++) {
    21             if (i != pos && num.charAt(pos) == '0') {
    22                 return;
    23             }
    24             long curNum = Long.valueOf(num.substring(pos, i + 1));
    25             if (pos == 0) {
    26                 helper(res, path + curNum, num, target, i + 1, curNum, curNum);
    27             } else {
    28                 helper(res, path + "+" + curNum, num, target, i + 1, curRes + curNum, curNum);
    29                 helper(res, path + "-" + curNum, num, target, i + 1, curRes - curNum, -curNum);
    30                 helper(res, path + "*" + curNum, num, target, i + 1, curRes - preNum + preNum * curNum,
    31                         preNum * curNum);
    32             }
    33         }
    34     }
    35 }

    LeetCode 题目总结

  • 相关阅读:
    生成器
    各种表达式
    迭代器
    闭包函数及装饰器
    名称空间及作用域
    函数的嵌套
    函数对象
    OpenSSL Heartbleed “心脏滴血”漏洞简单攻击示例
    PHP函数usort是咋回事?还能当后门?
    CVE-2017-7269—IIS 6.0 WebDAV远程代码执行漏洞分析
  • 原文地址:https://www.cnblogs.com/cnoodle/p/12928364.html
Copyright © 2011-2022 走看看