在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ 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 Example 1: Input: Example 2: Input: 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果。你需要给出所有可能的组合的结果。有效的运算符号包含 示例 1: 输入: 示例 2: 输入: 12ms 1 class Solution { 2 3 var c = [String : [Int]]() 4 func diffWaysToCompute(_ input: String) -> [Int] { 5 let sp = Array(input) 6 let result = getResult(sp) 7 return result 8 } 9 10 func getResult(_ s:[Character]) -> [Int] { 11 if let arr = c[String(s)] { 12 return arr 13 } 14 var ans = [Int]() 15 for i in 0 ..< s.count { 16 if s[i] == "+" || s[i] == "-" || s[i] == "*" { 17 let left = Array(s.prefix(upTo: i)) 18 let right = Array(s.suffix(from: i+1)) 19 let l = getResult(left) 20 let r = getResult(right) 21 22 for a in l { 23 for b in r { 24 if s[i] == "+" { 25 ans.append(a + b) 26 } else if s[i] == "-" { 27 ans.append(a - b) 28 } else if s[i] == "*" { 29 ans.append(a * b) 30 } 31 } 32 } 33 } 34 } 35 if ans.isEmpty { 36 var val : Int = 0 37 for dig in s { 38 val = val * 10 + Int(dig.unicodeScalars.first!.value - "0".unicodeScalars.first!.value) 39 } 40 ans.append(val) 41 } 42 c[String(s)] = ans 43 return ans 44 } 45 } 24ms 1 class Solution { 2 func diffWaysToCompute(_ input: String) -> [Int] { 3 4 var res = [Int]() 5 for (i, c) in input.enumerated(){ 6 if c == "+" || c == "-" || c == "*" { 7 var left = diffWaysToCompute(input.substring(0, i)) 8 var right = diffWaysToCompute(input.substring(i+1)) 9 for j in left.indices { 10 for k in right.indices { 11 if c == "+" { res.append(left[j] + right[k])} 12 if c == "-" { res.append(left[j] - right[k])} 13 if c == "*" { res.append(left[j] * right[k])} 14 } 15 } 16 } 17 } 18 19 if res.isEmpty { res.append(Int(input)!) } 20 return res 21 } 22 } 23 24 extension String { 25 func substring(_ i: Int, _ len: Int = -1) -> String { 26 var startInd = index(startIndex, offsetBy: i) 27 var endInd = endIndex 28 if len != -1 { endInd = index(startIndex, offsetBy: i+len) } 29 return String(self[startInd..<endInd]) 30 } 31 } 24ms 1 class Solution { 2 func diffWaysToCompute(_ input: String) -> [Int] { 3 let sarr = Array(input) 4 var res = [Int]() 5 for i in 0..<sarr.count { 6 let c = sarr[i] 7 if c == "+" || c == "-" || c == "*" { 8 let res1 = diffWaysToCompute(String(sarr[0..<i])) 9 let res2 = diffWaysToCompute(String(sarr[i+1..<sarr.count])) 10 for r1 in res1 { 11 for r2 in res2 { 12 if c == "+" { 13 res.append(r1 + r2) 14 }else if c == "-" { 15 res.append(r1 - r2) 16 }else if c == "*" { 17 res.append(r1 * r2) 18 } 19 } 20 } 21 } 22 } 23 if res.isEmpty { 24 res.append(Int(input)!) 25 } 26 return res 27 } 28 } 44ms 1 class Solution { 2 let operators : [Character] = ["+","-","*"] 3 func diffWaysToCompute(_ input: String) -> [Int] { 4 var result : [Int] = [] 5 var index : String.Index = input.startIndex 6 while index != input.endIndex { 7 if operators.contains(input[index]){ 8 let resultLeft = diffWaysToCompute(String(input[..<index])) 9 let resultRight = diffWaysToCompute(String(input[input.index(after: index)...])) 10 for leftNum in resultLeft { 11 for rightNum in resultRight { 12 switch input[index] { 13 case "+": 14 result.append(leftNum+rightNum) 15 case "-": 16 result.append(leftNum-rightNum) 17 case "*": 18 result.append(leftNum*rightNum) 19 default: 20 break 21 } 22 } 23 } 24 } 25 index = input.index(after: index) 26 } 27 if result.isEmpty { 28 result.append(Int(input) ?? 0) 29 } 30 31 return result 32 } 33 }
|
请发表评论