#力扣 LeetCode224. 基本计算器 @FDDLC
发布日期:2021-06-30 21:02:55 浏览次数:2 分类:技术文章

本文共 777 字,大约阅读时间需要 2 分钟。

题目描述:

 

Java代码:

class Solution { //数字、+-、()、空格    public int calculate(String s) {        char[] a=new char[s.length()+1];//a[a.length-1]为'\0',充当边界屏障        int ans=0,top=0,slen=s.length(),num=0,operand=1,ai=0;//top为栈指针,operand为当前操作数的直接符号(操作数的正负还结结合父括号的正负来判断))        for(int si=0;si
='0'){//本题a[i]>='0'即可判断是数字字符 num=a[i]-'0'+num*10;//要防溢出 if(a[i+1]<'0'){ //i+1要防越界 ans+=num*operand*operandStack[top]; num=0; operand=1; } }else if(a[i]=='('){ operandStack[top+1]=operandStack[top++]*operand;//之后top++ operand=1; }else if(a[i]==')')top--; else if(a[i]=='-')operand=-operand; } return ans; }}

 

 

 

 

 

 

转载地址:https://liuxingchang.blog.csdn.net/article/details/114677383 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:#力扣 LeetCode227. 基本计算器 II @FDDLC
下一篇:#力扣 LeeCode914. 卡牌分组 @FDDLC

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月29日 03时34分22秒