请写一个整数计算器,支持加减乘三种运算和括号。
数据范围:,保证计算结果始终在整型范围内
要求:空间复杂度: ,时间复杂度
class Solution: def solve(self , s: str) -> int: # write code here return self.solve1(s)[0] def solve1(self, s: str,flag=False,minor=False) : # write code here i = 0 res = 0 left = 0 stack1 = [] last = 0 while i < len(s): # print(s[i],i,res) # time.sleep(0.1) while s[i] == '('&nbs***bsp;stack1: if s[i]=='(': if not stack1: left = i + 1 stack1.append('(') if s[i] == ')': stack1.pop() if not stack1: res,off = self.solve1(s[left:i]) # result i=left+off+1 break i += 1 if i>=len(s): break if s[i] == '*': temp,off = self.solve1(s[i + 1:],True) res = res * temp elif s[i] == '+': temp, off = self.solve1(s[i + 1:]) res = res + temp elif s[i] == '-': temp, off = self.solve1(s[i + 1:],minor=True) res = res - temp else: res, off = self.find_digit(s[i:]) i= i+off+1 if flag: return res,i elif minor: if i<len(s) and s[i]!='*': return res,i return res,i def find_digit(self, s): res = '' i = 0 for i, v in enumerate(s): if v >= '0' and v <= '9': res += v else: return int(res) if res else 0, i-1 break return int(res) if res else 0, i
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # 返回表达式的值 # @param s string字符串 待计算的表达式 # @return int整型 class Solution: def solve(self , s: str) -> int: class Stack(object): def __init__(self) -> None: self.item = [] def push(self, node): self.item.append(node) def pop(self): return self.item.pop() def top(self): return self.item[-1] def isEmpty(self): if self.item == []: return True else: return False def get_num(item, s) -> tuple(): li = [int(item)] while s: if s[0].isdigit(): li.append(int(s[0])) s = s[1:] else: break num = 0 for i in range(len(li)): num += li[i] * 10 ** (len(li)-i-1) return num, s def cal_bracket(s) -> tuple(): stack_str = Stack() stack_num = Stack() while s: item, s = s[0], s[1:] if item == '(': num, s = cal_bracket(s) stack_num.push(num) elif item == ')': return (sum(stack_num.item), s) # 本cal_bracket结束,返回: ()计算值,新的s elif item == '*': if s[0] == '(': num, s = cal_bracket(s[1:]) stack_num.push(stack_num.pop() * num) elif s[0].isdigit(): num, s = get_num(s[0], s[1:]) stack_num.push(stack_num.pop() * num) elif item == '+': pass elif item == '-': if s[0] == '(': num, s = cal_bracket(s[1:]) stack_num.push(-num) elif s[0].isdigit(): num, s = get_num(s[0], s[1:]) stack_num.push(-num) elif item.isdigit(): num, s = get_num(item, s) stack_num.push(num) s = s + ')' return cal_bracket(s)[0]
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # 返回表达式的值 # @param s string字符串 待计算的表达式 # @return int整型 # #中缀表达式问题 class Solution: def solve(self , s: str) -> int: # 定义优先级 level = { '*': 2, '-': 1, '+': 1, '(': 3, ')': -1 } num = [] signal = [] # write code here signal.append('(') s = s + ')' l = len(s) i = 0 while i < l: t = i while(s[t].isdigit()): t = t + 1 if(t != i): num.append(int(s[i:t])) i = t continue if(signal[-1] == '(' and s[i] != ')'): signal.append(s[i]) i = i + 1 elif (level[s[i]] > level[signal[-1]]): signal.append(s[i]) i = i + 1 elif (level[s[i]] <= level[signal[-1]]): if (s[i] == ')' and signal[-1] == '('): signal.pop() i = i + 1 else: a = num.pop() b = num.pop() num.append(eval("{} {} {}".format(b, signal[-1], a))) signal.pop() return num[-1]
class Solution { public: int cct(stack<int> &number, stack<char> &operation) { int tmp_1 = number.top(); number.pop(); int tmp_2 = number.top(); number.pop(); char tmp_o = operation.top(); operation.pop(); if (tmp_o == '+') return tmp_2 + tmp_1; else if (tmp_o == '-') return tmp_2 - tmp_1; else return tmp_1 * tmp_2; } int solve(string s) { stack<int> nmb; stack<char> opt; for (int i = 0; i < s.size(); i++) { for (int j = 0, i_ofs = 0;; i_ofs++) if (s[i + i_ofs] > 47) // 该字符是数字。 j = j * 10 + s[i + i_ofs] - 48; else // 该字符是运算符。 { if (i_ofs) nmb.push(j); i += i_ofs; break; } if (i >= s.size()) break; while (s[i] % 2 && !opt.empty() && opt.top() != '(') // s[i] == ')', '+', '-'; nmb.push(cct(nmb, opt)); s[i] == ')' ? opt.pop() : opt.push(s[i]); } while (!opt.empty()) nmb.push(cct(nmb, opt)); return nmb.top(); } };
class Solution: def solve(self , s: str) -> int: return eval(s)
class Solution: def solve(self , s ): # write code here self.op_priority = {'+': 0, '-': 0, '*': 1} suffix_expression = self.infix2suffix(s) return self.suffix2result(suffix_expression) def suffix2result(self, suffix_expression): stack = [] for op in suffix_expression: if op not in ['+', '-', '*']: stack.append(int(op)) else: num1 = stack.pop() num2 = stack.pop() if op == '+': temp = num2 + num1 elif op == '-': temp = num2 - num1 else: temp = num2 * num1 stack.append(temp) return stack.pop() def infix2suffix(self, s): sop, L, i = [], [], 0 while i < len(s): if self.isNum(s[i]): num = '' while i < len(s) and self.isNum(s[i]): num += s[i] i += 1 L.append(num) elif s[i] == '(': sop.append(s[i]) i += 1 elif s[i] == ')': while sop[-1] != '(': L.append(sop.pop()) sop.pop() i += 1 else: while True: if sop == []&nbs***bsp;sop[-1] == '('&nbs***bsp;(self.op_priority[s[i]] > self.op_priority[sop[-1]]): sop.append(s[i]) break else: L.append(sop.pop()) i += 1 while sop: L.append(sop.pop()) return L def isNum(self, ch): if ch in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']: return True else: return False