当前位置:网站首页>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;
}
};
边栏推荐
- [jetcache] jetcache configuration description and annotation attribute description
- Rust ownership (very important)
- Shell implements basic file operations (cutting, sorting, and de duplication)
- leetcode-2280:表示一个折线图的最少线段数
- Rust所有权(非常重要)
- Centos7 one click compilation to build MySQL script
- 【AutoSAR 五 方法论】
- [IELTS reading] Wang Xiwei reading P2 (reading fill in the blank)
- Extension of flutter
- [shutter] image component (load network pictures | load static pictures | load local pictures | path | provider plug-in)
猜你喜欢

Hdu3507 (slope DP entry)

Unity learns from spaceshooter to record the difference between fixedupdate and update in unity for the second time
![[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)](/img/73/19e2e0fc5ea6f05e34584ba40a452d.jpg)
[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)

Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径

百度智能云牵头打造智能云综合标准化平台
![[MCU project training] eight way answering machine](/img/a3/6a50619cd16269bf485a4a273677aa.jpg)
[MCU project training] eight way answering machine

文件操作IO-Part2

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

Rust所有权(非常重要)

Rust string slicing, structs, and enumeration classes
随机推荐
mysql 多表联合删除
【AutoSAR 二 AppL概述】
Hundreds of continuous innovation to create free low code office tools
How SQLSEVER removes data with duplicate IDS
University of Oslo: Li Meng | deep reinforcement learning based on swing transformer
Free we media essential tools sharing
tail -f 、tail -F、tailf的区别
【Pulsar文档】概念和架构/Concepts and Architecture
leetcode-1964:找出到每个位置为止最长的有效障碍赛跑路线
NC24325 [USACO 2012 Mar S]Flowerpot
【AutoSAR 五 方法论】
【AutoSAR 九 C/S原理架构】
Cordova plugin device obtains the device information plug-in, which causes Huawei to fail the audit
leetcode-224:基本计算器
深度剖析数据在内存中的存储
图解网络:什么是虚拟路由器冗余协议 VRRP?
In the first half of 2022, there are 10 worth seeing, and each sentence can bring you strength!
Arduino开发之按键检测与正弦信号输出
kubernetes资源对象介绍及常用命令(五)-(NFS&PV&PVC)
helm 基础学习