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

    原题链接在这里:https://leetcode.com/problems/expression-add-operators/

    题目:

    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 -> []

    题解:

    It needs all the possible combinations. Thus it needs to use DFS.

    DFS states need rest string, target, current accumlated value, the diff added last time, current combination path and res.

    每次把新的num.substring按照三种方式添加到当前结果cur中,若是cur==target时, num string 也走到尾部,就把这个item结果加到res中.

    从num string 中依次取出长度大于1的string来生成一个数字, cur 记录当前运算后的结果,preCurDiff用来记录最后变化. e.g. 2+3*2,即将要运算到乘以2的时候,上次循环的cur = 5, 是2 加上 3 得到的,所以preCurDiff = 3, 而要算这个乘2的时候,需要把preCurDiff先从cur中减掉,在加上新的diff. 新的 diff 是3*2=6. 

    数字是可以合并出现的,比如"123", 15能返回"12+3".

    若是出现 2*05 这种情况,要排除.

    用Long型是防止溢出.

    Time Complexity: exponential.

    Space: O(n). n是num长度.

    AC Java:

     1 public class Solution {
     2     public List<String> addOperators(String num, int target) {
     3         List<String> res = new ArrayList<String>();
     4         dfs(num, target, 0, 0, "", res);
     5         return res;
     6     }
     7     
     8     private void dfs(String num, int target, long cur, long preCurDiff, String item, List<String> res){
     9         if(cur == target && num.length() == 0){ //cur加到了target 并且没有剩余的num string
    10             res.add(item);
    11             return;
    12         }
    13         
    14         //从头开始,每次从头取不同长度的string 作为curStr, 作为首个数字
    15         for(int i = 1; i<=num.length(); i++){
    16             String curStr = num.substring(0,i); 
    17             if(curStr.length() > 1 && curStr.charAt(0) == '0'){ //去掉corner case 1*05
    18                 break;
    19             }
    20             String nextStr = num.substring(i);
    21             if(item.length() == 0){ //当前item为空,说明第一个数字
    22                 dfs(nextStr, target, Long.valueOf(curStr), Long.valueOf(curStr), curStr, res);
    23             }else{
    24                 dfs(nextStr, target, cur + Long.valueOf(curStr), Long.valueOf(curStr), item + "+" + curStr, res);
    25                 dfs(nextStr, target, cur - Long.valueOf(curStr), -Long.valueOf(curStr), item + "-" + curStr, res);
    26                 dfs(nextStr, target, cur-preCurDiff + preCurDiff*Long.valueOf(curStr),  preCurDiff*Long.valueOf(curStr), item + "*" + curStr, res);
    27             }
    28         }
    29     }
    30 }
  • 相关阅读:
    Backbone.js 1.0.0源码架构分析(一)
    汤姆大叔的博客(深入理解JavaScript系列(2):揭秘命名函数表达式)
    求职之路(拿到百度、美团、趋势科技、华为offer)
    请大家注意这个网站www.haogongju.net
    后缀数组求最长重复子串
    2015阿里在线笔试题求两个字符串的最长子串
    剑指offer42:翻转单词顺序 VS 左旋转字符串(更高效、简便的解法)
    求和为s的连续正数序列
    数组中的逆序对
    求一串字符串的全排列和所有组合
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/4919251.html
Copyright © 2011-2022 走看看