class Solution {
public:
int calculate(string s) {
stack<int> num;
stack<char> symbol;
for(int i=0;i<s.length();i++){
if(s[i]==‘ ‘) continue;
else if(s[i]==‘(‘||s[i]==‘+‘||s[i]==‘-‘) symbol.push(s[i]);
else if(s[i]>=‘0‘&&s[i]<=‘9‘){
for(int j=i+1;j<s.length();j++){
if(s[j]<‘0‘||s[j]>‘9‘){
num.push(stoi(s.substr(i,j-i)));
i=j-1;
break;
}
}
}
else if(s[i]==‘)‘){
stack<int> tem_i;
stack<char> tem_c;
while(symbol.top()!=‘(‘){
tem_i.push(num.top());
num.pop();
tem_c.push(symbol.top());
symbol.pop();
}
symbol.pop();
while(!tem_c.empty()){
char c=tem_c.top();
tem_c.pop();
if(c==‘+‘){
int a=tem_i.top();
tem_i.pop();
int b=tem_i.top();
tem_i.pop();
tem_i.push(a+b);
}
if(c==‘-‘){
int a=tem_i.top();
tem_i.pop();
int b=tem_i.top();
tem_i.pop();
tem_i.push(a-b);
}
}
num.push(tem_i.top());
tem_i.pop();
}
}
stack<int> tem_i;
stack<char> tem_c;
while(!symbol.empty()){
tem_i.push(num.top());
num.pop();
tem_c.push(symbol.top());
symbol.pop();
}
while(!tem_c.empty()){
char c=tem_c.top();
tem_c.pop();
if(c==‘+‘){
int a=tem_i.top();
tem_i.pop();
int b=tem_i.top();
tem_i.pop();
tem_i.push(a+b);
}
if(c==‘-‘){
int a=tem_i.top();
tem_i.pop();
int b=tem_i.top();
tem_i.pop();
tem_i.push(a-b);
}
}
return tem_i.top();
}
int stoi(string s){
int res=0;
for(int i=0;i<s.length();i++){
res=res*10+(s[i]-‘0‘);
}
return res;
}
};
LeetCode() Basic Calculator 不知道哪里错了
时间: 2024-10-10 13:40:15
LeetCode() Basic Calculator 不知道哪里错了的相关文章
leetcode Basic Calculator
题目连接 https://leetcode.com/problems/basic-calculator/ Basic Calculator Description 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
LeetCode——Basic Calculator II
Description: Implement a basic calculator to evaluate a simple expression string. The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should truncate toward zero. You may assume that
LeetCode——Basic Calculator
Description: 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 . You may assume that the given e
[LeetCode] Basic Calculator II 基本计算器之二
Implement a basic calculator to evaluate a simple expression string. The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should truncate toward zero. You may assume that the given ex
解题报告:LeetCode Basic Calculator(简单计算器)
题目出处:https://leetcode.com/problems/basic-calculator/题意描述: 给定一个只含加减号,括号,空格和非负数的合法字符串,在不调用库函数eval的条件下求出其值 解决思路: 由于此题只有加减号,因此不存在运算符优先级的问题,只需要稍微注意一下括号即可. 因此,可以用两个栈ops,numbers来分别存储未处理的运算符和未处理的数字,并对字符串进行一次扫描.对于对于扫描到的任意字符是s[i],做如下处理: 若s[i]为空格,则继续扫描. 若s[i]为左
[LeetCode] Basic Calculator II
The basic idea of is as follows: Maintain a deque operands for the numbers and another deque operations for the operators +, -, *,/`. Scan the expression from left to right, each time we meet a digit, extract the whole number with and after it, push
[LeetCode] Basic Calculator IV 基本计算器之四
Given an expression such as expression = "e + 8 - a + 5" and an evaluation map such as {"e": 1}(given in terms of evalvars = ["e"] and evalints = [1]), return a list of tokens representing the simplified expression, such as [
[LeetCode][JavaScript]Basic Calculator
Basic Calculator 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 . You may assume that the giv
【Leetcode】Basic Calculator II
题目链接:https://leetcode.com/problems/basic-calculator-ii/ 题目: Implement a basic calculator to evaluate a simple expression string. The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division s