• [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 题目总结

  • 相关阅读:
    启动eclipse报错 Could not create the Java Virtual Machine
    网站优化-HTML关键词代码使用
    [转载]我们应该更依赖手机一点
    跨域问题
    我研制的操盘机器人今天继续大获全胜2017-03-01
    昨日买的股票今日大获全胜,今日买的毫发无损,操盘机器人年后至今仅一天失误,超强,雄起
    机器人战胜人类操盘手,我研制三年的成果
    年后连续5天开门红,机器人越战越勇
    在家附近散个步(新昌),调整一下情绪,明天就要上班了
    今天开门红,好兆头
  • 原文地址:https://www.cnblogs.com/cnoodle/p/12928364.html
Copyright © 2020-2023  润新知