[LeetCode]224. Basic Calculator(模拟,栈)

题目链接:https://leetcode.com/problems/basic-calculator/#/description

题意:计算一个只含有括号、加减、非负数的表达式。

用一个栈记数,一个栈记符号。

 1 class Solution {
 2 public:
 3     int calculate(string s) {
 4         int n = s.length();
 5         stack<int> st;
 6         stack<char> op;
 7         int ret = 0;
 8         op.push(‘+‘);
 9         for(int i = 0; i < n; i++) {
10             if(isdigit(s[i])) {
11                 int x = 0;
12                 while(isdigit(s[i])) x = x * 10 + s[i++] - ‘0‘;
13                 i--;
14                 char o = op.top(); op.pop();
15                 if(o == ‘+‘) ret += x;
16                 else ret -= x;
17             }
18             else if(s[i] == ‘+‘ || s[i] == ‘-‘) op.push(s[i]);
19             else if(s[i] == ‘(‘) {
20                 st.push(ret);
21                 op.push(‘+‘);
22                 ret = 0;
23             }
24             else if(s[i] == ‘)‘) {
25                 char o = op.top(); op.pop();
26                 if(o == ‘+‘) ret += st.top();
27                 else ret = st.top() - ret;
28                 st.pop();
29             }
30         }
31         return ret;
32     }
33 };
时间: 2024-10-23 06:29:12

[LeetCode]224. Basic Calculator(模拟,栈)的相关文章

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

(medium)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

【LeetCode】224. 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][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

【Leetcode】Basic Calculator

题目链接:https://leetcode.com/problems/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 em

Leetcode 227. Basic Calculator II JAVA语言

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 OJ Basic Calculator II

Basic Calculator II 题目 思路 和这个一样:Basic Calculator I 代码 class ExpressionTransformation { public: string trans_to_postfix_expression_to_s(string); // 将得到的表达式转化为后缀表达式 long long int calculate_from_postfix_expression(); // 根据后缀表达式计算值 private: vector<string