当前位置:网站首页>Leetcode-224: basic calculator
Leetcode-224: basic calculator
2022-07-03 00:47:00 【Chrysanthemum headed bat】
leetcode-224: Basic calculator
subject
Topic linking
Here's a string expression for you s , Please implement a basic calculator to calculate and return its value .
Be careful : Any built-in functions that evaluate strings as mathematical expressions are not allowed , such as eval() .
Example 1:
Input :s = "1 + 1"
Output :2
Example 2:
Input :s = " 2-1 + 2 "
Output :3
Example 3:
Input :s = "(1+(4+5+2)-3)+(6+8)"
Output :23
Problem solving
Method 1 : Stack
class Solution {
public:
int calculate(string s) {
int res=0,num=0,sign=1;
stack<int> st;
for(char c:s){
if(c>='0'&&c<='9'){
num=num*10+(c-'0');
}
else if(c=='+'||c=='-'){
res+=sign*num;
num=0;
sign=c=='+'?1:-1;
}
else if(c=='('){
st.push(res);
st.push(sign);
res=0;
sign=1;
}
else if(c==')'){
res+=sign*num;
num=0;
res*=st.top();
st.pop();
res+=st.top();
st.pop();
}
}
res+=sign*num;
return res;
}
};
边栏推荐
- Two common methods and steps of character device registration
- 简单聊聊运维监控的其他用途
- Hdu3507 (slope DP entry)
- 指针初阶(基础)
- Kubernetes simple introduction to writing YML
- Overlay of shutter (Pop-Up)
- 【AutoSAR 十二 模式管理】
- Introduction of UART, RS232, RS485, I2C and SPI
- Baidu AI Cloud takes the lead in building a comprehensive and standardized platform for smart cloud
- Andorid gets the system title bar height
猜你喜欢

tail -f 、tail -F、tailf的区别

Use Jenkins II job

Explain in detail the significance of the contour topology matrix obtained by using the contour detection function findcontours() of OpenCV, and how to draw the contour topology map with the contour t

Why is the website slow to open?

Hdu3507 (slope DP entry)

File operation io-part2

Shell implements basic file operations (cutting, sorting, and de duplication)

Rust所有权(非常重要)
![[shutter] image component (load network pictures | load static pictures | load local pictures | path | provider plug-in)](/img/7e/4f9d96edd04e9ffb26434baf34aa43.jpg)
[shutter] image component (load network pictures | load static pictures | load local pictures | path | provider plug-in)

One of the reasons why setinterval timer does not take effect in ie: the callback is the arrow function
随机推荐
Shell implements basic file operations (cutting, sorting, and de duplication)
Shell 实现文件基本操作(sed-编辑、awk-匹配)
FAQ | FAQ for building applications for large screen devices
Sentry developer contribution Guide - configure pycharm
[IELTS reading] Wang Xiwei reading P1 (reading judgment question)
mysql 多表联合删除
AttributeError: ‘tuple‘ object has no attribute ‘layer‘问题解决
【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(中)-- 搜索建议
How SQLSEVER removes data with duplicate IDS
Cordova plugin device obtains the device information plug-in, which causes Huawei to fail the audit
Briefly talk about other uses of operation and maintenance monitoring
Kubernetes resource object introduction and common commands (V) - (NFS & PV & PVC)
The most painful programming problem in 2021, adventure of code 2021 Day24
helm 基础学习
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
Centos7 one click compilation to build MySQL script
Win10 多种方式解决无法安装.Net3.5的问题
世平信息首席科学家吕喆:构建以数据和人员为中心的安全能力
Preview word documents online
如何系统学习机器学习