id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.l...
3
{ "code": "class Solution {\npublic:\n int maximalSquare(vector<vector<char>>& m) {\n vector<vector<int>> dp(m.size()+1, vector<int>(m[0].size()));\n\t\n\t // preprocess from left side to to signify i,j showing consecutive 1s from horizontal line starting from i,j\n\t for(int i=0;i<m.size();i+...
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.l...
3
{ "code": "class Solution {\npublic:\n int maximalSquare(vector<vector<char>>& matrix) {\n struct Data\n {\n int numberOfConsecutiveSquaresLeft = 0;\n int numberOfConsecutiveSquaresUp = 0;\n int sizeOfSquare = 0;\n };\n\n auto data = vector<vector<Data>>...
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.l...
3
{ "code": "class Solution {\npublic:\n int maximalSquare(vector<vector<char>>& matrix) {\n int m = matrix.size();\n int n = matrix[0].size();\n vector<int> h(n + 2);\n int ans = 0;\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n if (mat...
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.l...
3
{ "code": "class Solution {\npublic:\n int solve(vector<vector<char>>& matrix, int i, int j){\n int n = matrix.size();\n // base case 20 oct 2023\n if( i >= n || j >= matrix[0].size()){\n return 0;\n }\n\n // initialise the pointer right , diagonal , down\n int right = solve( matrix, i , ...
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.l...
3
{ "code": "class Solution {\npublic:\n using p=pair<int, int>;\n int maximalSquare(vector<vector<char>>& matrix) {\n int r=matrix.size();\n int c=matrix[0].size();\n vector<vector<int>> vec(r, vector<int>(c));\n for(auto y=0; y<r; y++)\n {\n for(auto x=0; x<c;x++)\n...
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.l...
3
{ "code": "class Solution {\npublic:\n using p=pair<int, int>;\n int maximalSquare(vector<vector<char>>& matrix) {\n int r=matrix.size();\n int c=matrix[0].size();\n vector<vector<int>> vec(r, vector<int>(c));\n for(auto y=0; y<r; y++)\n {\n for(auto x=0; x<c;x++)\n...
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.l...
3
{ "code": "class Solution {\npublic:\n int largestSqInHist(vector<int>& height) {\n int area = 0;\n int n = height.size();\n stack<int> st;\n int width = 0;\n int mini = INT_MAX;\n int maxsq = 0;\n\n for(int i = 0; i <= n; i++) {\n while(!st.empty() && (i...
223
<p>Given the coordinates of two <strong>rectilinear</strong> rectangles in a 2D plane, return <em>the total area covered by the two rectangles</em>.</p> <p>The first rectangle is defined by its <strong>bottom-left</strong> corner <code>(ax1, ay1)</code> and its <strong>top-right</strong> corner <code>(ax2, ay2)</code>...
0
{ "code": "class Solution {\npublic:\n int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {\n int ans=0;\n\n ans+=(ay2-ay1)*(ax2-ax1)+(by2-by1)*(bx2-bx1);\n\n int len=min(ax2,bx2)-max(ax1,bx1);\n int wid=min(ay2,by2)-max(ay1,by1);\n if(len<0 |...
223
<p>Given the coordinates of two <strong>rectilinear</strong> rectangles in a 2D plane, return <em>the total area covered by the two rectangles</em>.</p> <p>The first rectangle is defined by its <strong>bottom-left</strong> corner <code>(ax1, ay1)</code> and its <strong>top-right</strong> corner <code>(ax2, ay2)</code>...
0
{ "code": "class Solution {\npublic:\n int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {\n int x = max(0,min(ax2,bx2)-max(ax1,bx1));\n int y = max(0,min(ay2,by2)-max(ay1,by1));\n return (ax2-ax1)*(ay2-ay1) + (bx2-bx1)*(by2-by1) - x*y;\n }\n};", "me...
223
<p>Given the coordinates of two <strong>rectilinear</strong> rectangles in a 2D plane, return <em>the total area covered by the two rectangles</em>.</p> <p>The first rectangle is defined by its <strong>bottom-left</strong> corner <code>(ax1, ay1)</code> and its <strong>top-right</strong> corner <code>(ax2, ay2)</code>...
0
{ "code": "class Solution {\npublic:\n int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {\n int area1 = (ax2 - ax1) * (ay2 - ay1);\n int area2 = (bx2 - bx1) * (by2 - by1);\n int overlapWidth = min(ax2, bx2) - max(ax1, bx1);\n int overlapHeight = mi...
223
<p>Given the coordinates of two <strong>rectilinear</strong> rectangles in a 2D plane, return <em>the total area covered by the two rectangles</em>.</p> <p>The first rectangle is defined by its <strong>bottom-left</strong> corner <code>(ax1, ay1)</code> and its <strong>top-right</strong> corner <code>(ax2, ay2)</code>...
0
{ "code": "class Solution {\npublic:\n int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {\n int area1 = (ax2 - ax1) * (ay2 - ay1);\n int area2 = (bx2 - bx1) * (by2 - by1);\n int overlappedArea = max(0, min(ax2, bx2) - max(ax1, bx1)) * max(0, min(ay2, by2...
223
<p>Given the coordinates of two <strong>rectilinear</strong> rectangles in a 2D plane, return <em>the total area covered by the two rectangles</em>.</p> <p>The first rectangle is defined by its <strong>bottom-left</strong> corner <code>(ax1, ay1)</code> and its <strong>top-right</strong> corner <code>(ax2, ay2)</code>...
0
{ "code": "class Solution {\npublic:\n int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {\n\n int l=-max(ax1,bx1)+min(ax2,bx2);\n int b=-max(ay1,by1)+min(ay2,by2);\n int overlap=0;\n if(l>=0 && b>=0)\n overlap=l*b;\n int total=abs(ax2...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
0
{ "code": "\nclass Solution {\npublic:\n int calculate(const std::string& s) {\n idx = 0;\n return calc(s);\n }\n\nprivate:\n int idx; // Index for parsing the string\n\n int calc(const std::string& s) {\n int num = 0, res = 0;\n int sign = 1; // 1 for positive, -1 for negati...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
0
{ "code": "class Solution {\npublic:\n int calculate(string &s) {\n int ans = 0, sign = 1, number = 0; // `number` stores the current number\n stack<int> stk;\n stk.push(sign);\n for (char c : s) {\n if (isdigit(c)) number = number * 10 + (c - '0');\n else if (c ==...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
2
{ "code": "class Solution {\npublic:\n int calculate(string s) {\n stack<pair<int, int>> valueStore;\n\n int result = 0;\n int sign = 1;\n\n for (int i = 0; i < s.length(); i++) {\n if (isdigit(s[i])) {\n int num = s[i] - '0';\n while (i < s.leng...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
2
{ "code": "class Solution {\npublic:\n int calculate(string s) {\n stack<pair<int,int>> st; // pair(prev_calc_value , sign before next bracket () )\n \n long long int sum = 0;\n int sign = +1;\n \n for(int i = 0 ; i < s.size() ; ++i)\n {\n char ch = s[i];\n ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
2
{ "code": "class Calc {\npublic:\n Calc(string& source) {\n s = source;\n s.erase(std::remove(s.begin(), s.end(), ' '), s.end());\n }\n\n int eval() {\n return eval_expr();\n }\n\nprivate:\n string s;\n size_t i{ 0 };\n\n int eval_expr() {\n int sum{ 0 };\n whil...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
2
{ "code": "class Solution {\npublic:\n int calculate(string s) {\n vector<pair<bool, pair<int, char>>> store;\n string curr_num_str = \"\";\n bool num_valid = 0;\n int curr_num = 0;\n for (char c : s) {\n if (isdigit(c)) {\n curr_num_str += c;\n ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int calculate(string s) {\n vector<char> pos;\n vector<int> eval;\n int i = 0;\n while (i < s.length()){\n // cout << \"hi0 \" << s[i] << endl;\n if (s[i] == '(') {\n pos.push_back('(');\n i++;\n ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int calculate(string s) {\n stack<int> values;\n stack<char> ops;\n\n int current_value = 0;\n bool negate = false;\n bool last_token_was_op = true;\n for (int i = 0; i < s.length(); i++) {\n if (s[i] == ' ') continue;\n\n ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int solve(string &s,int l, int r){\n int res = 0;\n for(int i=l;i<=r;i++){\n if((s[i]=='-'||s[i]=='+')){\n int m = (s[i]=='-')? -1 : 1; i++;\n while(i<=r&&s[i]==' ') i++;\n if(s[i]=='('){\n ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int solve(string &s,int l, int r){\n int res = 0;\n for(int i=l;i<=r;i++){\n if((s[i]=='-'||s[i]=='+')){\n int m = (s[i]=='-')? -1 : 1; i++;\n if(s[i]=='('){\n int j = i+1; int ct=1;\n ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int opposign(int num){\n return -num;\n }\n string calculated(string &s){\n int sign=1,i=1;\n if(s[0]!='-'){\n sign=0,i=0;\n }\n int ans=0;\n while(i<s.size() and s[i]>='0' and s[i]<='9'){\n ans=ans*10+(s[...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int precedence(char ch) {\n if(ch == '+' || ch == '-') {\n return 1;\n }\n if(ch == 'X') {\n return 2;\n }\n return 0; // 0 for (\n }\n\n bool isHigherPrecedence(char curr, char prev) {\n if(curr == '+' || ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "//stack for char\nclass Solution {\npublic:\n int calculate(string s) {\n int n = 0;\n int operand = 0;\n stack<variant<int, char>> st;\n\n for(int i = s.size()-1; i >= 0; i--){\n char it = s[i];\n\n\n if(isdigit(it)){\n operand += (pow(10...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "#pragma GCC optimize (\"Ofast\")\n#pragma GCC optimize (\"O3\", \"unroll-loops\", \"-ffloat-store\")\n#pragma GCC target(\"avx,mmx,sse2,sse3,sse4\")\n\n// \"_\": A lambda function to enable faster I/O operations\nauto _=[]()noexcept{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);return 0;}();\n\n#include <...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int calculate(string s) {\n vector<pair<int,int>> ts;\n for(int i = 0;i < s.size();i++){\n if(s[i] == ' ')continue;\n else if(s[i] == '-' && (i == 0 || ts.back().second == '(' )){\n ts.push_back({0,0});\n ts.pu...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int calculate(string s) {\n vector<pair<int,int>> ts;\n for(int i = 0;i < s.size();i++){\n if(s[i] == ' ')continue;\n else if(s[i] == '-' && (i == 0 || ts.back().second == '(' )){\n ts.push_back({0,0});\n ts.pu...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "#include <iostream>\n#include <stack>\n#include <string>\n#include <vector>\nusing namespace std;\nclass Solution {\npublic:\n vector<string> createTokens(string s) {\n vector<string> tokens;\n int start = 0, end = 0;\n if (s[0] != '+' && s[0] != '-') {\n tokens.push_back...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int calculate(string s) {\n stack<string> st;\n int n = s.size();\n for(int i = 0; i < n; i++) {\n if(s[i] == ' ') continue;\n else if(s[i] == '(') st.push(\"(\");\n else if(s[i] == '+') st.push(\"+\");\n else i...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int calculate(string s) {\n stack<string>st;\n int n=s.length();\n cout<<n<<endl;\n for(int i=0;i<n;i++){\n if(s[i]==' ') continue;\n else if(s[i]=='('){\n st.push(\"(\");\n }\n else if(s[i...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n bool isOperator(char c) {\n return c == '*' || c == '+' || c == '-' || c == '/';\n }\n\n static int eval12(int a, int b, string i) {\n if (i == \"+\")\n return a + b;\n if (i == \"*\")\n return a * b;\n if (i == \"-\")\n...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n // Function to evaluate a primary factor (number or expression in\n // parentheses)\n double evalFactor(std::stringstream& ss) {\n ss >> std::ws; // Skip any whitespace\n char c = ss.peek();\n\n // Handle unary plus and minus\n if (c == '+' |...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int calculate(string s) {\n stack<string> memo;\n\n for (int i = 0; i < s.size(); ++i) {\n char currChar = s[i];\n if (currChar == ')'){\n cleanStack(memo);\n }\n else{\n if (currChar == '...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int calculate(string s) {\n string str=\"\";\n for(int i=0;i<s.size();i++){\n if(s[i]!=' ')\n str.push_back(s[i]);\n }\n s=str;\n stack<string> st;\n s=\"(\"+s+\")\";\n string t=\"\";\n for(int i=0;...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n\n enum class CalcType\n {\n Number,\n Plus,\n Minus,\n OpenBracket,\n CloseBracket\n };\n\n struct CalcOp\n {\n CalcType type;\n int value;\n };\n\n vector<CalcOp> opList;\n\n void printOps()\n {\n ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n \n void solve(stack<string>&st){\n string a =\"\";\n while(!st.empty() && st.top()[0] != '('){\n a += st.top();\n st.pop();\n }\n\n if(!st.empty()){\n st.pop();\n }\n reverse(a.begin(),a.end());\n ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n \n void solve(stack<string>&st){\n string a =\"\";\n while(!st.empty() && st.top()[0] != '('){\n a += st.top();\n st.pop();\n }\n\n if(!st.empty()){\n st.pop();\n }\n reverse(a.begin(),a.end());\n ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int f(string s){\n // cout<<s<<\" \";\n string res=\"\";\n int n=s.size();\n int ans=0,j=0;\n while(j<n){\n \n string res=\"\";\n if(j<=n-2){\n if(isdigit(s[j])==false && isdigit(s[j+1])==false...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "#include <iostream>\n#include <string>\n#include <stack>\nusing namespace std;\nclass Solution {\npublic:\n int calculate(string s) {\n // All The Calculations are Pushed And Done On A Stack\n stack<string> stack;\n bool preparing_for_calculation = false;\n bool is_operator_r...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "#include <iostream>\n#include <string>\n#include <stack>\nusing namespace std;\nclass Solution {\npublic:\n int calculate(string s) {\n // All The Calculations are Pushed And Done On A Stack\n stack<string> stack;\n bool preparing_for_calculation = false;\n bool is_operator_r...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "#include <iostream>\n#include <string>\n#include <stack>\nusing namespace std;\nclass Solution {\npublic:\n int calculate(string s) {\n // All The Calculations are Pushed And Done On A Stack\n stack<string> stack;\n bool preparing_for_calculation = false;\n bool is_operator_r...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int calculate(string s) {\n int n = s.size();\n\n stack<string> st;\n string tmpNum = \"\";\n int cnt = 0;\n int tmpCnt = 0;\n int tmpA = 0;\n int tmpB = 0;\n bool isNeg = false;\n bool isBasket = false;\n ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int calculate(string s) {\n int n = s.size();\n\n stack<string> st;\n string tmpNum = \"\";\n int cnt = 0;\n int tmpCnt = 0;\n int tmpA = 0;\n int tmpB = 0;\n bool isNeg = false;\n bool isBasket = false;\n ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\n bool isdigit(char ch) {\n if(ch >= '0' && ch <= '9')\n return true;\n return false;\n }\n\n string convertCharToString(char ch) {\n string dummy = \"\";\n dummy = dummy + ch;\n return dummy;\n }\n\npublic:\n int calculate(strin...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\n int F(vector<string>& tokens, int& i, int multiplier, int& result) {\n if (i >= tokens.size())\n return 0;\n\n int sign = 1;\n\n for (; i < tokens.size(); i++) {\n string token = tokens[i];\n if (isdigit(token[0])) {\n ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int calculate(string s) {\n vector<char> oper_stack;\n vector<string> num_stack;\n vector<string> rpn;\n\n map<char, int> prec = {{'N', 4}, {'+', 2}, {'-', 2}, {'*', 3}, {'/', 3}};\n string stoi_tmp;\n bool unary_flag = true;\n\n ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int calculate(string s) {\n stack<char> st;\n vector<string> rpn;\n char ch;\n char prev = '(';\n\n for (int i = 0; i < s.length(); i++) {\n ch = s[i];\n if (ch == ' ') {\n continue;\n }\n\n ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int calculate(string s) {\n stack<char> st;\n vector<string> rpn;\n string str = \"\";\n char ch;\n bool hasOp = false;\n\n for (int i = 0; i < s.length(); i++) {\n if (s[i] != ' ') {\n str += s[i];\n ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n bool isOperator(string& s)\n {\n return (s == \"+\" || s == \"-\" || s == \"*\" || s == \"/\");\n }\n\n int evalRPN(vector<string>& tokens) {\n stack<int> integerStack{};\n for (auto s : tokens)\n {\n if (!isOperator(s))\n ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "#include <algorithm>\n#include <cctype>\n#include <memory>\n#include <pstl/glue_algorithm_defs.h>\n#include <stack>\n#include <stdexcept>\n#include <string>\n#include <utility>\n\nusing namespace std;\n\nclass Expr {\npublic:\n virtual int eval() const = 0;\n // define descructor since its relied upon by...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int calculate(string s) {\n vector<string>v;\n // if(s[0]=='+'||s[0]=='-')s=\"0\"+s;\n int i=0;\n while(i<s.size()){\n if(s[i]=='('||s[i]==')'||s[i]=='+'||s[i]=='-'){\n string temp;\n temp.push_back(s[i]);\n // c...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "#include <algorithm>\n#include <cassert>\n#include <charconv>\n#include <cstddef>\n#include <iterator>\n#include <optional>\n#include <string>\n#include <string_view>\n#include <vector>\n\nusing namespace std;\nclass Solution {\n public:\n int calculate(string s) {\n auto* const root = buildTree(s);\n ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n std::string evaluateString(string s)\n {\n std::vector<std::string> equation;\n int res = 0;\n int start = 0;\n\n for (int i = start; i < s.size(); ++i)\n {\n if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/')\n ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int calculate(string s) {\n // Convert infix to postfix\n vector<string> postfix;\n stack<string> my_stack;\n string num = \"\";\n bool seen_num = false;\n for (int i = 0; i < s.length(); i++) {\n string c = s.substr(i, 1);...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n\n enum class NextOperation: uint8_t\n {\n ADD,\n SUBSTRACT\n };\n\n int computeCurrentLine(string s)\n {\n// std::cout << \"Processing \" << std::quoted(s) << std::endl;\n if ((s.front() == '(') and (s.back() == ')'))\n s = s...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int calculate(string s) {\n stack<string> st;\n vector<string> ans;\n\n //обратная польская запись в стеке\n s += ' ';\n string strV = \"\";\n char prev = ' ';\n for(char ch: s) {\n if(isdigit(ch)) {\n ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n using Num = int64_t;\n using Nil = std::monostate;\n\n enum class UnOp {\n Negate\n };\n\n enum class BiOp {\n Add,\n Sub\n };\n\n using Ops = std::variant<Nil, UnOp, BiOp>;\n\n struct Expr;\n\n using ExprVariant = std::variant<\n ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\n\nenum TokenType {\n NUMBER,\n ADD, \n SUBTRACT,\n MULTIPLY,\n DIVIDE,\n OPENING_PARENTHESIS,\n CLOSING_PARENTHESIS\n};\n\nstatic unordered_map<char, TokenType> char_to_type_map;\n\nclass Token {\npublic:\n TokenType type;\n string value;\n Token(TokenType ty...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n typedef variant<int, char> token;\n\n enum class op {\n add,\n sub,\n mul,\n div\n };\n\n op char_to_op(char c) {\n switch(c) {\n case '+':\n return op::add;\n case '-':\n return o...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n bool isDigit(char c) {\n return '0' <= c && c <= '9';\n }\n int calculate(string s) {\n s = \"(\" + s + \")\"; // trigger top-level evaluation\n vector<string> tokenize;\n for (int i = 0; i < s.size(); /* manual update */) {\n char...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int calculate(string s) {\n vector<string> v;\n v.push_back(\"(\");\n string t = \"\";\n for(int i = 0; i<s.length(); i++) if(s[i] != ' ') t += s[i];\n s = t;\n for(int i = 0;i < s.length(); i++){\n if(s[i] >= '0' && s[i] <...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int calculate(string s) {\n string str = \"\";\n int n = s.size();\n\n bool isAny = false;\n for(int i = 0; i < n; i++){\n if(s[i] != ' '){\n str += s[i];\n }\n if(s[i] == '+' || s[i] == '-' || s[i] =...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n struct ASTNode {\n ASTNode(char op_, const std::shared_ptr<ASTNode> &arg1_, const std::shared_ptr<ASTNode> &arg2_)\n : op {op_}, value {0}, arg1 {arg1_}, arg2 {arg2_}\n {}\n ASTNode(char op_, const std::shared_ptr<ASTNode> &arg1_)\n ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "struct Operand \n{\n string s; \n bool isDigit = false; \n bool isLeftPar = false;\n bool isRightPar = false;\n bool isMinus = false;\n bool isPlus = false;\n int val = INT_MIN;\n int sign = 1;\n Operand(const string& _s): s(_s)\n {\n if (s.length() >= 2)\n {\n ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "struct Operand \n{\n string s; \n bool isDigit = false; \n bool isLeftPar = false;\n bool isRightPar = false;\n bool isMinus = false;\n bool isPlus = false;\n int val = INT_MIN;\n int sign = 1;\n Operand(const string& _s): s(_s)\n {\n if (s.length() >= 2)\n {\n ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int calculate(string s) {\n vector<string> arr;\n for (int i = 0; i < s.size(); i++) {\n if (s[i] == ' ') {\n continue;\n }\n else if (s[i] == '(' || s[i] == '+' || s[i] == '-') {\n string t = \"\";\...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int calculate(string s) {\n string ss;\n for (char c : s) {\n if (c != ' ') {\n ss += c;\n }\n }\n\n if (ss[0] == '-') {\n ss = '0' + ss;\n }\n\n vector<string> arr;\n for (int i ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\nint make_oper(string oper, int first, int second)\n{\n if (oper == \"+\") return first + second;\n else return first - second;\n}\nint calculate(string s)\n{\n stack<string> calc;\n\n int i = 0, res = 0;\n string d;\n while (i < s.size())\n {\n if (s[i...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\nint make_oper(string oper, int first, int second)\n{\n if (oper == \"+\") return first + second;\n else return first - second;\n}\nint calculate(string s)\n{\n stack<string> calc;\n\n int i = 0, res = 0;\n string d;\n while (i < s.size())\n {\n if (s[i...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int calculate(string s) {\n stack<string> st;\n s += \")\";\n st.push(\"(\");\n for(int i = 0; i<s.size(); i++){\n if(s[i]==' '){continue;}\n else if(s[i] == '('){st.push(\"(\");}\n else if(s[i] == '-'){st.push(\"-\...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n map<string, int> OPS = {\n {\"+\", 1},\n {\"-\", 1},\n {\"UNARY_NEGATE\", 2},\n {\"(\", 3},\n {\")\", 3},\n };\n\n vector<string> getTokens(string& s) {\n int idx = 0;\n vector<string> tokens;\n while (idx < s.size()) {\n while (idx < s.size() && ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int calculate(string s) {\n stack<string> bracket;\n vector<string> given;\n vector<string> reverse;\n string new1=\"\";\n for(int i=0;i<s.size();i++){\n if(s[i]!=' '){\n new1+=s[i];\n }\n }\n ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int calculate(string s) {\n stack<string> bracket;\n vector<string> given;\n vector<string> reverse;\n string new1=\"\";\n for(int i=0;i<s.size();i++){\n if(s[i]!=' '){\n new1+=s[i];\n }\n }\n ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int calculate(string s) {\n stack<string> bracket;\n vector<string> given;\n vector<string> reverse;\n string new1=\"\";\n for(int i=0;i<s.size();i++){\n if(s[i]!=' '){\n new1+=s[i];\n }\n }\n ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n bool expNeg = false;\n int calculate(string s) {\n expNeg = false;\n vector<string> expression = getExpression(s);\n vector<string> postfix = getInfixToPostFixExpression(expression);\n // cout << \"hi\\n\";\n return calculatePostFix(postf...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n long long int calc(vector<string> st){\n \n long long int ans=0;\n bool plusCheck=true;\n\n for(int i=st.size()-1;i>=0;i--){\n string x=st[i];\n if(x==\"+\"){\n plusCheck=true;\n }\n else if(x=...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n long long int calc(vector<string> st){\n for(auto x:st){\n cout<<x<<\" \";\n }\n cout<<endl;\n long long int ans=0;\n bool plusCheck=true;\n\n for(int i=st.size()-1;i>=0;i--){\n string x=st[i];\n if(x...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int helper(vector<string> s) {\n int sum = 0;\n bool add = !(s[0] == \"-\");\n for (auto &c: s) {\n bool flag = (c[0] == '-' && c.size()>1);\n if (c == \" \")\n continue;\n else if (std::all_of(c.begin(), c.end(), ::isdigit) || flag) {...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int process(vector<string> v) {\n int sol = 0;\n int i = 0;\n while (i < v.size()) {\n if (v[i] == \"+\") {\n sol = sol + stoi(v[i + 1]);\n i += 2;\n } else if (v[i] == \"-\") {\n sol = so...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n vector<string> tokenize(string s) {\n std::vector<string> result;\n\n bool is_bin_sub = false;\n \n for (int cursor = 0; cursor < s.length();) {\n\n // Skip preceeding whitespace.\n while (cursor < s.length() && s.at(cursor) =...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "#include <algorithm>\n#include <iostream>\n#include <iterator>\n#include <string>\nclass Solution {\npublic:\n bool isNum(char c) {\n return '0' <= c && c <= '9';\n }\n\n vector<string> createTokens(string s) {\n vector<string> tokens;\n \n int start = 0, end = 0;\n ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n bool isNum(char c) {\n return '0' <= c && c <= '9';\n }\n\n vector<string> createTokens(string s) {\n vector<string> tokens;\n \n int start = 0, end = 0;\n if (s[0] == '-') {\n tokens.push_back(\"0\");\n }\n while ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "#include <algorithm>\n#include <iostream>\n#include <iterator>\n#include <string>\nclass Solution {\npublic:\n bool isNum(char c) {\n return '0' <= c && c <= '9';\n }\n\n vector<string> createTokens(string s) {\n vector<string> tokens;\n \n int start = 0, end = 0;\n ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class myexception : public exception{\n virtual const char* what() const throw(){\n return \"zero divisor\";\n } \n}myex;\nclass Solution {\npublic:\n // int calculate(const string& s) {\n // long res = 0, num = 0, n = s.size();\n // vector<int> sign(2,1);\n // for(i...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class myexception : public exception{\n virtual const char* what() const throw(){\n return \"zero divisor\";\n } \n}myex;\nclass Solution {\npublic:\n // int calculate(const string& s) {\n // long res = 0, num = 0, n = s.size();\n // vector<int> sign(2,1);\n // for(i...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class myexception : public exception{\n virtual const char* what() const throw(){\n return \"zero divisor\";\n } \n}myex;\nclass Solution {\npublic:\n // int calculate(const string& s) {\n // long res = 0, num = 0, n = s.size();\n // vector<int> sign(2,1);\n // for(i...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int calculate(string s) {\n int res = 0; \n int cur = 0; \n bool processing_num = false; \n bool is_neg = false; \n for (int i = 0; i < s.length(); i++) {\n char c = s[i]; \n\n if (c == '(') {\n int start...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int getNum(const string& s, int& i) {\n int num = 0;\n while (i < s.size() && isdigit(s[i])) {\n num = num * 10 + (s[i] - '0');\n i++;\n }\n i--;\n return num;\n }\n\n int calculate(string s) {\n char lastO...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int getNum(const string& s, int& i) {\n int num = 0;\n while (i < s.size() && isdigit(s[i])) {\n num = num * 10 + (s[i] - '0');\n i++;\n }\n i--; \n return num;\n }\n\n int calculate(string s) {\n char last...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\nprivate:\n string removeSpaces(string s){\n string result = \"\";\n for (int i = 0; i < s.length(); i++){\n if (s[i]!= ' ') {\n result+=s[i];\n }\n }\n return result;\n }\n vector<string> tokenize(string s){\n ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int calculate(string s) {\n if (s.size() == 0)\n return 0;\n int tot = 0;\n int op = 0;\n int i = 0;\n while (i < s.size()) {\n int rhs = 0;\n while (s[i] == ' ') {\n i++;\n }\n\n ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int getNum(const string& s, int& i) {\n int num = 0;\n while (i < s.size() && isdigit(s[i])) {\n num = num * 10 + (s[i] - '0');\n i++;\n }\n i--;\n return num;\n }\n\n int calculate(string s) {\n char lastO...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\nint calculate(string s) {\n std::vector<int> bidx;\n std::vector<int> sign = {1, 1};\n int n = s.length();\n int i = 0, res = 0;\n while (i < n) {\n if (s[i] >= '0' && s[i] <= '9') {\n int tmp = 0;\n while (s[i] >= '0' && s[i] <= '9') {...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int getNum(const string& s, int& i) {\n auto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n return 0;\n }();\n int num = 0;\n while (i < s.size() && isdigit(s[i])) {\n ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int calculate(const string& s) {\n int curRes = 0;\n\n int sign = 1;\n\n for (int i = 0; i < s.size(); i++) {\n if (s[i] == '(') {\n int startIndex = i + 1;\n int squareCount = 0;\n while (i < s.size...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int calculate(string s) {\n int sum = 0;\n return resolveExp(s);\n }\n int resolveExp(string s)\n {\n int operand1 = 0;\n int operand2 = 0;\n int pos = 0;\n bool waiting = false;\n int op = 0; // -1 if subtraction, 1 i...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n/*\nexpr := 0 | [1-9][0-9]*\nexpr := (expr)\nexpr := expr + expr\nexpr := expr - expr\n*/\n int calculate(string s) {\n int acc = 0;\n char op = '+';\n\n for(int i = 0; i < s.size(); ++i) {\n if(s[i] == ' ') continue; // whitespace\n ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n int calculate(string s) {\n int res=0;\n int sign = 1;\n for(int i=0; i<s.length(); ++i)\n {\n if(s[i] == ' ') continue;\n if(s[i] == '+' || s[i] == '-')\n {\n sign = s[i] =='+' ? 1:-1;\n ...
224
<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p> <p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <...
3
{ "code": "class Solution {\npublic:\n string solve(vector<string>v){\n stack<string>st;\n for(int i=0;i<v.size();i++){\n if(v[i] == \"-\"){\n int x = 0;\n if(st.empty() == false){\n x = stoi(st.top());\n st.pop();\n }\n int y = stoi(v[i+1]);\n st.push(...