您的位置:首页 > 编程语言 > Lua

FWNX-Evaluate Reverse Polish Notation -c++ VERSION-solution accepted

2013-12-28 04:32 302 查看
class Solution {
public:
    int evalRPN(vector<string> &tokens) {
        
        
        
          int subresult = 0;
        stack<string> stack_op;
        for(int i = 0;i<tokens.size();++i)
        {
            if(tokens[i] != "+" && tokens[i] !="-" && tokens[i]!="*" && tokens[i]!="/")
            {
                stack_op.push(tokens[i]);
            }
            else
            {
                if(stack_op.size()==0)
                {
                    cout << "stack is empty"<<endl;
                    return -1;//-1 means wrong in the statement;
                }
                int right = atoi(stack_op.top().c_str());
                stack_op.pop();
                
                if(stack_op.size()==0)
                {
                    cout << "stack is empty"<<endl;
                    return -1;//-1 means wrong in the statement;
                }
                int left = atoi(stack_op.top().c_str());
                stack_op.pop();
                /*
                switch(tokens[i])
                {
                    case "+" : subresult = right+left;break;
                    case "-" : subresult = left - right;break;
                    case "*" : subresult = right*left;break;
                    case "/" : subresult = left /right;break;
                    default : 
                    {
                        cout << "wrong oprend"<<endl;
                        return -2;
                    }
                }*/
                
                
                if(tokens[i] == "+")
                {
                    subresult = right+left;
                }
                else
                {
                    if(tokens[i] == "-")
                    {
                         subresult = left - right;
                    }
                    else
                    {
                        if(tokens[i] == "*")
                        {
                            subresult = right*left;
                        }
                        else
                        {
                            if(tokens[i] == "/")
                            {
                                subresult = left /right;
                            }
                            else
                            {
                                cout << "wrong oprend"<<endl;
                                return -2;
                            }
                        }
                    }
                }
                
                
                
                char str[25] = {0};
                sprintf(str,"%d",subresult);
                string sub_str_result(str);
                
                stack_op.push(sub_str_result); 
               
                subresult = 0;
            }
        }
        
        int result = atoi(stack_op.top().c_str());
        stack_op.pop();
        return result;
    }
};


http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/

be care about the switch case

1 = it can only be used in the int in the switch area;

2 = it can use if else to change it; it can do everything;

3 = be careful about the stack empty qeustions
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: