码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode150

时间:2018-10-21 21:52:07      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:return   str   bsp   nbsp   []   tac   code   convert   public   

public class Solution
    {
        public int EvalRPN(string[] tokens)
        {
            Stack<int> ST_NUM = new Stack<int>();
            foreach (var to in tokens)
            {
                if (to == "+" || to == "-" || to == "*" || to == "/")
                {
                    var num1 = ST_NUM.Pop();
                    var num2 = ST_NUM.Pop();
                    if (to == "+")
                    {
                        var num = num2 + num1;
                        ST_NUM.Push(num);
                    }
                    else if (to == "-")
                    {
                        var num = num2 - num1;
                        ST_NUM.Push(num);
                    }
                    else if (to == "*")
                    {
                        var num = num2 * num1;
                        ST_NUM.Push(num);
                    }
                    else if (to == "/")
                    {
                        var num = num2 / num1;
                        ST_NUM.Push(num);
                    }
                }
                else
                {
                    var num = Convert.ToInt32(to);
                    ST_NUM.Push(num);
                }
            }
            var result = ST_NUM.Pop();
            return result;
        }
    }

 

leetcode150

标签:return   str   bsp   nbsp   []   tac   code   convert   public   

原文地址:https://www.cnblogs.com/asenyang/p/9826672.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!