leetcode 227. 基本计算器 II(栈)

给你一个字符串表达式 s ,请你实现一个基本计算器来计算并返回它的值。

整数除法仅保留整数部分。

示例 1:

输入:s = “3+2*2”
输出:7

解题思路

利用两个栈,一个记录操作数,一个记录操作符,用-1和1表示操作符加减

代码

class Solution {public int calculate(String s) {Stack<Integer> stack=new Stack<>();Stack<Integer> characterStack=new Stack<>();int n=s.length();int i = 0,sign=1,cur=0;while ( i < n) {char c = s.charAt(i);if(c==' '){i++;continue;}if(Character.isDigit(c))//取出操作数进栈{int sum=0;while (i < n&&Character.isDigit(s.charAt(i))){sum*=10;sum+=s.charAt(i)-'0';i++;}stack.push(sum);i--;}else {if(c=='+'||c=='-'){if(!characterStack.isEmpty())//前面出现同级的加减运算,计算前面的{stack.push(stack.pop()*characterStack.pop()+stack.pop());}characterStack.push(c=='+'?1:-1);//操作符入栈}else// 操作符为*/{int sum=0;i++;while (s.charAt(i)==' ') i++;while (i < n&&Character.isDigit(s.charAt(i)))//取下一个操作数进行操作{sum*=10;sum+=s.charAt(i)-'0';i++;}if(c=='*')stack.push(stack.pop()*sum);elsestack.push(stack.pop()/sum);i--;}}i++;}return  stack.size()==1?stack.pop():stack.pop()*characterStack.pop()+stack.pop();}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/391627.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

100米队伍,从队伍后到前_我们的队伍

100米队伍,从队伍后到前The last twelve months have brought us a presidential impeachment trial, the coronavirus pandemic, sweeping racial justice protests triggered by the death of George Floyd, and a critical presidential election. News coverage of these e…

idea使用 git 撤销commit

2019独角兽企业重金招聘Python工程师标准>>> 填写commit的id 就可以取消这一次的commit 转载于:https://my.oschina.net/u/3559695/blog/1596669

ES6标准入门(第二版)pdf

下载地址&#xff1a;网盘下载 内容简介 ES6&#xff08;又名 ES2105&#xff09;是 JavaScript 语言的新标准&#xff0c;2015 年 6 月正式发布后&#xff0c;得到了迅速推广&#xff0c;是目前业界超级活跃的计算机语言。《ES6标准入门&#xff08;第2版&#xff09;》…

hexo博客添加暗色模式_我如何向网站添加暗模式

hexo博客添加暗色模式同一个网站&#xff0c;两种不同的配色方案 (Same website, two different color schemes) Last year I made it a point to redesign my website from scratch. I wanted something simple and minimalist looking that clearly stated what this was — …

leetcode 331. 验证二叉树的前序序列化

序列化二叉树的一种方法是使用前序遍历。当我们遇到一个非空节点时&#xff0c;我们可以记录下这个节点的值。如果它是一个空节点&#xff0c;我们可以使用一个标记值记录&#xff0c;例如 #。_9_/ \3 2/ \ / \4 1 # 6 / \ / \ / \ # # # # # # 例如&#xff0…

mongodb数据可视化_使用MongoDB实时可视化开放数据

mongodb数据可视化Using Python to connect to Taiwan Government PM2.5 open data API, and schedule to update data in real time to MongoDB — Part 2使用Python连接到台湾政府PM2.5开放数据API&#xff0c;并计划将数据实时更新到MongoDB —第2部分 目标 (Goal) This ti…

4.kafka的安装部署

为了安装过程对一些参数的理解&#xff0c;我先在这里提一下kafka一些重点概念,topic,broker,producer,consumer,message,partition,依赖于zookeeper, kafka是一种消息队列,他的服务端是由若干个broker组成的&#xff0c;broker会向zookeeper&#xff0c;producer生成者对应一个…

javascript初学者_针对JavaScript初学者的调试技巧和窍门

javascript初学者by Priyanka Garg由Priyanka Garg My intended audience for this tutorial is beginner programmers. You’ll learn about frustration-free debugging with chrome dev tools.本教程的目标读者是初学者。 您将学习使用chrome开发工具进行无挫折的调试。 D…

leetcode 705. 设计哈希集合

不使用任何内建的哈希表库设计一个哈希集合&#xff08;HashSet&#xff09;。 实现 MyHashSet 类&#xff1a; void add(key) 向哈希集合中插入值 key 。 bool contains(key) 返回哈希集合中是否存在这个值 key 。 void remove(key) 将给定值 key 从哈希集合中删除。如果哈希…

ecshop 前台个人中心修改侧边栏 和 侧边栏显示不全 或 导航现实不全

怎么给个人中心侧边栏加项或者减项 在模板文件default/user_menu.lbi 文件里添加或者修改,一般看到页面都会知道怎么加,怎么删,这里就不啰嗦了 添加一个栏目以后,这个地址跳的页面怎么写 这是最基本的一个包括左侧个人信息,头部导航栏 <!DOCTYPE html PUBLIC "-//W3C//…

leetcode 706. 设计哈希映射

不使用任何内建的哈希表库设计一个哈希映射&#xff08;HashMap&#xff09;。 实现 MyHashMap 类&#xff1a; MyHashMap() 用空映射初始化对象 void put(int key, int value) 向 HashMap 插入一个键值对 (key, value) 。如果 key 已经存在于映射中&#xff0c;则更新其对应…

数据库语言 数据查询_使用这种简单的查询语言开始查询数据

数据库语言 数据查询Working with data is becoming an increasingly important skill in the modern workplace. 在现代工作场所中&#xff0c;处理数据已成为越来越重要的技能。 Data is no longer the domain of analysts and software engineers. With todays technology,…

面向对象编程思想-观察者模式

一、引言 相信猿友都大大小小经历过一些面试&#xff0c;其中有道经典题目&#xff0c;场景是猫咪叫了一声&#xff0c;老鼠跑了&#xff0c;主人被惊醒&#xff08;设计有扩展性的可加分&#xff09;。对于初学者来说&#xff0c;可能一脸懵逼&#xff0c;这啥跟啥啊是&#x…

typescript 使用_如何使用TypeScript轻松修改Minecraft

typescript 使用by Josh Wulf通过乔什沃尔夫(Josh Wulf) 如何使用TypeScript轻松修改Minecraft (How to modify Minecraft the easy way with TypeScript) Usually, modifying Minecraft requires coding in Java, and a lot of scaffolding. Now you can write and share Min…

Python:在Pandas数据框中查找缺失值

How to find Missing values in a data frame using Python/Pandas如何使用Python / Pandas查找数据框中的缺失值 介绍&#xff1a; (Introduction:) When you start working on any data science project the data you are provided is never clean. One of the most common …

监督学习-回归分析

一、数学建模概述 监督学习&#xff1a;通过已有的训练样本进行训练得到一个最优模型&#xff0c;再利用这个模型将所有的输入映射为相应的输出。监督学习根据输出数据又分为回归问题&#xff08;regression&#xff09;和分类问题&#xff08;classfication&#xff09;&#…

leetcode 54. 螺旋矩阵(递归)

给你一个 m 行 n 列的矩阵 matrix &#xff0c;请按照 顺时针螺旋顺序 &#xff0c;返回矩阵中的所有元素。 示例 1&#xff1a; 输入&#xff1a;matrix [[1,2,3],[4,5,6],[7,8,9]] 输出&#xff1a;[1,2,3,6,9,8,7,4,5] 示例 2&#xff1a; 输入&#xff1a;matrix [[1,…

微服务架构技能

2019独角兽企业重金招聘Python工程师标准>>> 微服务架构技能 博客分类&#xff1a; 架构 &#xff08;StuQ 微服务技能图谱&#xff09; 2课程简介 本课程分为基础篇和高级篇两部分&#xff0c;旨在通过完整的案例&#xff0c;呈现微服务的开发、测试、构建、部署、…

phpstorm 调试_PhpStorm中的多用户调试

phpstorm 调试by Ray Naldo雷纳尔多(Ray Naldo) PhpStorm中的多用户调试 (Multi-User Debugging in PhpStorm) 使用Xdebug和DBGp代理 (Using Xdebug and DBGp Proxy) “Er, wait a minute… Don’t you just use xdebug.remote_connect_back which has been introduced since …

Tableau Desktop认证:为什么要关心以及如何通过

Woah, Tableau!哇&#xff0c;Tableau&#xff01; By now, almost everyone’s heard of the data visualization software that brought visual analytics to the public. Its intuitive drag and drop interface makes connecting to data, creating graphs, and sharing d…