首页 > 代码库 > [leetcode-150-Evaluate Reverse Polish Notation]

[leetcode-150-Evaluate Reverse Polish Notation]

Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are +, -, *, /. Each operand may be an integer or another expression.
Some examples:
["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6

 1 int evalRPN(vector<string>& tokens)
 2     {//字符串可以直接比较。。不用strcmp 直接tokens[i]=="*"
 3         stack<int> st;
 4         for (int i = 0; i < tokens.size();i++)
 5         {
 6             if (atoi(tokens[i].c_str())!=0)st.push(atoi(tokens[i].c_str()));//返回非0代表为数字 
 7             else if (strcmp(tokens[i].c_str(), "0") == 0)
 8             {
 9                  st.push(atoi(tokens[i].c_str()));                
10             }
11             else
12             {                
13                 int b = st.top();
14                 st.pop();
15                 int a = st.top();
16                 st.pop();
17                 if (strcmp(tokens[i].c_str(), "+")==0)    st.push(a + b);                
18                 else if (strcmp(tokens[i].c_str(), "-") == 0)st.push(a - b);
19                 else if (strcmp(tokens[i].c_str(), "*") == 0)st.push(a * b);
20                 else if (tokens[i] == "/")st.push(a / b);
21                 else
22                 {
23                     //说明有别的字符 错误                    
24                 }
25             }
26         }
27         return st.top();
28     }

 

再借鉴一个别人遍历vector的方法:用迭代器访问。

 1  int evalRPN(vector<string> &tokens) {
 2         stack<int> st;
 3         int s1,s2;
 4         s1=s2=0;
 5         int res=0;
 6         for(vector<string>::iterator iter=tokens.begin();iter!=tokens.end();iter++)
 7         {
 8                 if (*iter == "+")
 9                 {
10                     s1=st.top();
11                     st.pop();
12                     s2=st.top();
13                     st.pop();
14                    res=s1+s2;
15                    st.push(res);
16                 }
17                    
18                 else if (*iter == "-")
19                 {
20                     s1=st.top();
21                     st.pop();
22                     s2=st.top();
23                     st.pop();
24                    res=s2-s1;
25                    st.push(res);
26                 }
27                 else if (*iter == "*")
28                 {
29                     s1=st.top();
30                     st.pop();
31                     s2=st.top();
32                     st.pop();
33                    res=s1*s2;
34                    st.push(res);
35                 }
36                 else if (*iter== "/")
37                 {
38                     s1=st.top();
39                     st.pop();
40                     s2=st.top();
41                     st.pop();
42                     res=s2/s1;
43                     st.push(res);
44                 }
45                 else 
46                 {
47                     st.push(atoi((*iter).c_str()));
48                 }
49             }
50             return st.top(); 
51             
53         }

 

[leetcode-150-Evaluate Reverse Polish Notation]