zoukankan      html  css  js  c++  java
  • 栈和队列_leetcode150

    class Solution(object):
    def evalRPN(self, tokens):
    """
    :type tokens: List[str]
    :rtype: int
    """

    stack = []
    ops = ["+","-","*","/"]

    for i in tokens:
    if i in ops:
    if i == '+':
    op2 = stack.pop()
    op1 = stack.pop()
    num = int(op1) + int(op2)
    stack.append(num)
    if i == '-':
    op2 = stack.pop()
    op1 = stack.pop()
    num = int(op1) - int(op2)
    stack.append(num)
    if i == '*':
    op2 = stack.pop()
    op1 = stack.pop()
    num = int(op1) * int(op2)
    stack.append(num)
    if i == '/':
    op2 = stack.pop()
    op1 = stack.pop()
    num = int(op1) / int(op2)
    stack.append(num)
    else:
    stack.append(i)

    return stack[0]



    class Solution:
    def evalRPN(self, tokens):
    """
    :type tokens: List[str]
    :rtype: int
    """
    stack = list()
    oper = ['+', '-', '*', '/']
    for char in tokens:
    if char not in oper:
    stack.append(int(char))
    else:
    top1 = stack.pop()
    top2 = stack.pop()
    if char == '+':
    stack.append(top2 + top1)
    elif char == '-':
    stack.append(top2 - top1)
    elif char == '*':
    stack.append(top2 * top1)
    elif char == '/':
    stack.append(int(top2 / top1))
    return stack.pop()



    s = Solution()

    t = ["2", "1", "+", "3", "*"]
    t2 =["10","6","9","3","+","-11","*","/","*","17","+","5","+"]

    print s.evalRPN(t2)
  • 相关阅读:
    谈谈我对雾霾的看法
    2016年书单分享
    我的面试心得:面试官视角
    Cesium原理篇:GroundPrimitive
    Cesium原理篇:Batch
    Peter Hessler和他的中国三部曲(上)
    Cesium原理篇:Material
    全球PM25实时可视化
    Cesium原理篇:Property
    次郎的寿司梦
  • 原文地址:https://www.cnblogs.com/lux-ace/p/10547431.html
Copyright © 2011-2022 走看看