How to Design priority for operational expressions in leetcode
This article mainly shows you "how to design priorities for operational expressions in leetcode", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let me lead you to study and learn "how to design priorities for operational expressions in leetcode".
Given a string containing numbers and operators, add parentheses to the expression and change its priority to get different results. You need to give the results of all possible combinations. Valid operation symbols include +, -, and *.
Example 1:
Input: "2-1-1" output: [0,2] explanation: ((2-1)-1) = 0 (2-(1-1)) = 2 example 2:
Input: "2x 3-4 x 5" output: [- 34,-14,-10,-10, 10] explain: (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
Ideas for solving the problem:
1, split the input string into token
2, split the array into two parts at any operator location
3. Recursively evaluate the two parts
4, operate on the value
Func diffWaysToCompute (input string) [] int {t:=strToTok (input) fmt.Println (t) return cal (t)}
Func cal (t [] Token) [] int {var r [] int if len (t) = = 1 {r=append (r int if len t [0] .value) return r} for ipurl