[Review] 150. Inverse Polish expression evaluation-according to the inverse Polish notation, evaluate the value of the expression.

Topic: 150. Inverse Polish expression evaluation

According to the reverse Polish notation, find the value of the expression.

Valid operators include +, -, *, /. Each operand can be an integer or another inverse Polish expression.
Insert picture description here

answer:

This question examines the suffix expressions in the knowledge content of stacks and queues . The
core idea of ​​this question: When encountering a number, it will be pushed into the stack, and when encountering an operator, the top two numbers of the stack will be taken out for operation (pay attention to the order when looking at the code), and Push the result onto the stack.
Advantages of inverse Polish expressions:
1. No ambiguity after removing the parentheses
2. Suitable for stack operations

// C++代码
class Solution
{
    
    
public:
    int evalRPN(vector<string>& tokens)
    {
    
    
        stack<int> st;
        for (int i = 0; i < tokens.size(); i++) 
        {
    
    
            if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/") 
            {
    
    
                int num1 = st.top();
                st.pop();
                int num2 = st.top();
                st.pop();
                //注意num2在前,num1在后
                if (tokens[i] == "+") st.push(num2 + num1);
                if (tokens[i] == "-") st.push(num2 - num1);
                if (tokens[i] == "*") st.push(num2 * num1);
                if (tokens[i] == "/") st.push(num2 / num1);
            } 
            else 
            {
    
    
                st.push(stoi(tokens[i]));
            }
        }
        int result = st.top();
        st.pop(); // 把栈里最后一个元素弹出(其实不弹出也没事)
        return result;
    }
};
// C语言代码

// 支持动态增长的栈
typedef int STDataType;
typedef struct Stack
{
    
    
	STDataType* _a;
	int _top;		// 栈顶
	int _capacity;  // 容量 
}Stack;

// 初始化栈 
void StackInit(Stack* ps)
{
    
    
	if (ps == NULL)
		return;
	ps->_top = 0;
	ps->_capacity = 0;
	ps->_a = NULL;
}

// 栈容量检测
void StackCheck(Stack* ps)
{
    
    
	if (ps->_top == ps->_capacity)
	{
    
    
		int newcapacity = (ps->_capacity == 0 ? 1 : 2 * ps->_capacity);
		ps->_a = (STDataType*)realloc(ps->_a, sizeof(STDataType) * newcapacity);
		ps->_capacity = newcapacity;
	}
}

// 入栈 
void StackPush(Stack* ps, STDataType data)
{
    
    
	StackCheck(ps);
	ps->_a[ps->_top++] = data;
}

// 出栈 
void StackPop(Stack* ps)
{
    
    
	if (ps == NULL || ps->_top == 0)
		return;
	ps->_top--;
}

// 获取栈顶元素 
STDataType StackTop(Stack* ps)
{
    
    
	if (ps->_top == 0)
		return (STDataType)0;
	return ps->_a[ps->_top - 1];
}

// 获取栈中有效元素个数 
int StackSize(Stack* ps)
{
    
    
	return ps->_top;
}

// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
int StackEmpty(Stack* ps)
{
    
    
	return ps->_top == 0;
}

// 销毁栈 
void StackDestroy(Stack* ps)
{
    
    
	if (ps == NULL)
		return;
	if (ps->_a)
	{
    
    
		free(ps->_a);
		ps->_a = NULL;
		ps->_capacity = 0;
		ps->_top = 0;
	}
}

// 逆波兰表达式求值
int evalRPN(char** tokens, int tokensSize)
{
    
    
	// 注意:栈内的元素是int类型
	Stack ps;
    StackInit(&ps);
	int i = 0;
	for (i = 0; i < tokensSize; i++)
	{
    
    
		if ( !strcmp(tokens[i], "+") || !strcmp(tokens[i], "-") ||
			 !strcmp(tokens[i], "*") || !strcmp(tokens[i], "/") )
		{
    
    
			int num1 = StackTop(&ps);
			StackPop(&ps);
			int num2 = StackTop(&ps);
			StackPop(&ps);
			// 注意num2在前,num1在后
			if (!strcmp(tokens[i], "+"))
				StackPush(&ps, num2 + num1);
			if (!strcmp(tokens[i], "-"))
				StackPush(&ps, num2 - num1);
			if (!strcmp(tokens[i], "*"))
				StackPush(&ps, num2 * num1);
			if (!strcmp(tokens[i], "/"))
                StackPush(&ps, num2 / num1);	    
		}
		else
			// atoi():把字符串转换成整型数的一个函数
			StackPush(&ps, atoi(tokens[i]));
	}
	// 栈顶元素即为结果
	int result = StackTop(&ps);
	// 也可以不弹出结果
	StackPop(&ps);
	// 销毁栈
    StackDestroy(&ps);
    // 返回最终结果
	return result;
}

Guess you like

Origin blog.csdn.net/m0_46613023/article/details/114001704