zoukankan      html  css  js  c++  java
  • [Leetcode] Divide and conquer, DP--241. Different Ways to Add Parentheses

    Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +- and *.

    Example 1

    Input: "2-1-1".

    ((2-1)-1) = 0
    (2-(1-1)) = 2

    Output: [0, 2]

    Example 2

    Input: "2*3-4*5"

    (2*(3-(4*5))) = -34
    ((2*3)-(4*5)) = -14
    ((2*(3-4))*5) = -10
    (2*((3-4)*5)) = -10
    (((2*3)-4)*5) = 10

    Output: [-34, -14, -10, -10, 10]

    Solution:

    1. 1st use divide and conquer   

            we can recursively divide the string into left and right part substring.

       then  recursively calculate the left and right part substring  result when there is only one numerical number and the other numberical number with a valid operator

       finally,  merge their results.

         note:
      how to divide?  iterate from the input string;  if there is a "+", "-", "*" operator,  we will divide the string into left and right substring.
      when there is only one numerical number, directly add into the result list
     


    1 def calcMath(num1, num2, operator): #python does not have switch/case 2 res = 0 3 if operator == "+": 4 res = num1 + num2 5 elif operator == "-": 6 res = num1 - num2 7 elif operator == "*": 8 res = num1 * num2 9 return res 10 11 result = [] 12 for i, c in enumerate(input): 13 if c == "+" or c == "-" or c == "*": 14 leftSubStr = input[0:i] 15 rightSubStr = input[i+1::] 16 17 leftLst = self.diffWaysToCompute(leftSubStr) 18 rightLst = self.diffWaysToCompute(rightSubStr) 19 #print ("ttttt ", i, c, leftLst, rightLst) 20 for ls in leftLst: 21 for rs in rightLst: 22 rs = calcMath(ls, rs, c) 23 result.append(rs) 24 #print ("ttttt ", result) 25 if len(result) == 0: 26 result.append(int(input)) 27 return result

       

      2.  2nd optimization 1st method; (Dynamic programming) using hashmap to store previous subproblem's result; Dynamic programming
      #in the previous calculations, there are repeated calculation of substring with same left substring and right substring calculation


    1 def calcMath(num1, num2, operator): #python does not have switch/case 2 res = 0 3 if operator == "+": 4 res = num1 + num2 5 elif operator == "-": 6 res = num1 - num2 7 elif operator == "*": 8 res = num1 * num2 9 return res 10 11 12 13 def diffWaysToComputeHelper(input, start, end): 14 15 result = [] 16 strKey = str(start) + "-" + str(end) 17 if strKey in dic: 18 result = dic[strKey] 19 return result 20 #print ("sssss ", result, start, end) 21 for i, c in enumerate(input[start:end+1]): 22 if c == "+" or c == "-" or c == "*": 23 leftLst = diffWaysToComputeHelper(input, start, start+i-1 ) 24 rightLst = diffWaysToComputeHelper(input, start+i+1, end) 25 #print ("xxxxx ", i, c, leftLst, rightLst, start, end) 26 for ls in leftLst: 27 for rs in rightLst: 28 rs = calcMath(ls, rs, c) 29 result.append(rs) 30 31 #print ("ttttt ", result, start, end, input[start:end+1]) 32 if len(result) == 0: 33 result.append(int(input[start:end+1])) 34 dic[strKey] = result 35 return result 36 37 dic = {} #memoization 38 return diffWaysToComputeHelper(input, 0, len(input)-1)

    --reference:

    http://www.geeksforgeeks.org/all-ways-to-add-parenthesis-for-evaluation/

  • 相关阅读:
    北风设计模式课程---11、策略模式
    北风设计模式课程---9、原型模式的作用和意义
    linux下lamp.sh一键配置lamp环境流程
    linux日常---1、linux下安装、查看、卸载包常用命令
    常见协议端口号,功能
    尚学linux课程---12、vim操作命令2
    搞笑视频分析---3、爱做饭的芋头:佛跳墙
    黑马day11 脏读数据&解
    hdu 1150 Machine Schedule(最小顶点覆盖)
    Ubuntu在构建Robotframework+Selenium周围环境
  • 原文地址:https://www.cnblogs.com/anxin6699/p/7057589.html
Copyright © 2011-2022 走看看