力扣面试150 逆波兰表达式求值 栈 模拟栈

Problem: 150. 逆波兰表达式求值

👨‍🏫 参考题解

Java 复制代码
class Solution {
	//纯数组模拟栈实现(推荐)   3 ms	36 MB
	public static int evalRPN(String[] tokens) {
		int[] numStack = new int[tokens.length / 2 + 1];
		int index = 0;
		for (String s : tokens) {
			switch (s) {
			case "+":
				numStack[index - 2] += numStack[--index];
				break;
			case "-":
				numStack[index - 2] -= numStack[--index];
				break;
			case "*":
				numStack[index - 2] *= numStack[--index];
				break;
			case "/":
				numStack[index - 2] /= numStack[--index];
				break;
			default:
				// numStack[index++] = Integer.valueOf(s);
				//valueOf改为parseInt,减少自动拆箱装箱操作
				numStack[index++] = Integer.parseInt(s);
				break;
			}
		}
		return numStack[0];
	}
}
相关推荐
√尖尖角↑3 小时前
力扣——【1991. 找到数组的中间位置】
算法·蓝桥杯
Allen Wurlitzer3 小时前
算法刷题记录——LeetCode篇(1.8) [第71~80题](持续更新)
算法·leetcode·职场和发展
百锦再5 小时前
五种常用的web加密算法
前端·算法·前端框架·web·加密·机密
碳基学AI6 小时前
北京大学DeepSeek内部研讨系列:AI在新媒体运营中的应用与挑战|122页PPT下载方法
大数据·人工智能·python·算法·ai·新媒体运营·产品运营
独家回忆3647 小时前
每日算法-250410
算法
袖清暮雨7 小时前
Python刷题笔记
笔记·python·算法
熬夜造bug7 小时前
LeetCode Hot100 刷题笔记(1)—— 哈希、双指针、滑动窗口
笔记·leetcode·hot100
风掣长空7 小时前
八大排序——c++版
数据结构·算法·排序算法
流星白龙8 小时前
【C++算法】50.分治_归并_翻转对
c++·算法
Java致死10 小时前
费马小定理
算法·费马小定理