LeetCode-227.Basic Calculator II

来源:互联网 发布:淘宝羊皮女背包 编辑:程序博客网 时间:2024/05/23 17:01

https://leetcode.com/problems/basic-calculator-ii/

Implement a basic calculator to evaluate a simple expression string.

The expression string contains only non-negative integers, +-*/ operators and empty spaces . The integer division should truncate toward zero.

You may assume that the given expression is always valid.

Some examples:

"3+2*2" = 7" 3/2 " = 1" 3+5 / 2 " = 5

Note: Do not use the eval built-in library function.

比较笨的办法:

public int Calculate(string s)     {        Stack<char> op = new Stack<char>();        Stack<int> num = new Stack<int>();        for (int i = 0; i < s.Length; i++)        {            if (s[i] == ' ')                continue;            if (s[i] >= '0' && s[i] <= '9')            {                int tmp = s[i] - '0';                while (i < s.Length - 1 && char.IsDigit(s[i + 1]))                    tmp = tmp * 10 + (s[++i] - '0');                if (op.Count > 0 && (op.Peek() == '*' || op.Peek() == '/'))                {                    if (op.Pop() == '*')                        num.Push(num.Pop() * tmp);                    else                        num.Push(num.Pop() / tmp);                }                else                    num.Push(tmp);            }            else                op.Push(s[i]);        }        Stack<char> op2 = new Stack<char>();        Stack<int> num2 = new Stack<int>();        while (op.Count>0)            op2.Push(op.Pop());        while (num.Count > 0)            num2.Push(num.Pop());        int res = num2.Pop();        while (op2.Count>0)        {            if (op2.Pop() == '+')                res += num2.Pop();            else                res -= num2.Pop();        }        return res;    }

改进 (参考https://leetcode.com/discuss/41902/share-my-java-solution)

public int Calculate(string s)     {        Stack<int> stack = new Stack<int>();        char op = '+';        int num=0;        for (int i = 0; i < s.Length; i++)        {             if(char.IsDigit(s[i]))                num = num * 10 + (s[i] - '0');            if (!char.IsDigit(s[i]) && s[i] != ' ' || i ==s.Length-1)            {                if (op == '+')                    stack.Push(num);                else if (op == '-')                    stack.Push(-num);                else if (op == '*')                    stack.Push(stack.Pop() * num);                else                    stack.Push(stack.Pop() / num);                op = s[i];                num = 0;            }        }        num = 0;        while (stack.Count>0)            num += stack.Pop();        return num;    }

优化 ,不适用栈(参考https://leetcode.com/discuss/42903/java-straight-forward-iteration-solution-with-comments-stack)

public int Calculate(string s)     {        char op = '+';        int curNum=0,preNum=0,res=0;        for (int i = 0; i < s.Length; i++)        {            if(char.IsDigit(s[i]))                curNum = curNum * 10 + (s[i] - '0');            if (!char.IsDigit(s[i]) && s[i] != ' ' || i ==s.Length-1)            {                if (op == '+')                {                    res += preNum;                    preNum = curNum;                }                else if (op == '-')                {                    res += preNum;                    preNum = -curNum;                }                else if (op == '*')                    preNum *= curNum;                else                    preNum /= curNum;                op = s[i];                curNum = 0;            }        }        return res + preNum;    }


0 0
原创粉丝点击