leetcode 面试题 16.26. 计算器 栈 逆波兰表达式

面试题 16.26. 计算器

给定一个包含正整数、加(+)、减(-)、乘(*)、除(/)的算数表达式(括号除外),计算其结果。

表达式仅包含非负整数,+, - ,*,/ 四种运算符和空格 。 整数除法仅保留整数部分。

示例 1:

输入: “3+2*2”
输出: 7

示例 2:

输入: " 3/2 "
输出: 1

示例 3:

输入: " 3+5 / 2 "
输出: 5

说明:

你可以假设所给定的表达式都是有效的。
请不要使用内置的库函数 eval。

通过次数5,067
提交次数13,296


老经典的栈题逆波兰表达式了,

  1. 处理两便,
  2. 第一便把所有的*/处理掉,每次从栈里拿出两个值相乘/除,再把结果丢回栈里
  3. 第二遍处理+-即可

// #define debug
#ifdef debug
#include <time.h>
#include "win_majiao.h"
#endif

#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>

#define MAXN ((int)1e5+7)
#define ll long long int
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)

using namespace std;
#define VVI vector<vector<int> > G

#define show(x...) \
	do { \
	   cout << "\033[31;1m " << #x << " -> "; \
	   err(x); \
	} while (0)

void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }

namespace FastIO{

	char print_f[105];
	void read() {}
	void print() { putchar('\n'); }

	template <typename T, typename... T2>
	   inline void read(T &x, T2 &... oth) {
		   x = 0;
		   char ch = getchar();
		   ll f = 1;
		   while (!isdigit(ch)) {
			   if (ch == '-') f *= -1; 
			   ch = getchar();
		   }
		   while (isdigit(ch)) {
			   x = x * 10 + ch - 48;
			   ch = getchar();
		   }
		   x *= f;
		   read(oth...);
	   }
	template <typename T, typename... T2>
	   inline void print(T x, T2... oth) {
		   ll p3=-1;
		   if(x<0) putchar('-'), x=-x;
		   do{
				print_f[++p3] = x%10 + 48;
		   } while(x/=10);
		   while(p3>=0) putchar(print_f[p3--]);
		   putchar(' ');
		   print(oth...);
	   }
} // namespace FastIO
using FastIO::print;
using FastIO::read;

#define ENDWORD (ch<'0' || ch>'9')
class Solution {
public:
    int calculate(string s) {
		int ans = 0, x = 0;
		s.push_back('#');
		vector<int> vec;
		vector<char> vec2;

		for(auto ch : s) {
			if(ch == ' ') continue ;
			if(ch>='0' && ch<='9') {
				x = x * 10 + (ch - '0');
			} else {
				vec.push_back(x);
				if(ch != '#') vec2.push_back(ch);
				x = 0;
			}
		}

		vector<int> stk;
		vector<char> stk2;

		stk.push_back(vec[0]);
		for(int i=0; i<vec2.size(); i++) { //第一遍 
			stk.push_back(vec[i+1]);
			if(vec2[i] == '*') { //从栈里取出两个相乘后再丢回栈里
				int fst = stk.back(); stk.pop_back();
				int sec = stk.back(); stk.pop_back();
				stk.push_back(fst*sec);
			} else if(vec2[i] == '/') {
				int sec = stk.back(); stk.pop_back();
				int fst = stk.back(); stk.pop_back();
				stk.push_back(fst/sec);
			} else {
				stk2.push_back(vec2[i]);
			}
		}
		ans = stk[0];
		for(int i=0; i<stk2.size(); i++) { //第二遍从前往后扫描即可
			int p = stk[i+1];
			if(stk2[i] == '-') p = -p;
			ans += p;
		}
		return ans;
    }
};

#ifdef debug
signed main() {

	Solution s;
	string str = " 1-1+1";
	cout << s.calculate(str) << endl;



   return 0;
}
#endif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值