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

  • 相关阅读:
    HDU-1754 I Hate It (树状数组模板题——单点更新,区间查询最大值)
    HDU-1166 敌兵布阵 (树状数组模板题——单点更新,区间求和)
    JavaScript dotAll模式
    ECMAScript6补全字符串长度方法padStart()和padEnd()
    ECMAScript6重复字符串方法repeat()
    JavaScript确定一个字符串是否包含在另一个字符串中的四种方法
    JavaScript中label与break配合使用
    JavaScript数据结构与算法-集合练习
    JavaScript数据结构与算法-散列练习
    JavaScript数据结构与算法-字典练习
  • 原文地址:https://www.cnblogs.com/cnoodle/p/12928364.html
Copyright © 2011-2022 走看看