`
Thomas会Coding
  • 浏览: 93041 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

leetcode-Evaluate Reverse Polish Notation

阅读更多

Valid operators are +-*/. Each operand may be an integer or another expression.

public class Solution {
    public int evalRPN(String[] tokens) {
       int result=0;
		List<Integer> arrayList=new LinkedList<Integer>();
		int temp1=0;
		int temp2=0;
		for(String temp:tokens){
			if(temp.compareTo("+")==0){
				temp1=arrayList.remove(arrayList.size()-1);
				temp2=arrayList.remove(arrayList.size()-1);
				result=temp2+temp1;
				arrayList.add(arrayList.size(),result);
			}
			else if(temp.compareTo("-")==0){
				temp1=arrayList.remove(arrayList.size()-1);
				temp2=arrayList.remove(arrayList.size()-1);
				result=temp2-temp1;
				arrayList.add(arrayList.size(),result);
			}else if(temp.compareTo("*")==0){
				temp1=arrayList.remove(arrayList.size()-1);
				temp2=arrayList.remove(arrayList.size()-1);
				result=temp2*temp1;
				arrayList.add(arrayList.size(),result);
			}else if(temp.compareTo("/")==0){
				temp1=arrayList.remove(arrayList.size()-1);
				temp2=arrayList.remove(arrayList.size()-1);
				result=temp2/temp1;
				arrayList.add(arrayList.size(),result);
			}else{
				arrayList.add(arrayList.size(),Integer.parseInt(temp));
			}
		}
		
		result=arrayList.get(0);
		return result;
    }
}

 思路:将数组中的内容遍历,遇见数字就存入list中,遇见符号就弹出,将计算的结果再存入list中。

一开始用temp==“/”老是报runtime error 。换成temp.compareTo("/")==0就Accepted了

0
3
分享到:
评论

相关推荐

    leetcode不会-evaluate-reverse-polish-notation:评估反向波兰语表达

    leetcode 不会评估反向波兰表示法 以逆波兰表示法计算算术表达式的值。 有效的运算符是 +、-、*、/。 每个操作数可以是整数或其他表达式。 笔记: 两个整数之间的除法应向零截断。 给定的 RPN 表达式始终有效。 这...

    Leetcode的ac是什么意思-LeetCodeInJava:leetcode-java

    Leetcode的ac是什么意思 LeetCodeInJava List #98 Validate Binary Search Tree #100 Same Tree #104 Maximum Depth of Binary Tree #122 Best Time to Buy and Sell Stock II #136 Single Number #150 Evaluate ...

    gasstationleetcode-leetcode-in-niuke:在牛客网上的在线编程中的leetcode在线编程题解

    evaluate-reverse-polish-notation 链表 sort-list 排序 insertion-sort-list 树 binary-tree-postorder-traversal 树 binary-tree-preorder-traversal 链表 linked-list-cycle-ii 链表 linked-list-cycle 链表 copy...

    最大公共字符串leetcode-leetcode:坚持每周刷一道LeetCode题

    最大公共字符串leetcode leetcode刷题打卡 接下来,坚持每周至少刷一道LeetCode题,...Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are+,-,*,/. Each operand may b

    逆波兰表达式求值

    链接:https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/ 思路 运用lambda表达式和字典对运算符进行重载,再通过栈 stacks 对运算对象及中间结果进行存储 Python代码 class Solution: def ...

    LeetCode:LeetCode解决方案

    LeetCodeLeetCode solutions(Java)树Minimum Depth of Binary Tree栈evaluate-reverse-polish-notation穷举max-points-on-a-line链表sort-list排序insertion-sort-list树binary-tree-postorder-traversal树binary-...

    leetcode分发糖果-ForDaFactory:使用C++的个人leetcode解决方案

    leetcode分发糖果 ...150-逆波兰表达式求值:evaluate-reverse-polish-notation 155-最小栈:min-stack Tree 普通二叉树 94-二叉树中序遍历:inorder-traversal 100-相同的二叉树:same-tree 101-对称二叉树:symmetric-

    Coding Interview In Java

    3 Evaluate Reverse Polish Notation 21 4 Isomorphic Strings 25 5 Word Ladder 27 6 Word Ladder II 29 7 Median of Two Sorted Arrays 33 8 Kth Largest Element in an Array 35 9 Wildcard Matching 37 10 ...

    leetcode中国-leetcode:刷算法了

    Evaluate Reverse Polish Notation 逆波兰的后半截实现 Linked List Cycle 快慢指针 Linked List Cycle II 快慢指针再加个初始指针 慢指针到链表开始位置时, 快指针总是比他快k步(每次移动快1步, 移动k次), 第一次...

    LeetCode最全代码

    190 | [Reverse Bits](https://leetcode.com/problems/reverse-bits/) | [C++](./C++/reverse-bits.cpp) [Python](./Python/reverse-bits.py) | _O(1)_ | _O(1)_ | Easy ||| 191 |[Number of 1 Bits]...

    leetcode双人赛-Algorithm:leetcode算法刷题总结,长期更新,欢迎探讨与指正

    [leetcode]150.Evaluate Reverse Polish Notation 题目: ["2", "1", "+", "3", "*"] -&gt; ((2 + 1) * 3) -&gt; 9 ["4", "13", "5", "/", "+"] -&gt; (4 + (13 / 5)) -&gt; 6 思路:每次遇到符号,出栈两个元素进行运算,并将...

    leetcode第一题

    这是evaluate_reverse_polish_notation,这是evaluate_reverse_polish_notat

    javalruleetcode-leetcode:这是Java写的leetcode

    [Java](./src/Evaluate Reverse Polish Notation/Solution.java) 2013/11/27 中等的 [Java](./src/直线上的最大点数/Solution.java) 2013/11/22 难的 [Java](./src/排序列表/Solution.java) 2013/11/16 中等的 [Java...

    LeetCode判断字符串是否循环-leetcode:用lin编码

    Notation(150) Vector 向量 vector 是一种对象实体, 能够容纳许多其他类型相同的元素, 因此又被称为容器。 与string相同, vector 同属于STL(Standard Template Library, 标准模板库)中的一种自定义的数据类型, ...

    cpp-算法精粹

    Evaluate Reverse Polish Notation Implement Stack using Queues 队列 Implement Queue using Stacks 二叉树 二叉树的遍历 Binary Tree Preorder Traversal Binary Tree Inorder Traversal Binary Tree Postorder ...

Global site tag (gtag.js) - Google Analytics