leetcode-Basic Calculator-224

输入一个字符串,包含数字或者+,-,(,),求这个表达式的结果

1.没有括号时顺序执行

2.有括号时先计算括号内的表达式

这里用一个栈来保存遇到括号(之前的表达式的结果,也就是说没遇到括号之前,按顺序正常执行设结果为x,遇到括号之后要先计算括号内的表达式,设结果为y,然后再把y拿出来和x顺序执行。因此计算括号内的表达式时要先保存之前计算的x的值,为什么要用栈呢,因为括号可以嵌套,如a-(b-(c+d)),遇到第一个括号时,要保存a,然后进入括号,又遇到了括号要保存b,然后进入第二个括号得到c+d之后返回和b进行计算,再返回和a进行计算,这就是栈的后进先出。当然还要把符号也压栈

这里对+和-直接用一个标志flag=1和flag=-1来表示,这样计算a-b时可以直接a+b*flag

 1 class Solution {
 2 public:
 3     int calculate(string s) {
 4         int len=s.size();
 5         if(len==0) return 0;
 6         stack<int> st;
 7         int ans=0;
 8         int i=0;
 9         int flag=1;
10         while(i<len){
11             if(s[i]==‘ ‘){
12                 i++;
13                 continue;
14             }
15             if(isdigit(s[i])){
16                 int num=0;
17                 while(i<len&&isdigit(s[i])){
18                     num=num*10+s[i]-‘0‘;
19                     i++;
20                 }
21                 ans+=num*flag;
22                 continue;
23             }
24             if(s[i]==‘+‘) flag=1;
25             else if(s[i]==‘-‘) flag=-1;
26             else if(s[i]==‘(‘){
27                 st.push(ans);
28                 ans=0;
29                 st.push(flag);
30                 flag=1;
31             }
32             else{
33                 int a=st.top();
34                 st.pop();
35                 int b=st.top();
36                 st.pop();
37                 ans=b+a*ans;
38             }
39             i++;
40         }
41         return ans;
42     }
43 };
时间: 2024-10-05 19:03:52

leetcode-Basic Calculator-224的相关文章

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 不知道哪里错了

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.pu

[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 224: Basic Calculator

Basic Calculator Total Accepted: 2801 Total Submissions: 18103 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

Java for LeetCode 224 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 given expression is