You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
🔥 Basic Calculator 🔥 || 3 Approaches || Simple Fast and Easy || with Explanation
Solution - 1
classSolution {
boolisDigit(String? s) {
if (s ==null) {
returnfalse;
}
returnint.tryParse(s) !=null;
}
intcalculate(String s) {
// all possible case: "+", "-", "(", ")", " ", "1-9"// +: change sign to positive +1// -: change sign to negative -1// (: push current result value and sign onto the stack// ): pop the previous result value and sign off the stack and do the addition// " ": skip// 1-9: read all digits after current digitQueue<int> stack =Queue();
// initialize result to be 0, sign to be 1int res =0, sign =1;
int n = s.length;
// iterate through all characters of the inputfor (int i =0; i < n; i++) {
String curr = s[i];
switch (curr) {
case'+':// make sign become positive to indicate we are adding a value
sign =1;
break;
case'-':// make sign become negative to indicate we are subtracting a value
sign =-1;
break;
case'(':// pushing current result as well as the sign onto the stack
stack.addFirst(res);
stack.addFirst(sign);
// reset result and sign
res =0;
sign =1;
break;
case')':// popping previous result and sign off the stack and do the addition(subtraction)// with the current calculation resultint prevSign = stack.removeFirst();
int prevRes = stack.removeFirst();
res = prevRes + prevSign * res;
break;
case' ':// skip the empty spacesbreak;
default:// in case current char is a digit, read the whole integerint startIdx = i;
while (i < n &&isDigit(s[i])) {
i++;
}
int v =int.parse(s.substring(startIdx, i));
res += sign * v;
i--;
}
}
return res;
}
}
Solution -2
classSolution {
boolisDigit(String? s) {
if (s ==null) {
returnfalse;
}
returnint.tryParse(s) !=null;
}
intcalculate(String s) {
List<int> stack = [];
int result =0;
int number =0;
int sign =1;
for (int i =0; i < s.length; i++) {
String c = s[i];
if (isDigit(c)) {
number =10* number + (c.codeUnitAt(0) -'0'.codeUnitAt(0));
// number completed.// update result
} elseif (c =='+') {
result += sign * number;
number =0;
sign =1;
// number completed.// update result
} elseif (c =='-') {
result += sign * number;
number =0;
sign =-1;
// number completed.// update result
} elseif (c =='(') {
//we push the result first, then sign;
stack.add(result);
stack.add(sign);
//reset the sign and result for the value in the parenthesis
sign =1;
result =0;
// number completed, result updated already before when we encountered +/- before opening bracket
} elseif (c ==')') {
result += sign * number;
number =0;
result *=
stack.removeLast(); //stack.pop() is the sign before the parenthesis
result += stack
.removeLast(); //stack.pop() now is the result calculated before the parenthesis
}
}
if (number !=0) result += sign * number;
return result;
}
}
Solution -3
classSolution {
int pos =0;
boolisNum(String curr) {
int nums = curr.codeUnitAt(0) -'0'.codeUnitAt(0);
return nums >=0&& nums <=9;
}
intsolve(String s) {
int sign =1; // initial take as positiveint nums =0;
int res =0;
while (pos < s.length) {
String curr = s[pos++];
if (curr ==' ') {
continue;
} elseif (isNum(curr)) {
nums = nums *10+ curr.codeUnitAt(0) -'0'.codeUnitAt(0);
} elseif (curr =='(') {
nums =solve(s);
} elseif (curr ==')') {
res += nums * sign;
return res;
} else {
res += sign * nums;
sign = curr =='-'?-1:1;
nums =0;
}
}
int ret = res + (sign * nums);
return ret;
}
intcalculate(String s) {
pos =0;
returnsolve(s);
}
}