# Python solution, easy to understand

• class Solution(object):

``````def __init__(self):
self.idx = 0

# call when idx pointing to the first index of a inner level
def calculate(self, s):
"""
:type s: str
:rtype: int
"""
stk = []
self.processWhiteSpaces(s)
while self.idx < len(s) and s[self.idx] != ')':
stk.append(self.getNextNum(s))
self.processWhiteSpaces(s)
sum = 0
for v in stk:
sum = sum + v
self.idx = self.idx + 1 #need to advance idx to pass over processed ')'
return sum

# process one adder at current level
def getNextNum(self, s):
isPlus = 1  #have to decclare
#MESE
# sign
if(s[self.idx] == '-' or s[self.idx] == '+'):
isPlus = self.processSign(s)
# white space
if self.processWhiteSpaces(s):
return 0
# (
if s[self.idx] == '(':
self.idx = self.idx + 1
return self.calculate(s) * isPlus

n = self.idx #cannot be too early
# number
while self.idx < len(s) and self.isNumChar(s):
self.idx = self.idx + 1
return int(s[n:self.idx]) * isPlus

def isNumChar(self, s):
if s[self.idx] >= '0' and s[self.idx] <= '9':
return True
else:
return False

def processSign(self, s):
isPlus = 1
if s[self.idx] == '-':
isPlus = -1
self.idx = self.idx + 1
return isPlus

def processWhiteSpaces(self, s):
while self.idx < len(s) and s[self.idx] == ' ':
self.idx = self.idx + 1
return self.idx == len(s)``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.