尧图网络 高端网站定制 · 原创设计
免费咨询热线
400-888-6620
免费获取方案
千问    LeetCode 224. Basic Calculator Java Implement
This is a classic stack sign tracking problem. The key insight is that parentheses only change the sign context — we don’t need to evaluate sub-expressions separately, just propagate the current sign into nested scopes.Core IdeaMaintain three things while scanning left to right:result: the accumulated sum so farsign: the current sign (1 or -1) for the next numberstack: stores the result and sign before each (, so we can restore them at )When we hit (, we push the current context and start fresh. When we hit ), we pop and merge the sub-result back.Java Implementationimport java.util.Deque;import java.util.ArrayDeque;class Solution {public int calculate(String s) {Deque stack new ArrayDeque();int result 0; // accumulated resultint sign 1; // current sign: 1 or -1int num 0; // current number being builtfor (int i 0; i s.length(); i) { char c s.charAt(i); if (Character.isDigit(c)) { num num * 10 (c - 0); } else if (c ) { result sign * num; num 0; sign 1; } else if (c -) { result sign * num; num 0; sign -1; } else if (c () { // Push current result and sign, then reset stack.push(result); stack.push(sign); result 0; sign 1; } else if (c )) { // Finish the number inside parentheses result sign * num; num 0; // Pop sign and previous result, merge result * stack.pop(); // sign before ( result stack.pop(); // result before ( } // spaces are ignored } // Dont forget the last number result sign * num; return result; }}Walkthrough with (1-(4-52)-3)Step Char Action result sign stack( push context 0 1 [0, 1]1 build num 0 1 [0, 1]result 1×1 1 -1 [0, 1]( push 0 1 [0,1, 1,-1]4 build 0 1 …result 1×4 4 -1 …5 build 4 -1 …result (-1)×5 -1 1 …2 build -1 1 …) result 1×21; ×(-1)-1; 10 0 — [0,1]result (-1)×0 0 -1 [0,1]3 build 0 -1 [0,1]end result (-1)×3 -3ComplexityTime: O(n) — single passSpace: O(d) where d is the max nesting depth of parenthesesKey Interview PointsWhy not recursion? Recursion works too (each ( spawns a recursive call), but the iterative stack approach avoids call-stack overhead and is easier to reason about in interviews.Why use Deque instead of Stack? Stack is a legacy class with synchronized methods — ArrayDeque is the modern, faster choice.Common bug: Forgetting to process the last number after the loop ends. Always add sign * num at the end.Follow-up: If the interviewer asks about * and /, you’d need a two-stack approach (operator stack operand stack) or a precedence-based parser — that’s LeetCode 772 (Basic Calculator III).Want me to also show the recursive version? It’s cleaner to read and sometimes easier to explain verbally in an interview.
RELATED

相关推荐

Outlook / Hotmail 邮箱监控配置流程

Outlook / Hotmail 邮箱监控配置流程

Outlook / Hotmail 邮箱监控配置流程 1. 注册 Microsoft Azure 账号 正常注册通常需要绑定 Visa 信用卡。 没有信用卡时,可尝试走学生通道。 学生通道需要 edu 邮箱。 可通过闲鱼获取 edu 邮箱;请注意合规与账号安全风险。 2. 进入 Microsoft Entra ID 注册完成后,在 Micro…

📅 2026/10/11 1:55:07
AI安全监管,为何越管越松?

AI安全监管,为何越管越松?

最近华盛顿挺热闹的。两份跟AI监管有关的信,几乎同时落地。 一封质疑白宫——有参议员给财长和商务部长写信,问是不是在科技巨头影响下,把强制AI安全筛查改成了自愿的。 另一封冲着一家AI公司——有参议员致信Anthropic CEO,问他一…

📅 2026/10/11 1:55:07
快速上线还是定制体验?如何借助 AI 选择支付接入方式

快速上线还是定制体验?如何借助 AI 选择支付接入方式

一、引言 上一篇,我们讨论了咖啡店如何根据单次点单、熟客复购和月卡续费,选择合适的收款方案。 现在我们聚焦于点单页面:顾客选好一杯拿铁,点击“去结账”,接下来会看到什么?是进入现成的收银台&#xf…

📅 2026/10/11 1:55:07
MORE NEWS

更多资讯

📰

STM32寄存器白话手册:手把手寄存器操作点亮LED

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

📰

6款网络工程师效率神器:从抓包到自动化监控的实战指南

干网络这一行,最累人的往往不是技术难题,而是那些重复、琐碎、还不能出错的操作。白天要配网、调策略、查日志,晚上还要蹲告警,别人看我捧着电脑好像很忙,其实大部分时间都花在App之间来回切换、手动重复同样的命令、等…

📰

Trae国际版实战:从配置到Builder模式,AI IDE高效开发指南

Trae国际版这阵子热度挺高,作为一个每天跟代码打交道的开发者,我第一时间装来折腾了一周,把几个主力项目都深度用了一遍。这篇文章不聊官方文档里已经写了的东西,就说说我实际使用中跑通的一套最佳实践:从安装配置、AI…

📰

MyEclipse 10.7汉化完整指南:Babel语言包安装与避坑实践

简介:一份针对 MyEclipse 10.7 的完整汉化资源包,面向中文环境下使用该 Eclipse 系 Java IDE 的开发者,覆盖菜单栏、代码编辑器、调试、运行配置及内置插件界面,可有效消除英文操作门槛,适合日常开发、教学演示与项目迁…

📰

AI芯片软硬件协同实战:算子融合、DMA调度与硅前验证全解析

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

📰

impeccable:可验证的工程质量标准与四层落地实践

1. “impeccable”不是一句空泛夸奖,而是可拆解、可验证、可复现的专业标准最近在多个技术评审会和设计交付现场,反复听到这个词被高频使用:“这个接口文档写得真impeccable”“UI动效的时序控制达到了impeccable级别”“CI流水线的失败归因逻…

TODAY

今日更新

THIS WEEK

本周精选

THIS MONTH

本月热门

读完文章,想聊聊您的网站?

告诉我们您的行业与需求,资深顾问一对一梳理方案与报价,全程免费。

📞 💬