title Source:https://leetcode.com/problems/basic-calculator/
Test Instructions Description:
Given a valid string with only a minus, parenthesis, space, and non-negative number, the value is calculated without calling the library function eval
Solution Ideas:
Since this topic only has a minus sign, there is no problem with operator precedence, just a little attention to parentheses.
Therefore, you can use two stack ops,numbers to store the unhandled operators and the unhandled numbers separately, and to scan the strings one at a time. For any character scanned to be s[i], do the following:
If S[i] is a space, the scan continues.
If S[i] is the left parenthesis ' (', then directly into the stack
If S[i] is the right parenthesis ') ', then all operators above the left parenthesis in the stack are processed sequentially from the top, and the left parenthesis are stacked after processing.
If S[i] is a plus ' + ' or minus '-', then determine whether the previous operator exists, and whether it is an opening parenthesis ' (', if present and not, the stack is numbers the top of the stack, and the operator calculates the result to press the result into the stack. Otherwise, the operator is pressed directly into the stack.
If S[i] is a number, take out all the numbers in the current position and calculate the value, press the number into the numbers stack, and move the scan pointer back to the corresponding position.
Finally, after the scan is finished, directly output the elements at the top of the numbers stack.
Algorithm Complexity Analysis:
This algorithm scans a string once, with a time complexity of O (n) and two stacks to store the contents of the string, so the spatial complexity is also O (n), where n is the length of the string.
The following is directly attached to the source code (note: In order to follow-up procedures to determine the simplicity, at the beginning of the function calculate preprocessing, in the operator stack ops into the left parenthesis ' (', and the end of S added a closing parenthesis ') ')
1#include <iostream>2#include <string>3#include <ctype.h>4#include <stack>5 using namespacestd;6 7 classSolution {8 Public:9 Ten intOpintN1,intN2,Charc) One { A intAns =0; - Switch(c) - { the Case '+': -Ans = n1+N2; - Break; - Case '-': +Ans = n1-N2; - Break; + } A returnans; at } - - intCalculatestrings) - { -stack<Char>ops; -stack<int>numbers; inOps.push ('('); -S.push_back (')'); to for(inti =0; I < s.length (); i + +) + { - Switch(S[i]) the { * Case ' ':Continue; Break; $ Case '(': Ops.push (S[i]); Break;Panax Notoginseng Case ')': - while(Ops.top ()! ='(') the { + intN1 =numbers.top (); A Numbers.pop (); the intN2 =numbers.top (); + Numbers.pop (); - CharOp_top =ops.top (); $ Ops.pop (); $ Numbers.push (OP (N2, N1, op_top)); - } - Ops.pop (); the Break; - Case '+':Wuyi Case '-': the if(Ops.empty () | | ops.top ()! ='(') - { Wu intN1 =numbers.top (); - Numbers.pop (); About intN2 =numbers.top (); $ Numbers.pop (); - CharOp_top =ops.top (); - Ops.pop (); - Numbers.push (OP (N2, N1, op_top)); A } + Ops.push (S[i]); the Break; - default: $ intnum = s[i]-'0'; the while(IsDigit (s[i+1])) the { thenum = num*Ten+s[++i]-'0'; the } - Numbers.push (num); in Break; the } the } About returnnumbers.top (); the } the }; the + intMain () - { the solution solution;Bayi strings; theCIN >>s; thecout << solution.calculate (s) <<Endl; - - return 0; the}
Problem Solving report: Leetcode Basic Calculator (Simple Calculator)