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)
  • 相关阅读:
    语言特性-上下文对象
    语言特性-闭包
    语言特性-变量作用域
    语言特性-函数重载与类型检查
    面向对象的JS代码
    单例模式
    wait操作接口
    进程的创建模型
    模拟密码登陆过程
    目录操作的一些函数理解
  • 原文地址:https://www.cnblogs.com/lux-ace/p/10547431.html
Copyright © 2011-2022 走看看