很经典的stack应用,为了省事儿,不再自己写stack了。
Leetcode #224 Basic Calculator
Implement a basic calculator to evaluate a simple expression string.
The expression string may contain open (
and closing parentheses )
, the plus +
or minus sign -
, non-negative integers and empty spaces .
You may assume that the given expression is always valid.
Some examples:
"1 + 1" = 2 " 2-1 + 2 " = 3 "(1+(4+5+2)-3)+(6+8)" = 23
Note: Do not use the eval
built-in library function.
class Solution {
public:
int calculate(string s){
s += ')';
stack<int> nums;
stack<char> opts;
int index = 0;
opts.push('(');
//nums.push(0); //对于空字符串的处理
while (index < s.size()) {
char c = s[index++];
if (isSpace(c)) continue;
else if (isOpt(c)) {
char pre_c = opts.top();
while (priority(pre_c, c) == 1){
int second = nums.top(); nums.pop();
int first = nums.top(); nums.pop();
opts.pop();
switch(pre_c){
case '+' :
nums.push(first + second); break;
case '-':
nums.push(first - second); break;
}
pre_c = opts.top();
}
if (priority(pre_c, c) == 0) opts.pop();
else opts.push(c);
}
else{
int n = ctoi(c);
while(isNum(s[index]))
n = n * 10 + ctoi(s[index++]);
nums.push(n);
}
}
return nums.top();
}
private:
bool isSpace(char c){
return c == ' ';
}
bool isOpt(char c){
return c == '+' || c == '-' || c == '(' || c == ')';
}
bool isNum(char c){
return c >= '0' && c <= '9';
}
int ctoi(char c){
return c - '0' + 0;
}
int priority(char c1, char c2){
if (c1 == '(')
if(c2 == ')') return 0;
else return -1;
if(c2 == '(') return -1;
return 1;
}
};
Leetcode227 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.
class Solution {
public:
int calculate(string s){
s += '#';
stack<int> nums;
stack<char> opts;
int index = 0;
opts.push('#');
// nums.push(0); //对于空字符串的处理
while (index < s.size()) {
char c = s[index++];
if (isSpace(c)) continue;
else if (isOpt(c)) {
char pre_c = opts.top();
while (priority(pre_c, c)){
int second = nums.top(); nums.pop();
int first = nums.top(); nums.pop();
opts.pop();
switch(pre_c){
case '*' :
nums.push(first * second); break;
case '/' :
nums.push(first / second); break;
case '+' :
nums.push(first + second); break;
case '-':
nums.push(first - second); break;
}
pre_c = opts.top();
}
opts.push(c);
}
else{
int n = ctoi(c);
while(isNum(s[index]))
n = n * 10 + ctoi(s[index++]);
nums.push(n);
}
}
return nums.top();
}
private:
bool isSpace(char c){
return c == ' ';
}
bool isOpt(char c){
return c == '+' || c == '-' || c == '*' || c == '/' || c== '#';
}
bool isNum(char c){
return c >= '0' && c <= '9';
}
int ctoi(char c){
return c - '0' + 0;
}
bool priority(char c1, char c2){
if (c1 == '#') return false;
if (c2 == '#') return true;
if (c1 == '*' || c1 == '/') return true;
if (c1 == '+' || c1 == '-')
if (c2 == '*' || c2 =='/') return false;
else return true;
}
};