2022-11-20 Daily Challenge
Today I have done leetcode's November LeetCoding Challenge with cpp
.
November LeetCoding Challenge 20
Description
Basic Calculator
Given a string s
representing a valid expression, implement a basic calculator to evaluate it, and return the result of the evaluation.
Note: You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as eval()
.
Example 1:
Input: s = "1 + 1" Output: 2
Example 2:
Input: s = " 2-1 + 2 " Output: 3
Example 3:
Input: s = "(1+(4+5+2)-3)+(6+8)" Output: 23
Constraints:
1 <= s.length <= 3 * 105
s
consists of digits,'+'
,'-'
,'('
,')'
, and' '
.s
represents a valid expression.'+'
is not used as a unary operation (i.e.,"+1"
and"+(2 + 3)"
is invalid).'-'
could be used as a unary operation (i.e.,"-1"
and"-(2 + 3)"
is valid).- There will be no two consecutive operators in the input.
- Every number and running calculation will fit in a signed 32-bit integer.
Solution
auto speedup = [](){
cin.tie(nullptr);
cout.tie(nullptr);
ios::sync_with_stdio(false);
return 0;
}();
class Solution {
bool shouldCompute(char current, char last) {
if(current == '(') return false;
if((current == '+' || current == '-') && last != '(') return true;
if(last == '*' || last == '/') return true;
return false;
}
void compute(stack<int> &n, stack<char> &op) {
char o = op.top();
op.pop();
int op2 = n.top();
n.pop();
if(o == 'u') {
n.push(-op2);
return;
}
int op1 = n.top();
n.pop();
// cout << op1 << ' '<< o << " " << op2 << endl;
switch(o){
case '+':
n.push(op1+op2);
break;
case '-':
n.push(op1-op2);
break;
case '*':
n.push(op1*op2);
break;
case '/':
n.push(op1/op2);
break;
}
}
public:
int calculate(string s) {
int curNum = 0;
stack<int> n;
stack<char> op;
bool inNum = false;
bool inCompute = false;
bool negative = false;
// cout << "$$$$$$$$$$$$$$$$$$$$" << endl;
for(auto c : s) {
if(c == ' ') continue;
if(inNum && !isdigit(c)) {
if(negative) curNum = -curNum;
// cout << curNum << endl;
n.push(curNum);
curNum = 0;
inNum = false;
negative = false;
}
if(!inCompute && !inNum && c == '-') {
op.push('u');
} else if(isdigit(c)) {
curNum *= 10;
curNum += c - '0';
inNum = true;
} else if(c == ')') {
while(op.size() && op.top() != '(') {
compute(n, op);
}
op.pop();
} else {
while(op.size() && shouldCompute(c, op.top())) {
compute(n, op);
}
op.push(c);
}
inCompute = c != '(';
}
if(inNum) n.push(curNum);
while(op.size()) {
compute(n, op);
}
return n.top();
}
};
// Accepted
// 44/44 cases passed (26 ms)
// Your runtime beats 43.62 % of cpp submissions
// Your memory usage beats 28.22 % of cpp submissions (9.4 MB)