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/

  • 相关阅读:
    学习记录-1
    初识CSS3 3D效果,浅谈理解
    so easy,too happy
    软件工程作业----自我介绍
    从零玩转JavaWeb系列7web服务器-----表单的提交
    从零玩转JavaWeb系列7web服务器-----get与post的区别
    从零玩转JavaWeb系列7web服务器-----用户登录界面二维码的制作
    mina 粘包、多包和少包的解决方法
    《MFC游戏开发》笔记十 游戏中的碰撞检测进阶:地图类型&障碍物判定
    使用 mina 传输大字节数组
  • 原文地址:https://www.cnblogs.com/anxin6699/p/7057589.html
Copyright © 2011-2022 走看看