当前位置:网站首页>Stack implementation calculator
Stack implementation calculator
2022-07-01 08:13:00 【The world is ordinary】
leetcode 224 Use stack to realize calculator .
Extend the , It's not just about adding and subtracting , Multiplication and division are also possible .
In addition, negative numbers can be realized for (-2) + 3 This operation of negative numbers .
Double stack
opv: Digital stack
opc: Symbol stack
Priority words :
- If the top of the stack is plus or minus , Current is addition and subtraction , You can calculate .
- If the top of the stack is multiplication and division , Currently, it is not an open parenthesis , You can calculate .
- If the top of the stack is an open bracket , Currently, if it is a right parenthesis , Just directly Once out of the stack , Otherwise, it doesn't count .
Dealing with negative numbers :
Negative numbers are not preceded by numbers , There is no right parenthesis , So what satisfies these two characteristics is negative numbers .
The processing of numbers :
Pay attention to the use of numbers in intermediate calculation long Make a strong turn . Otherwise, if it is INT_MAX Explosive int.
Treatment of left and right parentheses :
The right bracket is not put on the stack . The left bracket does not count .
Handling of spaces :
only Clear the digital counter cur, No calculation .
class Solution {
public:
int calculate(string s) {
stack<int>opv;
stack<char>opc;
auto cal = [&]() {
char ch = opc.top(); opc.pop();
int a = opv.top(); opv.pop();
int b = opv.top(); opv.pop();
if (ch == '+') opv.push(b + a);
else if (ch == '-') opv.push(b - a);
else if (ch == '*') opv.push(b * a);
else if (ch == '/') opv.push(b / a);
// printf("%d %c %d = %d\n", b, ch, a, opv.top());
};
auto check = [&](char ch) -> int {
char top = opc.top();
if (top == '+' || top == '-') {
if (ch == '+' || ch == '-' || ch == ')')
return 1;
}
else if (top == '*' || top == '/') {
if (ch != '(')
return 1;
}
else if (top == '(' && ch == ')') return 0;
return -1;
};
s += ' ';
int cur = -1;
opc.push('#');
char pre = '#';
for (char ch : s) {
if (isdigit(ch)) {
if (cur == -1) cur = 0;
cur = (long)cur * 10 + ch - '0'; // 2. Prevent explosion int
// printf("%d\n", cur);
}
else {
if (cur != -1) opv.push(cur);
cur = -1;
if (ch == ' ') continue; // Space
if (ch == '-' && (!isdigit(pre) && pre != ')')) { // negative
// printf("pre = %c\n", pre);
opv.push(0);
}
while (check(ch) > 0) cal();
if (check(ch) == 0) // Right bracket , The left bracket shows a , Not all of them
opc.pop();
if (ch != ')') // The right bracket is not put in
opc.push(ch);
}
if (pre != ' ') pre = ch;
}
while (opc.top() != '#') cal();
return opv.top();
}
};
边栏推荐
- Book of quantitative trading - reading notes of the man who conquers the market
- How to troubleshoot SharePoint online map network drive failure?
- LSTM of RNN
- Li Kou daily question - Day 32 -1822 Symbol of array element product
- Vhost kick & call principle
- 01 NumPy介绍
- OJ input and output exercise
- Aardio - 阴影渐变文字
- php laravel微信支付
- Deep learning systematic learning
猜你喜欢

038 network security JS

Five combination boxing, solving six difficult problems on campus and escorting the construction of educational informatization

源代码加密的意义和措施

Serial port oscilloscope software ns-scope

Day5: scanner object, next() and nextline(), sequential structure, selection structure, circular structure
![[getting started] input n integers and output the smallest K of them](/img/b8/20852484f10bc968d529e9c1ff5480.png)
[getting started] input n integers and output the smallest K of them

【入门】输入整型数组和排序标识,对其元素按照升序或降序进行排序

QT -- 1. QT connection database

软键盘高度报错

Access report realizes subtotal function
随机推荐
Aardio - [problem] the problem of memory growth during the callback of bass Library
源代码加密的意义和措施
01 NumPy介绍
Precautions and skills in using regular expressions in golang
How can beginners correctly understand Google's official suggested architectural principles (questions?)
Instead of houses, another kind of capital in China is rising
Implementation and encapsulation of go universal dynamic retry mechanism
How outlook puts together messages with the same discussion
Gru of RNN
Access报表实现小计功能
On June 30, 2022, the record of provincial competition + national competition of Bluebridge
Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
[website architecture] solve 90% of distributed transactions in one move, and introduce the working principles and application scenarios of database transactions and distributed transactions
P4 installation bmv2 detailed tutorial
[getting started] enter the integer array and sorting ID, and sort its elements in ascending or descending order
go通用动态重试机制解决方案的实现与封装
【批处理DOS-CMD命令-汇总和小结】-Cmd窗口中常用操作符(<、<<、&<、>、>>、&>、&、&&、||、|、()、;、@)
Gdip - hatchBrush图案表
防“活化”照片蒙混过关,数据宝“活体检测+人脸识别”让刷脸更安全
量化交易之读书篇 - 《征服市场的人》读书笔记