★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10204296.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Implement a basic calculator to evaluate a simple expression string.
The expression string may contain open (
and closing parentheses )
, the plus +
or minus sign -
, non-negative integers and empty spaces .
Example 1:
Input: "1 + 1" Output: 2
Example 2:
Input: " 2-1 + 2 " Output: 3
Example 3:
Input: "(1+(4+5+2)-3)+(6+8)" Output: 23
Note:
- You may assume that the given expression is always valid.
- Do not use the
eval
built-in library function.
实现一个基本的计算器来计算一个简单的字符串表达式的值。
字符串表达式可以包含左括号 (
,右括号 )
,加号 +
,减号 -
,非负整数和空格
。
示例 1:
输入: "1 + 1" 输出: 2
示例 2:
输入: " 2-1 + 2 " 输出: 3
示例 3:
输入: "(1+(4+5+2)-3)+(6+8)" 输出: 23
说明:
- 你可以假设所给定的表达式都是有效的。
- 请不要使用内置的库函数
eval
。
96ms
1 class Solution { 2 func calculate(_ s: String) -> Int { 3 var num = 0 4 var res = 0 5 var sign = 1 6 var stack = [Int]() 7 var s = Array(s) 8 for i in 0 ..< s.count { 9 var c = s[i] 10 11 if c == "+" { 12 res += sign*num 13 num = 0 14 sign = 1 15 } else if c == "-" { 16 res += sign*num 17 num = 0 18 sign = -1 19 } else if c == "(" { 20 stack.append(res) 21 stack.append(sign) 22 res = 0 23 sign = 1 24 } else if c == ")" { 25 res += sign*num 26 num = 0 27 res *= stack.removeLast() 28 res += stack.removeLast() 29 } else { 30 if let n = Int(String(c)) { 31 num = 10*num+n 32 } 33 } 34 } 35 36 if num != 0 { 37 res += sign*num 38 } 39 return res 40 } 41 }
132ms
1 class Solution { 2 func calculate(_ s: String) -> Int { 3 var res = 0 4 var num = 0 5 var sign = 1 6 var stack = [Int]() 7 let sArray = Array(s) 8 9 for i in 0..<sArray.count { 10 if let value = Int(String(sArray[i])) { 11 num = num * 10 + value 12 }else if sArray[i] == "+" || sArray[i] == "-" { 13 res += sign * num 14 sign = sArray[i] == "+" ? 1 : -1 15 num = 0 16 }else if sArray[i] == "(" { 17 stack.append(res) 18 stack.append(sign) 19 sign = 1 20 res = 0 21 }else if sArray[i] == ")" { 22 res += num * sign 23 let tmp = res * stack.removeLast() 24 res = stack.removeLast() + tmp 25 sign = 1 26 num = 0 27 } 28 } 29 30 return num == 0 ? res : res + sign * num 31 } 32 }
144ms
1 class Solution { 2 func calculate(_ s: String) -> Int { 3 var result = 0 4 var num = 0 5 var sign = 1 6 var equationStack = [Int]() 7 8 for char in s { 9 if let digit = Int(String(char)) { 10 num = 10 * num + digit 11 } else if char == "+" { 12 result += sign * num 13 num = 0 14 sign = 1 15 } else if char == "-" { 16 result += sign * num 17 num = 0 18 sign = -1 19 } else if char == "(" { 20 equationStack.append(result) 21 equationStack.append(sign) 22 sign = 1 23 result = 0 24 } else if char == ")" { 25 result += sign * num 26 num = 0 27 result *= equationStack.removeLast() 28 result += equationStack.removeLast() 29 } 30 } 31 if num != 0 { result += sign * num } 32 return result 33 } 34 }
192ms
1 class Solution { 2 func calculate(_ s: String) -> Int { 3 var res = 0, num = 0, sign = 1, n = s.count 4 var st = [Int]() 5 for c in s { 6 if c == " " { continue } 7 else if c == "+" || c == "-" { 8 res += sign*num 9 num = 0 10 sign = (c == "+") ? 1: -1 11 } 12 else if c == "(" { 13 st.append(res) 14 st.append(sign) 15 res = 0 16 sign = 1 17 } 18 else if c == ")" { 19 res += sign*num 20 num = 0 21 res *= st.removeLast() 22 res += st.removeLast() 23 24 } 25 else { 26 num = num*10 + Int(String(c))! 27 } 28 } 29 res += sign*num 30 31 return res 32 } 33 }
196ms
1 class Solution { 2 func calculate(_ s: String) -> Int { 3 let trimmedS = s.trimmingCharacters(in: .whitespaces) 4 let charArray = [Character](trimmedS) 5 var stack = [Int]() 6 var num = 0 7 var result = 0 8 var sign = 1 9 10 for i in 0..<charArray.count{ 11 12 if let digit = Int(String(charArray[i])){ 13 num = num*10 + digit 14 if (i < charArray.count - 1 && Int(String(charArray[i + 1])) == nil) || i == charArray.count - 1{ 15 result += num*sign 16 } 17 }else{ 18 num = 0 19 if charArray[i] == "+"{ 20 sign = 1 21 }else if charArray[i] == "-"{ 22 sign = -1 23 }else if charArray[i] == "("{ 24 stack.append(result) 25 stack.append(sign) 26 result = 0 27 sign = 1 28 }else if charArray[i] == ")"{ 29 let signBeforeParenthese = stack.removeLast() 30 let resultBeforeParenthese = stack.removeLast() 31 result = resultBeforeParenthese + signBeforeParenthese*result 32 }else{ 33 continue 34 } 35 } 36 37 } 38 return result 39 40 } 41 }
344ms
1 class Solution { 2 func calculate(_ s: String) -> Int { 3 var s = Array(s.characters) 4 var tk_i = 0 5 func nextToken() -> String? { 6 if tk_i >= s.count { return nil } 7 if s[tk_i] == " " { 8 tk_i += 1 9 return nextToken() 10 } 11 if s[tk_i] >= "0" && s[tk_i] <= "9" { 12 var j = tk_i + 1 13 while j < s.count && s[j] >= "0" && s[j] <= "9" { j += 1 } 14 let ret = String(s[tk_i..<j]) 15 tk_i = j 16 return ret 17 } 18 tk_i += 1 19 return String(s[tk_i - 1]) 20 } 21 22 var stack = [String]() 23 func eval(_ from: Int, _ to: Int) -> Int { 24 var ret = Int(stack[from])! 25 var i = from + 1 26 while i <= to { 27 let opr = stack[i] 28 let opnd = Int(stack[i + 1])! 29 if opr == "+" { 30 ret = ret + opnd 31 } else { 32 ret = ret - opnd 33 } 34 i += 2 35 } 36 return ret 37 } 38 39 while let tk = nextToken() { 40 if tk == ")" { 41 var j = stack.count - 1 42 while stack[j] != "(" { j -= 1 } 43 let res = eval(j + 1, stack.count - 1) 44 while stack.count > j { stack.removeLast() } 45 stack.append(String(res)) 46 } else { 47 stack.append(tk) 48 } 49 } 50 51 return eval(0, stack.count - 1) 52 } 53 }