id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
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 <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "1 + 1"
<strong>Output:</strong> 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = " 2-1 + 2 "
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "(1+(4+5+2)-3)+(6+8)"
<strong>Output:</strong> 23
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of digits, <code>'+'</code>, <code>'-'</code>, <code>'('</code>, <code>')'</code>, and <code>' '</code>.</li>
<li><code>s</code> represents a valid expression.</li>
<li><code>'+'</code> is <strong>not</strong> used as a unary operation (i.e., <code>"+1"</code> and <code>"+(2 + 3)"</code> is invalid).</li>
<li><code>'-'</code> could be used as a unary operation (i.e., <code>"-1"</code> and <code>"-(2 + 3)"</code> is valid).</li>
<li>There will be no two consecutive operators in the input.</li>
<li>Every number and running calculation will fit in a signed 32-bit integer.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculate(string s) {\n if (s.size() == 0) {\n return 0;\n }\n vector<int> st;\n char sign = '+';\n long number = 0;\n \n for (int i = 0; i < s.size(); i++) {\n if (isdigit(s[i])) {\n number = number * 10 + (long)(s[i] - '0');\n } else if (s[i] == '(') {\n int braces = 1;\n int j = i + 1;\n while (braces > 0) {\n if (s[j] == '(') {\n braces++;\n } else if (s[j] == ')') {\n braces--;\n }\n j++;\n }\n int length = (j - 1) - i;\n number = calculate(s.substr(i + 1, length));\n i = j - 1;\n }\n \n if (s[i] == '+' || s[i] == '-' || i == s.size() - 1) {\n switch(sign) {\n case '+': {\n st.push_back(number);\n break;\n }\n case '-': {\n st.push_back(-number);\n break;\n }\n }\n sign = s[i];\n number = 0;\n }\n }\n int sum = 0;\n while (!st.empty()) {\n sum += st.back();\n st.pop_back();\n }\n return sum;\n }\n};",
"memory": "78743"
} |
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 <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "1 + 1"
<strong>Output:</strong> 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = " 2-1 + 2 "
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "(1+(4+5+2)-3)+(6+8)"
<strong>Output:</strong> 23
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of digits, <code>'+'</code>, <code>'-'</code>, <code>'('</code>, <code>')'</code>, and <code>' '</code>.</li>
<li><code>s</code> represents a valid expression.</li>
<li><code>'+'</code> is <strong>not</strong> used as a unary operation (i.e., <code>"+1"</code> and <code>"+(2 + 3)"</code> is invalid).</li>
<li><code>'-'</code> could be used as a unary operation (i.e., <code>"-1"</code> and <code>"-(2 + 3)"</code> is valid).</li>
<li>There will be no two consecutive operators in the input.</li>
<li>Every number and running calculation will fit in a signed 32-bit integer.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculate(string s) {\n int n = 0;\n bool plus = true;\n int d = 0;\n for(int i = 0; i < s.size(); i++){\n if(isdigit(s[i])){\n if(plus) (n = n - d + 10*d + (s[i] - '0'));\n else (n = n + d - 10*d - (s[i] - '0'));\n\n d = 10*d + (s[i]-'0');\n } \n else{\n d = 0;\n } \n if(s[i] == ' ') continue;\n if(s[i] == '+') plus = true;\n if(s[i] == '-') plus = false;\n if(s[i] == '('){\n int c = 0;\n int j = i+1;\n while(!(s[j] == ')' && c == 0)){\n if(s[j] == '(') c+=1;\n if(s[j] == ')') c-=1;\n j++;\n }\n if(plus) n += calculate(s.substr(i+1, j-(i+1)));\n else n-= calculate(s.substr(i+1, j-(i+1)));\n i = j;\n }\n }\n\n return n;\n }\n};",
"memory": "80618"
} |
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 <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "1 + 1"
<strong>Output:</strong> 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = " 2-1 + 2 "
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "(1+(4+5+2)-3)+(6+8)"
<strong>Output:</strong> 23
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of digits, <code>'+'</code>, <code>'-'</code>, <code>'('</code>, <code>')'</code>, and <code>' '</code>.</li>
<li><code>s</code> represents a valid expression.</li>
<li><code>'+'</code> is <strong>not</strong> used as a unary operation (i.e., <code>"+1"</code> and <code>"+(2 + 3)"</code> is invalid).</li>
<li><code>'-'</code> could be used as a unary operation (i.e., <code>"-1"</code> and <code>"-(2 + 3)"</code> is valid).</li>
<li>There will be no two consecutive operators in the input.</li>
<li>Every number and running calculation will fit in a signed 32-bit integer.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculate(string s) {\n char sign = '+';\n std::stack<int> sta;\n int num = 0;\n\n for(int i = 0; i < s.size(); i++)\n {\n char c = s[i];\n if(isdigit(c)) num = num * 10 + (c - '0');\n if(c == '(') {\n int right = i, count = 0;\n for(; right < s.size(); right++) {\n if(s[right] == '(') count++;\n else if(s[right] == ')') count--;\n if(count == 0) break;\n }\n num = calculate(s.substr(i + 1, right - i - 1));\n i = right;\n }\n\n if(!isspace(c) && !isdigit(c) || i == s.size() - 1) {\n switch(sign) {\n case '+' : sta.push(num); break;\n case '-' : sta.push(-num); break;\n }\n sign = c;\n num = 0;\n }\n }\n\n int res = 0;\n while(!sta.empty()) {\n res += sta.top();\n sta.pop();\n }\n return res;\n }\n};",
"memory": "81556"
} |
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 <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "1 + 1"
<strong>Output:</strong> 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = " 2-1 + 2 "
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "(1+(4+5+2)-3)+(6+8)"
<strong>Output:</strong> 23
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of digits, <code>'+'</code>, <code>'-'</code>, <code>'('</code>, <code>')'</code>, and <code>' '</code>.</li>
<li><code>s</code> represents a valid expression.</li>
<li><code>'+'</code> is <strong>not</strong> used as a unary operation (i.e., <code>"+1"</code> and <code>"+(2 + 3)"</code> is invalid).</li>
<li><code>'-'</code> could be used as a unary operation (i.e., <code>"-1"</code> and <code>"-(2 + 3)"</code> is valid).</li>
<li>There will be no two consecutive operators in the input.</li>
<li>Every number and running calculation will fit in a signed 32-bit integer.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculate(string s) {\n char sign = '+';\n std::stack<int> sta;\n int num = 0;\n\n for(int i = 0; i < s.size(); i++) \n {\n char c = s[i];\n if(isdigit(c)) num = num * 10 + (c - '0');\n if(c == '(') {\n int right = i, count = 0;\n for(; right < s.size(); right++) {\n if(s[right] == '(') count++;\n else if(s[right] == ')') count--;\n if(count == 0) break;\n }\n num = calculate(s.substr(i + 1, right - i - 1));\n i = right;\n }\n\n if((!isspace(c) && !isdigit(c)) || i == s.size() - 1) {\n switch(sign) {\n case '+' : sta.push(num); break;\n case '-' : sta.push(-num); break;\n }\n sign = c;\n num = 0;\n }\n }\n\n int res = 0;\n while(!sta.empty()) {\n res += sta.top();\n sta.pop();\n }\n return res;\n }\n};",
"memory": "82493"
} |
226 | <p>Given the <code>root</code> of a binary tree, invert the tree, and return <em>its root</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/invert1-tree.jpg" style="width: 500px; height: 165px;" />
<pre>
<strong>Input:</strong> root = [4,2,7,1,3,6,9]
<strong>Output:</strong> [4,7,2,9,6,3,1]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/invert2-tree.jpg" style="width: 500px; height: 120px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> [2,3,1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void helper(TreeNode *node) {\n if (node == NULL) {\n return;\n }\n swap(node->left, node->right);\n helper(node->left);\n helper(node->right);\n }\n TreeNode* invertTree(TreeNode* root) {\n helper(root);\n return root;\n }\n};",
"memory": "11500"
} |
226 | <p>Given the <code>root</code> of a binary tree, invert the tree, and return <em>its root</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/invert1-tree.jpg" style="width: 500px; height: 165px;" />
<pre>
<strong>Input:</strong> root = [4,2,7,1,3,6,9]
<strong>Output:</strong> [4,7,2,9,6,3,1]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/invert2-tree.jpg" style="width: 500px; height: 120px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> [2,3,1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\n\nclass Solution {\npublic:\n void invertNode(TreeNode* root) {\n if (!root) {\n return;\n }\n invertNode( root->left);\n invertNode( root->right);\n TreeNode* tenp = root->left ;\n root->left = root->right;\n root->right = tenp;\n \n }\n TreeNode* invertTree(TreeNode* root) {\n if (root)\n invertNode(root);\n return root;\n \n }\n};",
"memory": "11600"
} |
226 | <p>Given the <code>root</code> of a binary tree, invert the tree, and return <em>its root</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/invert1-tree.jpg" style="width: 500px; height: 165px;" />
<pre>
<strong>Input:</strong> root = [4,2,7,1,3,6,9]
<strong>Output:</strong> [4,7,2,9,6,3,1]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/invert2-tree.jpg" style="width: 500px; height: 120px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> [2,3,1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* invertTree(TreeNode* root) {\n if(root == NULL) return NULL;\n invertTree(root->left);\n invertTree(root->right);\n swap(root->left, root->right);\n return root;\n }\n};",
"memory": "11600"
} |
226 | <p>Given the <code>root</code> of a binary tree, invert the tree, and return <em>its root</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/invert1-tree.jpg" style="width: 500px; height: 165px;" />
<pre>
<strong>Input:</strong> root = [4,2,7,1,3,6,9]
<strong>Output:</strong> [4,7,2,9,6,3,1]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/invert2-tree.jpg" style="width: 500px; height: 120px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> [2,3,1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
| 0 | {
"code": "\nclass Solution {\npublic:\n void helper(TreeNode* root){\n if(root == NULL) return;\n TreeNode* temp = root->left;\n root->left = root->right;\n root->right = temp;\n helper(root->left);\n helper(root->right);\n }\n TreeNode* invertTree(TreeNode* root) {\n helper(root);\n return root;\n }\n};",
"memory": "11700"
} |
226 | <p>Given the <code>root</code> of a binary tree, invert the tree, and return <em>its root</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/invert1-tree.jpg" style="width: 500px; height: 165px;" />
<pre>
<strong>Input:</strong> root = [4,2,7,1,3,6,9]
<strong>Output:</strong> [4,7,2,9,6,3,1]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/invert2-tree.jpg" style="width: 500px; height: 120px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> [2,3,1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* invertTree(TreeNode* root) {\n if(!root)\n return root;\n if(root -> left)\n invertTree(root -> left);\n if(root -> right)\n invertTree(root -> right);\n\n TreeNode* temp= root -> left;\n root -> left = root -> right;\n root -> right = temp;\n\n return root;\n }\n};",
"memory": "11700"
} |
226 | <p>Given the <code>root</code> of a binary tree, invert the tree, and return <em>its root</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/invert1-tree.jpg" style="width: 500px; height: 165px;" />
<pre>
<strong>Input:</strong> root = [4,2,7,1,3,6,9]
<strong>Output:</strong> [4,7,2,9,6,3,1]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/invert2-tree.jpg" style="width: 500px; height: 120px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> [2,3,1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* invertTree(TreeNode* root) {\n if (root == nullptr)\n return nullptr;\n\n invertTree(root->right);\n invertTree(root->left);\n swap(root->left, root->right);\n return root;\n }\n};",
"memory": "11800"
} |
226 | <p>Given the <code>root</code> of a binary tree, invert the tree, and return <em>its root</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/invert1-tree.jpg" style="width: 500px; height: 165px;" />
<pre>
<strong>Input:</strong> root = [4,2,7,1,3,6,9]
<strong>Output:</strong> [4,7,2,9,6,3,1]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/invert2-tree.jpg" style="width: 500px; height: 120px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> [2,3,1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\n static auto _ = [] {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();\nclass Solution {\npublic:\n TreeNode* invertTree(TreeNode* root) {\n if (root == nullptr)\n return nullptr;\n\n invertTree(root->right);\n invertTree(root->left);\n swap(root->left, root->right);\n return root;\n }\n};",
"memory": "11800"
} |
226 | <p>Given the <code>root</code> of a binary tree, invert the tree, and return <em>its root</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/invert1-tree.jpg" style="width: 500px; height: 165px;" />
<pre>
<strong>Input:</strong> root = [4,2,7,1,3,6,9]
<strong>Output:</strong> [4,7,2,9,6,3,1]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/invert2-tree.jpg" style="width: 500px; height: 120px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> [2,3,1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* invertTree(TreeNode* root)\n {\n transform(root);\n return root;\n }\n\n void transform(TreeNode* root)\n {\n if(!root) return;\n\n TreeNode* temp = root->left;\n root->left = root->right;\n root->right = temp;\n\n transform(root->left);\n transform(root->right);\n\n }\n};",
"memory": "11900"
} |
226 | <p>Given the <code>root</code> of a binary tree, invert the tree, and return <em>its root</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/invert1-tree.jpg" style="width: 500px; height: 165px;" />
<pre>
<strong>Input:</strong> root = [4,2,7,1,3,6,9]
<strong>Output:</strong> [4,7,2,9,6,3,1]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/invert2-tree.jpg" style="width: 500px; height: 120px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> [2,3,1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* invertTree(TreeNode* root) {\n if(root!=nullptr){\n TreeNode *left = root->left;\n TreeNode *right = root->right;\n root->left =right;\n root->right = left;\n invertTree(root->left);\n invertTree(root->right); \n }\n return root;\n }\n\n};",
"memory": "11900"
} |
226 | <p>Given the <code>root</code> of a binary tree, invert the tree, and return <em>its root</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/invert1-tree.jpg" style="width: 500px; height: 165px;" />
<pre>
<strong>Input:</strong> root = [4,2,7,1,3,6,9]
<strong>Output:</strong> [4,7,2,9,6,3,1]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/invert2-tree.jpg" style="width: 500px; height: 120px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> [2,3,1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* invertTree(TreeNode* root) {\n if(root == nullptr) return root;\n\n TreeNode *tmp = invertTree(root->left);\n root->left = invertTree(root->right);\n root->right = tmp;\n return root;\n }\n};",
"memory": "12000"
} |
226 | <p>Given the <code>root</code> of a binary tree, invert the tree, and return <em>its root</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/invert1-tree.jpg" style="width: 500px; height: 165px;" />
<pre>
<strong>Input:</strong> root = [4,2,7,1,3,6,9]
<strong>Output:</strong> [4,7,2,9,6,3,1]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/invert2-tree.jpg" style="width: 500px; height: 120px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> [2,3,1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* invertTree(TreeNode* root) {\n if(root == NULL){\n return NULL;\n }\n\n TreeNode* temp = root->left;\n root->left = root->right;\n root->right = temp;\n\n invertTree(root->left);\n invertTree(root->right);\n\n return root;\n }\n};",
"memory": "12000"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 0 | {
"code": "class Solution {\n inline bool isOp(char c) {\n return (c == '+') || (c == '-') || (c == '/') || (c == '*');\n }\npublic:\n int calculate(const string& s) {\n int total { 0 };\n int exp { 0 };\n char lastOp { '+' };\n int currNum { 0 };\n\n for (auto& c : s) {\n if (c == ' ') continue;\n if (c >= '0' && c <= '9') {\n currNum = currNum * 10 + (c - '0');\n } else if (isOp(c)) {\n if (lastOp == '+') {\n total += exp;\n exp = currNum;\n } else if (lastOp == '-') {\n total += exp;\n exp = -currNum;\n } else if (lastOp == '*') {\n exp *= currNum;\n } else {\n exp /= currNum;\n }\n currNum = 0;\n lastOp = c;\n }\n }\n if (lastOp == '+') {\n exp += currNum;\n } else if (lastOp == '-') {\n exp -= currNum;\n } else if (lastOp == '*') {\n exp *= currNum;\n } else {\n exp /= currNum;\n }\n total += exp;\n return total;\n }\n};",
"memory": "10406"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 0 | {
"code": "/**\n understanding\n we have an arithmetic expression with non-negative integers\n we need to evaluate the expression with normal order of operations\n\n approach\n \"3+2*2\" \n\n maintain\n total\n last op\n last expression\n last num\n\n when finish reading number (when reach operator or end):\n if last op is +/- => add last expression to total\n if last op is * or / => apply that op to last num and expression\n\n when read an operator =>\n do the above\n last_op = curr\n\n when reading space => ignore\n\n at end => apply last op to last num and last expression, then add last expression to result\n\n total = 0\n last op = +\n last expression = 0\n last num = 0\n\n\n*/\n\nclass Solution {\n inline bool isOp(char c) {\n return (c == '+') || (c == '-') || (c == '/') || (c == '*');\n }\npublic:\n int calculate(const string& s) {\n int total { 0 };\n int exp { 0 };\n char lastOp { '+' };\n int currNum { 0 };\n\n for (auto& c : s) {\n if (c == ' ') continue;\n if (c >= '0' && c <= '9') {\n currNum = currNum * 10 + (c - '0');\n } else if (isOp(c)) {\n if (lastOp == '+') {\n total += exp;\n exp = currNum;\n } else if (lastOp == '-') {\n total += exp;\n exp = -currNum;\n } else if (lastOp == '*') {\n exp *= currNum;\n } else {\n exp /= currNum;\n }\n currNum = 0;\n lastOp = c;\n }\n }\n if (lastOp == '+') {\n exp += currNum;\n } else if (lastOp == '-') {\n exp -= currNum;\n } else if (lastOp == '*') {\n exp *= currNum;\n } else {\n exp /= currNum;\n }\n total += exp;\n return total;\n }\n};",
"memory": "10406"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int calculate(string s) \n {\n int n = s.size(); \n int ret = 0; \n int sum = 0; \n int cur = 0;\n char sign = '+';\n\n for (int i = 0; i < n; ++i)\n {\n char c = s[i]; \n if (c >= '0' && c <= '9')\n {\n cur = cur * 10 + (c - '0');\n }\n else if (c == '(')\n {\n int count = 0; \n int j = 0; \n for (; i < n; ++i)\n {\n if (s[i] == '(') ++count; \n else if (s[i] == ')') --count; \n if (count == 0) break; \n }\n\n cur = calculate(s.substr(j, i - j - 1));\n }\n\n if (c == '+' || c == '-' || c == '*' || c == '/' || i == n - 1)\n {\n switch (sign)\n {\n case '+': sum += cur; break; \n case '-': sum -= cur; break; \n case '*': sum *= cur; break; \n case '/': sum /= cur; break; \n }\n\n if (c == '+' || c == '-' || i == n - 1)\n {\n ret += sum; \n sum = 0; \n }\n\n cur = 0; \n sign = c; \n }\n }\n return ret; \n }\n};\n\n// 2 * 3 + (4 + 5 * 9)",
"memory": "10820"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int calculate(string s) {\n int len = s.length();\n if (len == 0) {\n return 0;\n }\n\n int currentNumber = 0; \n int lastNumber = 0;\n int result = 0;\n char sign = '+';\n\n for (int i = 0; i < len; ++i) {\n char currentChar = s[i];\n\n if (isdigit(currentChar)) {\n currentNumber = (currentNumber * 10) + (currentChar - '0');\n }\n\n if (!isdigit(currentChar) && !iswspace(currentChar) || i == len - 1) {\n if (sign == '+' || sign == '-') {\n result += lastNumber;\n lastNumber = (sign == '+') ? currentNumber : -currentNumber;\n } else if (sign == '*') {\n lastNumber = lastNumber * currentNumber;\n } else if (sign == '/') {\n lastNumber = lastNumber / currentNumber;\n }\n sign = currentChar;\n currentNumber = 0;\n }\n }\n\n result += lastNumber;\n return result;\n }\n};",
"memory": "10820"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n set<char> operands{'+', '-', '*', '/'};\n\n int calculate(string s) {\n int result = 0;\n int temp = 0;\n int operand = 0;\n int index = 0;\n char op = '+';\n for (char c : s) {\n if (c >= '0' && c <= '9') {\n operand = operand * 10 + (c - '0');\n }\n if (operands.contains(c) || index == s.length() - 1) {\n if (op == '+' || op == '-') {\n result += temp;\n temp = op == '+' ? operand : -operand;\n } else if (op == '*') {\n temp *= operand;\n } else if (op == '/') {\n if (temp < 0) {\n temp = -1 * ((double)-temp)/operand;\n } else {\n temp /= operand;\n }\n }\n operand = 0;\n op = c;\n }\n ++index;\n }\n result += temp;\n return result;\n }\n\n int calculate_fromright(string s) {\n int operand_right = 0;\n bool have_operand_right = false;\n int index = s.length() - 1;\n while (index >= 0) {\n if (!have_operand_right) {\n operand_right = read_operand(s, &index);\n }\n if (index < 0) {\n return operand_right;\n }\n char one_operator = s[index--];\n int operand_left = read_operand(s, &index);\n int result = 0;\n if (one_operator == '+') {\n result = operand_left + operand_right;\n } else if (one_operator == '-') {\n result = operand_left - operand_right;\n } else if (one_operator == '*') {\n result = operand_left * operand_right;\n } else if (one_operator == '/') {\n result = floor((double)operand_left / (double)operand_right);\n }\n operand_right = result;\n have_operand_right = true;\n }\n return operand_right;\n }\n\nprivate:\n int read_operand(const string& s, int* index) {\n int i = *index;\n long num = 0;\n long multiplier = 1;\n while (i >= 0 && ((s[i] >= '0' && s[i] <= '9') || s[i] == ' ')) {\n if (s[i] == ' ') {\n --i;\n } else {\n num += (s[i--] - '0') * multiplier;\n if (i >= 0) {\n multiplier *= 10;\n }\n }\n }\n *index = i;\n return num;\n }\n};",
"memory": "11234"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int solve(int a,int b,char c){\n cout<<a<<\" \"<<b<<\" \"<<c<<endl;\n if(c=='+') return a+b;\n if(c=='-') return b-a;\n if(c=='*') return a*b;\n if(c=='/') return b/a;\n return 0;\n }\n int prio(char st){\n if(st=='+'||st=='-'){\n return 1;\n }\n else if(st=='*' || st=='/'){\n return 2;\n }\n else return 0;\n }\n int calculate(string s) {\n stack<int> nums;\n stack<char> op;\n int n=s.length();\n int newnum=0;\n for(int i=0;i<n;i++){\n if(s[i]>=48 && s[i]<=57){\n int x=s[i]-48;\n if(nums.empty()||newnum) nums.push(x);\n else{\n x=(nums.top()*10)+x;\n nums.pop();\n nums.push(x);\n }\n newnum=0;\n }\n else if(s[i]==' ') continue;\n else{\n newnum=1;\n if(op.empty()){\n op.push(s[i]);\n }\n else if(prio(s[i])>prio(op.top())){\n op.push(s[i]);\n }\n else{\n while((op.size()>0) && (prio(op.top())>=prio(s[i]))){\n char oper=op.top();\n op.pop();\n int a=nums.top();\n nums.pop();\n int b=nums.top();\n nums.pop();\n int vals=solve(a,b,oper);\n nums.push(vals);\n }\n op.push(s[i]);\n }\n }\n }\n while(op.size()!=0){\n char oper=op.top();\n op.pop();\n int a=nums.top();\n nums.pop();\n int b=nums.top();\n nums.pop();\n int vals=solve(a,b,oper);\n nums.push(vals);\n }\n return nums.top();\n }\n};",
"memory": "11234"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 0 | {
"code": "class Solution {\n int calc(int lhs, int rhs, char c) {\n if (c == '+') {\n return lhs+rhs;\n } else if (c == '-') {\n return lhs-rhs;\n } else if (c == '*') {\n return lhs*rhs;\n } else if (c == '/') {\n return lhs/rhs;\n }\n return 0;\n } \n\npublic:\n int calculate(string s) {\n\n int curRes = 0, tmpRes = 0, curInt = 0;\n char prevOp = '+';\n s += \"aa\";\n\n for (const char& c : s) {\n if (c == ' ') continue;\n if (isdigit(c)) {\n curInt = curInt*10 + (c-'0');\n } else {\n if (prevOp == '*') {\n tmpRes *= curInt;\n } else if (prevOp == '/') {\n tmpRes /= curInt;\n } else {\n curRes += tmpRes;\n tmpRes = prevOp == '+' ? curInt : -curInt;\n }\n prevOp = c;\n curInt = 0;\n } \n }\n return curRes;\n }\n};",
"memory": "11648"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int calcStack(string s) {\n\n stack<int> stk;\n\n char last_operation{'+'};\n int current_num{0};\n char current_char{'0'};\n\n for(int i = 0; i < s.length(); i++) {\n current_char = s[i];\n if(isdigit(current_char)) {\n current_num = current_num*10 + ((int)current_char - '0');\n }\n bool is_space = current_char == ' ';\n bool last_digit = i == s.length() - 1;\n\n // If we are the last digit or an operator, the same thing happens\n if(last_digit || !isdigit(current_char) && !is_space) {\n switch(last_operation) {\n case '+':\n stk.push(current_num);\n break;\n case '-':\n stk.push(current_num * -1);\n break;\n case '*':\n {\n int last_val = stk.top();\n stk.pop();\n last_val = last_val * current_num;\n stk.push(last_val);\n } \n break;\n case '/':\n {\n int last_val = stk.top();\n stk.pop();\n last_val = last_val / current_num;\n stk.push(last_val);\n } \n break;\n }\n\n current_num = 0;\n last_operation = current_char;\n\n }\n\n\n }\n \n int rval{0};\n while(!stk.empty()) {\n rval += stk.top();\n stk.pop();\n }\n\n return rval;\n }\n\nint calcSavedNum(string s) {\n\n int rval{0};\n\n int last_number{0};\n char last_operation{'+'};\n int current_num{0};\n char current_char{'0'};\n\n for(int i = 0; i < s.length(); i++) {\n current_char = s[i];\n if(isdigit(current_char)) {\n current_num = current_num*10 + ((int)current_char - '0');\n }\n bool is_space = current_char == ' ';\n bool last_digit = i == s.length() - 1;\n\n // If we are the last digit or an operator, the same thing happens\n if(last_digit || !isdigit(current_char) && !is_space) {\n switch(last_operation) {\n case '+':\n rval += last_number;\n last_number = current_num;\n break;\n case '-':\n rval += last_number;\n last_number = -1*current_num;\n break;\n case '*':\n last_number *= current_num;\n break;\n case '/':\n last_number /= current_num;\n break;\n }\n\n current_num = 0;\n last_operation = current_char;\n\n }\n\n\n }\n rval += last_number;\n return rval;\n }\n\n\n\n int calculate(string s) {\n return calcSavedNum(s);\n }\n};",
"memory": "11648"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int calculate(string s) {\n // operands are important\n // if it's + or - execute right away\n // if it's * or / gotta undo, operate, add.\n // remember to store negative previous by *-1\n int res = 0;\n int current = 0;\n int previous = 0;\n\n char op = '+';\n\n for(int idx = 0; idx < s.size(); idx++)\n {\n // check if we have a space\n if( s[idx] == ' ')\n {\n continue;\n }\n // check if we have a space\n if(isdigit(s[idx]))\n {\n string number;\n while((idx < s.size()) && isdigit(s[idx]))\n {\n number += s[idx];\n idx++;\n }\n // let outer loop handle the idx\n idx--;\n current = stoi(number);\n\n if(op == '+')\n {\n res += current;\n previous = current;\n } else if (op == '-')\n {\n res -= current;\n previous = current * -1;\n } else if (op == '/')\n {\n // undo op first\n res -= previous;\n res += (previous / current);\n previous = (previous / current);\n } else\n {\n // undo op first\n res -= previous;\n res += (current * previous);\n previous = current * previous;\n }\n current = 0;\n }\n else\n {\n op = s[idx];\n }\n }\n return res; \n }\n};",
"memory": "12061"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int calculate(string s) {\n int i = 0;\n int acc = 0;\n string num = \"\";\n while(i < s.size() && !isOperator(s[i])) {\n if (s[i] != ' ') num += s[i];\n i++;\n }\n int prev = stoi(num);\n\n while (i < s.size()) {\n char opr = s[i];\n i++;\n num = \"\";\n while(i < s.size() && !isOperator(s[i])) {\n if (s[i] != ' ') num += s[i];\n i++;\n }\n\n\n if (opr == '*') {\n prev *= stoi(num);\n } else if (opr == '/') {\n prev /= stoi(num);\n } else if (opr == '+') {\n acc += prev;\n prev = stoi(num);\n } else {\n acc += prev;\n prev = stoi(num)*-1;\n }\n\n }\n \n return acc + prev;\n }\n\n bool isOperator(char c) {\n return c == '*' || c == '/' || c == '+' || c == '-';\n }\n};",
"memory": "12061"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculate(string s) {\n vector<int> st;\n stack<char> ops;\n int num = 0;\n s.push_back('+');\n bool in_num = false;\n for (int i = 0; i < s.size(); ++i) {\n if (s[i] >= '0' && s[i] <= '9') {\n in_num = true;\n num = num*10 + (s[i]-'0');\n } else if (in_num) {\n // number finish\n in_num = false;\n if (ops.empty()) {\n st.push_back(num);\n num = 0;\n } else {\n char op = ops.top();\n ops.pop();\n if (op == '-') {\n st.push_back(-num);\n } else if (op == '*') {\n int a = st.back(); st.pop_back();\n st.push_back(a * num);\n } else if (op == '/') {\n int a = st.back(); st.pop_back();\n st.push_back(a / num);\n } else {\n st.push_back(num);\n }\n }\n num = 0;\n }\n \n if (s[i]=='+' || s[i] == '-' || s[i] == '*' || s[i] == '/') {\n ops.push(s[i]);\n }\n }\n\n int ans = 0;\n return accumulate(st.begin(), st.end(), 0);\n }\n};",
"memory": "14958"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculate(string s) {\n vector<int> st;\n int num = 0;\n s.push_back('+');\n char op = '+';\n for (int i = 0; i < s.size(); ++i) {\n if (s[i] == ' ')\n continue;\n if (s[i] >= '0' && s[i] <= '9') {\n num = num*10 + (s[i]-'0');\n } else {\n // number finish\n if (op == '-') {\n st.push_back(-num);\n } else if (op == '*') {\n int a = st.back(); st.pop_back();\n st.push_back(a * num);\n } else if (op == '/') {\n int a = st.back(); st.pop_back();\n st.push_back(a / num);\n } else {\n st.push_back(num);\n }\n num = 0;\n op = s[i];\n }\n }\n\n int ans = 0;\n return accumulate(st.begin(), st.end(), 0);\n }\n};",
"memory": "14958"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\n std::vector<int> stack;\n\n void updateStack(int number, char operation) {\n if (operation == '*') {\n stack.back() *= number;\n }\n else if (operation == '/') {\n stack.back() /= number;\n } else {\n stack.push_back(number);\n } \n }\n\npublic:\n int calculate(string s) {\n bool isNumber = false;\n int sign = 1;\n int number = 0;\n char operation = 0;\n\n for (auto c : s) {\n if (c == ' ' || c == '+' || c == '-' || c == '*' || c == '/') {\n if (isNumber) {\n updateStack(number, operation);\n sign = 1;\n operation = 0;\n number = 0;\n isNumber = false;\n }\n if (c == '-') {\n sign *= -1;\n }\n else if (c == '*' || c == '/') {\n operation = c;\n }\n }\n else if (c >= '0' && c <= '9') { //digit\n isNumber = true;\n number = 10*number + sign*( (int)(c) - int('0') );\n }\n }\n if (isNumber) {\n updateStack(number, operation);\n }\n int result = 0;\n for (auto num: stack) {\n result += num;\n }\n return result; \n }\n};",
"memory": "15371"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\n std::vector<int> stack;\n\n void updateStack(int number, char operation) {\n if (operation == '*') {\n std::cout << stack.back() << \" * \" << number << std::endl;\n stack.back() *= number;\n }\n else if (operation == '/') {\n std::cout << stack.back() << \" / \" << number << std::endl;\n stack.back() /= number;\n } else {\n stack.push_back(number);\n std::cout << number << std::endl;\n } \n }\npublic:\n int calculate(string s) {\n bool isNumber = false;\n int sign = 1;\n int number = 0;\n char operation = 0;\n\n for (auto c : s) {\n if (c == ' ' || c == '+' || c == '-' || c == '*' || c == '/') {\n if (isNumber) {\n updateStack(number, operation);\n sign = 1;\n operation = 0;\n number = 0;\n isNumber = false;\n }\n if (c == '-') {\n sign *= -1;\n }\n else if (c == '*' || c == '/') {\n operation = c;\n }\n }\n else if (c >= '0' && c <= '9') { //digit\n isNumber = true;\n number = 10*number + sign*( (int)(c) - int('0') );\n //std::cout << number << \" --> \";\n }\n }\n if (isNumber) {\n updateStack(number, operation);\n }\n int result = 0;\n for (auto num: stack) {\n result += num;\n }\n return result; \n }\n};",
"memory": "15371"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int associativity(char cur){\n if(cur=='+' || cur=='-') return 1;\n if(cur=='*' || cur=='/') return 2;\n return 0;\n }\n string postFix(string &s){\n stack<char> st;\n st.push('&');\n string res=\"\";\n int i=0,n=s.length();\n while(i<n){\n string t=\"\";\n // if(s[i]==' '){\n // i++;\n // }\n while(i<n && s[i]>='0' && s[i]<='9'){\n t+=s[i];i++;\n }\n if(t!=\"\"){res+=t;res+='#'; continue;}\n else if(s[i]!=' '){\n while( associativity(st.top())>=associativity(s[i])){\n res+=st.top();st.pop();\n }\n st.push(s[i]);\n }\n i++;\n \n \n }\n while(!st.empty()){\n res+=st.top();\n st.pop();\n }\n res.pop_back();\n return res;\n\n }\n int compute(int a,int b,char c){\n if(c=='+') return a+b;\n if(c=='-') return a-b;\n if(c=='*') return a*b;\n if(c=='/') return a/b;\n return 0;\n }\n int calculate(string s) {\n string ans= postFix(s);\n // cout<<ans<<\" \";\n int i=0;\n int n=ans.size();\n stack<int> st;\n while(i<n){\n string num=\"\";\n while(i<n && ans[i]>='0' && ans[i]<='9' ){\n num+=ans[i];\n i++;\n }\n if(num!=\"\"){\n st.push(stoi(num));continue;\n }\n else if(ans[i]!='#'){\n int b=st.top();\n st.pop();\n int a=st.top();\n st.pop();\n int val=compute(a,b,ans[i]);\n st.push(val);\n }i++;\n }\n return st.top();\n }\n};",
"memory": "15785"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int evaluate(int a, int b, char op){\n if(op == '+')\n return a;\n else if(op == '-')\n return -a;\n else if(op == '*')\n return a*b;\n else\n return a/b;\n }\n\n int calculate(string s) {\n stack<int> nums;\n int i = 0;\n s.push_back('@');\n int n = s.size();\n int cur = 0;\n char op = '+';\n while(i<n){\n if(s[i] == ' '){\n i++;\n } else if(s[i] >='0' && s[i] <='9' ){\n string numstr = \"\";\n while(i<n && s[i] >='0' && s[i] <='9'){\n numstr.push_back(s[i++]);\n }\n cur = stoi(numstr);\n } else {\n if(op == '*'||op == '/'){\n int v = nums.top(); nums.pop();\n nums.push(evaluate(v, cur, op));\n } else {\n nums.push(evaluate(cur, 0, op));\n }\n\n op = s[i++];\n cur = 0;\n }\n }\n\n int res = 0;\n while(!nums.empty()){\n res += nums.top();\n nums.pop();\n }\n return res;\n }\n};",
"memory": "15785"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long calculate(string s) {\n char sign = '+';\n long num = 0;\n s+='+';\n stack<long> stack;\n for(char a : s){\n if(isdigit(a)) num = num*10 + a-'0';\n\n if(a=='+' || a=='-' || a=='*' || a=='/'){\n if(sign == '+') stack.push(num);\n if(sign == '-') stack.push(num*(-1));\n if(sign == '*'){\n long prev = stack.top();\n stack.pop();\n stack.push(prev*num);\n }\n if(sign == '/'){\n long prev = stack.top();\n stack.pop();\n stack.push(prev/num);\n }\n sign = a;\n num = 0;\n }\n }\n long res = 0;\n while(stack.size()){\n res += stack.top();\n stack.pop();\n }\n return res;\n }\n};",
"memory": "16199"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculate(string s) {\n stack<int> s1;\n stack<char> s2;\n int i = 0;\n while (i < s.size()) {\n if (s[i] == ' ') {\n } else if (isdigit(s[i])) {\n // number\n int num = int(s[i]) - 48;\n i++;\n while (isdigit(s[i])) {\n num = num * 10;\n num = num + (int(s[i]) - 48);\n i++;\n }\n if (s2.size() != 0 && s2.top() == '-') {\n s1.push(-num);\n } else {\n s1.push(num);\n }\n continue;\n } else {\n // operator\n if (!s2.empty() && s2.top() == '/') {\n int n1 = s1.top();\n s1.pop();\n int n2 = s1.top();\n s1.pop();\n s1.push(n2 / n1);\n s2.pop();\n } else if (!s2.empty() && s2.top() == '*') {\n int n1 = s1.top();\n s1.pop();\n int n2 = s1.top();\n s1.pop();\n s1.push(n2 * n1);\n s2.pop();\n }\n s2.push(s[i]);\n }\n i++;\n }\n while (s2.size() != 0) {\n if (!s2.empty() && s2.top() == '/') {\n int n1 = s1.top();\n s1.pop();\n int n2 = s1.top();\n s1.pop();\n s1.push(n2 / n1);\n s2.pop();\n } else if (!s2.empty() && s2.top() == '*') {\n int n1 = s1.top();\n s1.pop();\n int n2 = s1.top();\n s1.pop();\n s1.push(n2 * n1);\n s2.pop();\n } \n // else if (!s2.empty() && s2.top() == '-') {\n // int n1 = s1.top();\n // s1.pop();\n // int n2 = s1.top();\n // s1.pop();\n // if (n1 < 0) {\n // s1.push(n1 - n2);\n // } else {\n // s1.push(n2 - n1);\n // }\n\n // s2.pop();\n // }\n else {\n int n1 = s1.top();\n s1.pop();\n int n2 = s1.top();\n s1.pop();\n s1.push(n2 + n1);\n s2.pop();\n }\n }\n return s1.top();\n }\n};",
"memory": "16199"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculate(string s) {\n \n // Is it safe to assume that s is a valid expression\n // S contains , + , - , * , / -> numbers and spaces\n // Is the number going to fit within 32- bit integer.\n \n // So I think we have some idea on what will be the inputs\n // 3 + 2 * 5 / 6 + 3 - 4\n // 5 * 7 - 3\n // 3 - 6 / 2\n // So we can maintain a stack of int which stores the +/- operation that would be required later.\n \n // +3, +2 *5\n int N = s.size();\n int current = 0; \n char currentSign = '+';\n stack<pair<char, int>> holdValues;\n \n for(int i=0; i<N; i++){\n \n if(s[i] == ' '){\n continue;\n }else if(s[i] >= '0' && s[i] <= '9'){\n \n current = current*10 + (s[i]-'0');\n }else{\n \n pushValues(holdValues, currentSign, current);\n \n current = 0;\n currentSign = s[i];\n }\n \n }\n \n pushValues(holdValues, currentSign, current);\n \n int answer = 0;\n \n while(!holdValues.empty()){\n pair<char, int> topValue = holdValues.top();\n holdValues.pop();\n \n answer += (topValue.first == '+') ? topValue.second : -topValue.second;\n }\n \n return answer;\n \n }\n \nprivate:\n void pushValues(stack<pair<char,int>>& holdValues, char sign, int number){\n \n if(holdValues.empty() || sign == '+' || sign == '-'){\n holdValues.push({sign, number});\n return;\n }\n \n pair<char, int> topValue = holdValues.top();\n holdValues.pop();\n if(sign == '*'){\n holdValues.push({topValue.first, topValue.second* number});\n }else{\n holdValues.push({topValue.first, topValue.second/number});\n }\n \n }\n};",
"memory": "16613"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "// 1. remove spaces\n// 2. use a stack, \n\n// 3+2*2\n\n// 3+2*\n// for i from 0 to s.length() - 1:\n// if s[i] is number:\n// get the num\n// if !stk.empty() && stk.top() is '*' or '/':\n// pop two and get the results and push back\n// else if s[i] is operator:\n// push into stack\n\n// number,op,number,op,number\n// seperate operator stack and number stack\n\nclass Solution {\npublic:\n int calculate(string s2) {\n string s;\n for (char c : s2) {\n if (c != ' ')\n s.push_back(c);\n }\n stack<int> nums;\n int sign = 1;\n char prevOp = '0';\n for (int i = 0; i < s.length(); i++) {\n int num = 0;\n while (i < s.length() && s[i] >= '0' && s[i] <= '9') {\n num = num * 10 + (s[i] - '0');\n i++;\n }\n if (prevOp == '*' || prevOp == '/') {\n int num2 = nums.top();\n nums.pop();\n\n if (prevOp == '*')\n nums.push(num2 * num);\n else\n nums.push(num2 / num);\n }\n else // push '+' or '-'\n nums.push(num * sign);\n if (s[i] == '+')\n sign = 1;\n else if (s[i] == '-')\n sign = -1;\n if (i != s.length())\n prevOp = s[i];\n }\n while (nums.size() > 1) {\n int num = nums.top();\n nums.pop();\n int num2 = nums.top();\n nums.pop();\n nums.push(num + num2);\n }\n return nums.top();\n }\n};",
"memory": "16613"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long atoi(string num){\n if(num.length() == 0)return 0;\n long long val = 0, mul = 1;\n for(int i = num.length()-1; i >=0 ; i--){\n val += ((num[i]-'0')*mul);\n mul *= 10;\n }\n return val;\n }\n\n int calc(string s, int &pos){\n stack<long long> st;\n char sign = '?';\n while(pos < s.length() && s[pos] != ')'){\n\n string num = \"\";\n while(s[pos] == ' ')pos++;\n while(s[pos] <= '9' && s[pos] >= '0'){\n num += s[pos];\n pos++;\n }\n long long val = atoi(num);\n\n if(sign == '+' || sign == '?'){\n st.push(val);\n }else if(sign == '-'){\n st.push(-1*val);\n }\n else if(sign == '/'){\n long long tp = st.top();\n st.pop();\n st.push(tp/val); \n }else if(sign == '*'){\n long long tp = st.top();\n st.pop();\n st.push(tp*val); \n }else if(s[pos] == '('){\n pos++;\n st.push(calc(s,pos));\n pos++;\n }\n\n if(s[pos] > '9' || s[pos] < '0'){\n sign = s[pos];\n } \n else{\n sign = '?';\n }\n pos++;\n }\n\n long long ans = 0;\n while(!st.empty()){\n ans += st.top();\n st.pop();\n }\n return ans;\n }\n\n int calculate(string s) {\n int ans = 0;\n for(int i = 0; i < s.length(); i++){\n ans += calc(s,i);\n }\n return ans;\n }\n};",
"memory": "17026"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculate(string s) {\n char op = '+';\n int i = 0;\n vector<long> operands;\n while (i < s.size()) {\n while (i < s.size() && !isdigit(s[i])) ++i;\n if (i == s.size()) break;\n long n = 0;\n while (i < s.size() && isdigit(s[i])) {\n n = 10L * n + s[i] - '0';\n ++i;\n }\n if (op == '+') operands.push_back(n);\n else if (op == '-') {\n operands.push_back(-n);\n } else if (op == '*') {\n n = n * operands.back();\n operands.pop_back();\n operands.push_back(n);\n } else if (op == '/') {\n n = operands.back() / n;\n operands.pop_back();\n operands.push_back(n);\n }\n\n while (i < s.size() && s[i] != '+' && s[i] != '-' && s[i] != '*' && s[i] != '/') {\n ++i;\n }\n if (i == s.size()) break;\n op = s[i];\n }\n long ans = 0;\n for (long n : operands) ans += n;\n return ans;\n }\n};",
"memory": "17026"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calc(int value1, char oper, int value2){\n switch(oper){\n case '+':\n return value1 + value2;\n case '-':\n return value1 - value2;\n case '*':\n return value1 * value2;\n case '/':\n return value1 / value2;\n }\n return 0;\n }\n int calculate(string s) {\n string temp = \"\";\n for(int i=0;i<s.length();++i){\n if(s[i]!= ' ') temp += s[i];\n }\n s = temp;\n stack<int> numbers;\n stack<char> opers;\n string num = \"\";\n bool sign = false;\n for(int i=0;i<s.length();++i){\n if(isdigit(s[i])){\n num += s[i];\n }else{\n int temp = std::stoi(num);\n if(sign){\n temp *= -1;\n sign = false;\n }\n if(s[i] == '-') {\n sign = true;\n s[i] = '+'; \n }\n numbers.push(temp);\n num = \"\";\n \n if(!opers.empty() && (opers.top() == '*' || opers.top() == '/')){\n int value2 = numbers.top();\n numbers.pop();\n int value1 = numbers.top();\n numbers.pop();\n char oper = opers.top();\n opers.pop();\n int result = calc(value1, oper, value2);\n numbers.push(result);\n }\n opers.push(s[i]);\n }\n }\n int last = std::stoi(num);\n if(sign){\n last *= -1;\n }\n numbers.push(last);\n while(!opers.empty()){\n int value2 = numbers.top();\n numbers.pop();\n int value1 = numbers.top();\n numbers.pop();\n char oper = opers.top();\n opers.pop();\n int result = calc(value1, oper, value2);\n numbers.push(result);\n }\n return numbers.top();\n }\n};",
"memory": "17440"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculate(string s) {\n int i=0;\n stack<int>st;\n stack<char>op;\n while(i<s.length())\n {\n if(s[i]>='0'&&s[i]<='9'){\n string num=\"\";\n while(i<s.length()&&s[i]>='0'&&s[i]<='9')\n {\n num+=s[i];\n i++;\n }\n int k=stoi(num);\n st.push(k);\n }else if(s[i]=='+')\n {\n op.push(s[i]);\n i++;\n }else if(s[i]=='-')\n {\n op.push(s[i]);\n i++;\n }else if(s[i]=='*'){\n int k=st.top();\n st.pop();\n i++;\n if(s[i]>='0'&&s[i]<='9'){\n string num=\"\";\n while(i<s.length()&&s[i]>='0'&&s[i]<='9')\n {\n num+=s[i];\n i++;\n }\n int r=stoi(num);\n k=k*r;\n st.push(k);\n }\n \n }else if(s[i]=='/'){\n int k=st.top();\n st.pop();\n i++;\n if(s[i]>='0'&&s[i]<='9'){\n string num=\"\";\n while(i<s.length()&&s[i]>='0'&&s[i]<='9')\n {\n num+=s[i];\n i++;\n }\n int r=stoi(num);\n k=k/r;\n st.push(k);\n }\n }else{\n i++;\n }\n }\n stack<int>posi;\n stack<int>negi;\n while(!op.empty()&&st.size()>=2)\n {\n if(op.top()=='+')\n {\n posi.push(st.top());\n st.pop();\n }else{\n negi.push(st.top());\n st.pop();\n }\n op.pop();\n }\n int ans=0;\n while(!st.empty())\n {\n ans+=st.top();\n st.pop();\n }\n while(!posi.empty())\n {\n ans+=posi.top();\n posi.pop();\n }\n while(!negi.empty())\n {\n ans-=negi.top();\n negi.pop();\n }\n return ans;\n }\n};",
"memory": "17440"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int64_t calculate(string s) {\n deque<char> operations_stack;\n deque<int64_t> numbers_stack;\n for (int64_t i = 0; i < s.size(); i++) {\n if (isdigit(s[i])) {\n int64_t n = 0;\n while(i < s.size() && isdigit(s[i])) {\n n = n * 10 + s[i] - '0';\n i++;\n }\n i--;\n numbers_stack.push_back(n);\n if (operations_stack.size()) {\n // handle mul/div\n char oper = operations_stack.back();\n switch(oper) {\n case '/': {\n int64_t n2 = numbers_stack.back();\n numbers_stack.pop_back();\n int64_t n1 = numbers_stack.back();\n numbers_stack.pop_back();\n numbers_stack.push_back(n1 / n2);\n operations_stack.pop_back();\n break;\n }\n case '*': {\n int64_t n2 = numbers_stack.back();\n numbers_stack.pop_back();\n int64_t n1 = numbers_stack.back();\n numbers_stack.pop_back();\n numbers_stack.push_back(n1 * n2);\n operations_stack.pop_back();\n break;\n }\n };\n }\n } else if (s[i] != ' ') {\n operations_stack.push_back(s[i]);\n }\n }\n \n while (numbers_stack.size() > 1) {\n char oper = operations_stack.front();\n switch(oper) {\n case '+': {\n int64_t n1 = numbers_stack.front();\n numbers_stack.pop_front();\n int64_t n2 = numbers_stack.front();\n numbers_stack.pop_front();\n numbers_stack.push_front(n1 + n2);\n operations_stack.pop_front();\n break;\n }\n case '-': {\n int64_t n1 = numbers_stack.front();\n numbers_stack.pop_front();\n int64_t n2 = numbers_stack.front();\n numbers_stack.pop_front();\n numbers_stack.push_front(n1 - n2);\n operations_stack.pop_front();\n break;\n }\n }\n }\n return numbers_stack.front();\n }\n};",
"memory": "17854"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculate(string s) {\n /*\n if its a mult or divide, go to next num and then do it, if its an add just put it onto stack\n \n */\n s += \"-\";\n long long currentNum = 0;\n char lastOp = '0';\n vector<long long> st;\n for (char c: s) {\n if (c == ' ') { continue; }\n if (c <= '9' && c >= '0') {\n currentNum *= 10;\n currentNum += (lastOp == '-' ? '0' - c : c - '0' );\n // if (lastOp == '-') {\n // currentNum = min(-1*currentNum, currentNum);\n // }\n continue;\n }\n if (lastOp == '*' || lastOp == '/') {\n long long last = st.back();\n if (lastOp == '*')\n st[st.size() - 1] = last * currentNum;\n else\n st[st.size() - 1] = last / currentNum;\n } else {\n st.push_back(currentNum);\n }\n lastOp = c;\n currentNum = 0;\n }\n return accumulate(st.begin(), st.end(), 0);\n\n }\n};",
"memory": "17854"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculate(string s) {\n stack<int> operant;\n stack<char> operato;\n string curNum;\n for (char c : s) {\n if (c >= '0' && c <= '9') {\n curNum.push_back(c);\n } else if (c == ' ') {\n continue;\n } else {\n int right = stoi(curNum);\n curNum = \"\";\n // process * and / \n if (!operato.empty()) {\n char op = operato.top();\n if (op == '*') {\n int left = operant.top();\n operant.pop();\n right = left * right;\n operato.pop();\n } else if (op == '/') {\n int left = operant.top();\n operant.pop();\n right = left / right;\n operato.pop();\n }\n }\n operant.push(right);\n operato.push(c);\n }\n }\n operant.push(stoi(curNum));\n if (!operato.empty()){\n char op = operato.top();\n if (op == '*' || op == '/') {\n operato.pop();\n int right = operant.top();\n operant.pop();\n int left = operant.top();\n operant.pop();\n if (op == '*') operant.push(left * right);\n else operant.push(left / right);\n }\n }\n stack<int> opr;\n stack<char> opt;\n while (!operato.empty()) {\n opt.push(operato.top());\n operato.pop();\n }\n while (!operant.empty()) {\n opr.push(operant.top());\n operant.pop();\n }\n while(!opt.empty()) {\n int left = opr.top();\n opr.pop();\n int right = opr.top();\n opr.pop();\n char op = opt.top();\n opt.pop();\n if (op == '+') {\n opr.push(left + right);\n } else if (op == '-') {\n opr.push(left - right);\n } else if (op == '*') {\n opr.push(left * right);\n } else if (op == '/') {\n opr.push(left / right);\n }\n }\n return opr.top();\n }\n};",
"memory": "18268"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n unordered_map<char, int> prec;\n\n Solution() {\n prec['/'] = 2;\n prec['*'] = 2;\n prec['+'] = 1;\n prec['-'] = 1;\n }\n\n int calculate(string s) {\n string postfixExp = postfix(s);\n return evaluatePostfix(postfixExp);\n }\n\nprivate:\n string postfix(string s) {\n stack<char> st;\n string res = \"\";\n for (int i = 0; i < s.size(); i++) {\n if (s[i] == ' ') continue; // Skip spaces\n if ('0' <= s[i] && s[i] <= '9') {\n string temp = \"\";\n while (i < s.size() && '0' <= s[i] && s[i] <= '9') {\n temp += s[i++];\n }\n res += temp + ' '; // Add space to separate numbers\n i--; // Correct the increment\n } else {\n while (!st.empty() && prec[st.top()] >= prec[s[i]]) {\n res += st.top();\n res += ' '; // Add space to separate operators\n st.pop();\n }\n st.push(s[i]);\n }\n }\n while (!st.empty()) {\n res += st.top();\n res += ' '; // Add space to separate operators\n st.pop();\n }\n return res;\n }\n\n int evaluatePostfix(string post) {\n stack<int> st;\n for (int i = 0; i < post.size(); i++) {\n if (post[i] == ' ') continue; // Skip spaces\n if ('0' <= post[i] && post[i] <= '9') {\n int num = 0;\n while (i < post.size() && '0' <= post[i] && post[i] <= '9') {\n num = num * 10 + (post[i++] - '0');\n }\n st.push(num);\n i--; // Correct the increment\n } else {\n int b = st.top(); st.pop();\n int a = st.top(); st.pop();\n switch (post[i]) {\n case '+': st.push(a + b); break;\n case '-': st.push(a - b); break;\n case '*': st.push(a * b); break;\n case '/': st.push(a / b); break;\n }\n }\n }\n return st.top();\n }\n};",
"memory": "18268"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n deque<int> qn;\n queue<char> qs;\n int calculate(string s) {\n string tmptmp = \"\";\n for (int i = 0; i < s.size(); i++)\n {\n if (s[i] != ' ') tmptmp += s[i];\n }\n s = tmptmp;\n for (int i = 0; i < s.size(); i++)\n {\n if ('0' <= s[i] && s[i] <= '9')\n {\n string tmps = \"\";\n while (i < s.size() && '0' <= s[i] && s[i] <= '9')\n {\n tmps += s[i];\n i++;\n }\n i--;\n qn.push_back(stoi(tmps));\n }\n else\n {\n if (s[i] == '+' || s[i] == '-')\n {\n qs.push(s[i]);\n }\n else\n {\n char tmp_sign = s[i];\n int prev = qn.back();\n qn.pop_back();\n string tmps = \"\";\n i++;\n while (i < s.size() && '0' <= s[i] && s[i] <= '9')\n {\n tmps += s[i];\n i++;\n };\n i--;\n int post = stoi(tmps);\n\n if (tmp_sign == '*')\n {\n qn.push_back(post * prev);\n }\n else if (tmp_sign == '/')\n {\n qn.push_back(prev / post);\n }\n }\n }\n }\n\n while (!qn.empty())\n {\n int n1 = qn.front();\n if (qs.empty())\n {\n return n1;\n }\n qn.pop_front();\n int n2 = qn.front();\n qn.pop_front();\n char c = qs.front();\n qs.pop();\n if (c == '+')\n {\n qn.push_front(n1 + n2);\n }\n else if (c == '-')\n {\n qn.push_front(n1 - n2);\n }\n }\n\n return -1;\n }\n};",
"memory": "18681"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculate(string s) {\n stack<char> sign;\n stack<int> num;\n\n string n=\"\";\n for(int i=0; i<s.size(); i++){\n if(s[i]!=' '){\n n.push_back(s[i]);\n }\n }\n s=n;\n int i=0;\n while(i<s.size()){\n if(s[i]=='+' || s[i]=='-' || s[i]=='*' || s[i]=='/'){\n if(s[i]=='/'){\n int x=num.top();\n num.pop();\n string tmp=\"\";\n i++;\n while(s[i]>='0' && s[i]<='9'){\n tmp.push_back(s[i]);\n i++;\n }\n i--;\n int y=stoi(tmp);\n int res=x/y;\n\n num.push(res);\n }\n else if(s[i]=='*'){\n int x=num.top();\n num.pop();\n string tmp=\"\";\n i++;\n while(s[i]>='0' && s[i]<='9'){\n tmp.push_back(s[i]);\n i++;\n }\n i--;\n int y=stoi(tmp);\n int res=x*y;\n\n num.push(res);\n }\n else{\n sign.push(s[i]);\n }\n }\n else if(s[i]>='0' && s[i]<='9'){\n string ans=\"\";\n while(s[i]<='9' && s[i]>='0'){\n ans.push_back(s[i]);\n i++;\n }\n i--;\n num.push(stoi(ans));\n }\n i++;\n }\n \n stack<int> number;\n while(!sign.empty()){\n if(sign.top()=='-'){\n number.push(num.top()*(-1));\n }\n\n else{\n number.push(num.top());\n }\n num.pop();\n sign.pop();\n }\n\n if(!num.empty()){\n number.push(num.top());\n }\n int ans=0;\n\n while(!number.empty()){\n ans=ans+number.top();\n number.pop();\n }\n\n return ans;\n }\n};",
"memory": "18681"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculate(string s) {\n int index = 0;\n stack<char> operators;\n stack<int> numbers;\n \n while (index < s.length()) {\n if (isspace(s[index])) {\n index++;\n continue;\n }\n\n if (!isdigit(s[index])) {\n operators.push(s[index++]);\n continue;\n }\n\n string num = \"\";\n while (index < s.length() && isdigit(s[index])) {\n num += s[index++];\n }\n int number = stoi(num);\n\n if (operators.empty() || (operators.top() == '+' || operators.top() == '-')) {\n numbers.push(number);\n continue;\n }\n\n int n = numbers.top();\n numbers.pop();\n if (operators.top() == '*')\n numbers.push(number * n);\n else if (operators.top() == '/')\n numbers.push(n / number);\n\n operators.pop();\n }\n\n stack<char> revOp;\n while (!operators.empty()) {\n revOp.push(operators.top());\n operators.pop();\n }\n\n stack<int> revNum;\n while (!numbers.empty()) {\n revNum.push(numbers.top());\n numbers.pop();\n } \n\n while (!revOp.empty() && !revNum.empty()) {\n int num1 = revNum.top();\n revNum.pop();\n\n if (revNum.empty())\n break;\n\n int num2 = revNum.top();\n revNum.pop();\n\n if (revOp.top() == '+')\n revNum.push(num1 + num2);\n else if (revOp.top() == '-')\n revNum.push(num1 - num2);\n\n revOp.pop();\n }\n\n if (revNum.empty())\n return -1;\n\n return revNum.top();\n }\n};",
"memory": "19095"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculate(string s) {\n s += 'e';\n vector<int> arr1;\n vector<char> arr2;\n int n = s.size();\n\n for (int i = 0; i < n; ++i){\n if (s[i] > '9' || s[i] < '0'){\n if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/'){\n arr2.emplace_back(s[i]);\n }\n continue;\n }\n int temp = 0;\n for (int j = i; j < n; ++j){\n if (s[j] > '9' || s[j] < '0'){\n arr1.emplace_back(temp);\n i = j;\n\n if (s[j] == '+' || s[j] == '-' || s[j] == '*' || s[j] == '/'){\n arr2.emplace_back(s[j]);\n }\n break;\n }else{\n temp = temp * 10 - '0' + s[j];\n }\n }\n }\n\n stack<int> stk1;\n stack<char> stk2;\n\n stk1.emplace(arr1[0]);\n stk2.emplace('+');\n \n for (int i = 1; i < arr1.size(); ++i){\n stk1.emplace(arr1[i]);\n stk2.emplace(arr2[i-1]);\n if (stk2.top() == '*'){\n int x = stk1.top();\n stk1.pop();\n int y = stk1.top();\n stk1.pop();\n stk1.emplace(y * x);\n stk2.pop();\n }else if (stk2.top() == '/'){\n int x = stk1.top();\n stk1.pop();\n int y = stk1.top();\n stk1.pop();\n stk1.emplace(y / x);\n stk2.pop();\n }\n }\n\n int ans = 0;\n while(!stk2.empty()){\n if (stk2.top() == '+'){\n ans += stk1.top();\n stk2.pop();\n stk1.pop();\n }else{\n ans -= stk1.top();\n stk2.pop();\n stk1.pop();\n }\n }\n return ans;\n }\n};",
"memory": "19095"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n struct Elem {\n enum Type {\n add = 0,\n sub = 1,\n mult = 2,\n div = 3,\n number = 5\n };\n Type type;\n int num =0;\n\n Elem(Type type) {\n this->type = type;\n }\n Elem(Type type, int num) {\n this->type = type;\n this->num = num;\n }\n };\n\n int calculate(string s) {\n int res = 0;\n stack<Elem> st;\n\n for (int ii =0; ii < s.size(); ++ii) {\n char ch = s.at(ii);\n\n if (ch == '+') {\n st.push(Elem(Elem::add));\n }\n if (ch == '-') {\n st.push(Elem(Elem::sub));\n }\n if (ch == '*') {\n st.push(Elem(Elem::mult));\n }\n if (ch == '/') {\n st.push(Elem(Elem::div));\n }\n\n int num = 0;\n if (ch >= '0' && ch <= '9') {\n while (ch >= '0' && ch <= '9') {\n num = num*10 - '0' + ch;\n ++ii;\n if (ii == s.size()) break;\n ch = s.at(ii);\n }\n --ii;\n res = ProcessStack(st, num);\n st.push(Elem(Elem::number, res));\n }\n }\n\n res = 0;\n while (!st.empty()) {\n Elem elem = st.top();\n st.pop();\n res += elem.num;\n }\n return res;\n }\n\n int ProcessStack(stack<Elem>& st, int start) {\n if (st.empty()) return start;\n\n Elem elem = st.top();\n if (elem.type == Elem::number) return start;\n\n if (elem.type == Elem::add) {\n st.pop();\n }\n if (elem.type == Elem::sub) {\n st.pop();\n start = -start;\n }\n if (elem.type == Elem::mult) {\n st.pop();\n\n Elem elem2 = st.top();\n st.pop();\n start = start * elem2.num;\n return ProcessStack(st, start);\n }\n if (elem.type == Elem::div) {\n st.pop();\n\n Elem elem2 = st.top();\n st.pop();\n start = elem2.num/start;\n return ProcessStack(st, start);\n }\n return start;\n }\n};",
"memory": "19509"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string s=\"\";\n stack<int> number;\n stack<char> operators;\n\n void process(string ss){\n for(char i:ss){\n if(i!=' ')s+=i;\n }\n }\n\n void popp(){\n int t2=number.top();\n number.pop();\n int t1=number.top();\n number.pop();\n if(operators.top()=='*')number.push(t1*t2);\n if(operators.top()=='/')number.push(t1/t2);\n operators.pop();\n } \n\n int calculate(string ss) {\n process(ss);\n int j;\n\n for(int i=0;i<s.size();i++){\n j=i;\n if(s[i]=='/'){\n operators.push('/');\n }\n else if(s[i]=='*'){\n operators.push('*');\n }\n else if(s[i]=='+'){\n while(operators.size() && (operators.top()=='/' || operators.top()=='*')){\n popp();\n }\n operators.push('+');\n }\n // if the number has - before it we dont perform subtration\n // instead we make the number -ve and put it into number\n // and in operators we put + instead\n else if(s[i]=='-'){\n i++;j=i;\n while(operators.size() && (operators.top()=='/' || operators.top()=='*')){\n popp();\n }\n while(i+1<s.size() && isdigit(s[i+1]))i++;\n cout<<(s.substr(j,i-j+1))<<\" \";\n number.push(-1*stoi(s.substr(j,i-j+1)));\n\n if( operators.size() && (operators.top()=='/' || operators.top()=='*'))popp();\n\n operators.push('+');\n }else{\n while(i+1<s.size() && isdigit(s[i+1]))i++;\n cout<<(s.substr(j,i-j+1))<<\" \";\n number.push(stoi(s.substr(j,i-j+1)));\n\n if( operators.size() && (operators.top()=='/' || operators.top()=='*'))popp();\n\n }\n }\n\n while(number.size()>1){\n if(operators.top()=='+'){\n int t2=number.top();\n number.pop();\n int t1=number.top();\n number.pop();\n number.push(t1+t2);\n }\n else if(operators.top()=='-'){\n int t2=number.top();\n number.pop();\n int t1=number.top();\n number.pop();\n number.push(t1-t2);\n }\n else if(operators.top()=='*'){\n int t2=number.top();\n number.pop();\n int t1=number.top();\n number.pop();\n number.push(t1*t2);\n }\n else{\n int t2=number.top();\n number.pop();\n int t1=number.top();\n number.pop();\n number.push(t1/t2);\n }\n operators.pop();\n }\n return number.top();\n }\n};",
"memory": "19509"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n class stack_entry {\n public:\n int val;\n enum op {\n NONE,\n ADD,\n SUB,\n MUL,\n DIV\n } op;\n\n stack_entry() = default;\n };\n\n int _calculate(int opnd1, enum stack_entry::op op, int opnd2) {\n int res;\n\n switch (op) {\n case stack_entry::ADD:\n res = opnd1 + opnd2;\n break;\n case stack_entry::SUB:\n res = opnd1 - opnd2;\n break;\n case stack_entry::MUL:\n res = opnd1 * opnd2;\n break;\n case stack_entry::DIV:\n res = opnd1 / opnd2;\n break;\n default:\n throw exception();\n }\n\n return res;\n }\n\npublic:\n int calculate(string s) {\n int i, j, n;\n int idx;\n int res;\n stack_entry *cur, *next;\n stack_entry *op;\n stack_entry *opnd1, *opnd2;\n vector<stack_entry> stk;\n\n static const enum stack_entry::op tbl[] = {\n [(unsigned char)'+'] = stack_entry::ADD,\n [(unsigned char)'-'] = stack_entry::SUB,\n [(unsigned char)'*'] = stack_entry::MUL,\n [(unsigned char)'/'] = stack_entry::DIV\n };\n\n n = s.length();\n\n stk.resize(n * 2);\n\n op = opnd1 = nullptr;\n i = 0;\n idx = 0;\n for (;;) {\n if (i == n)\n break;\n if (s[i] == ' ') {\n ++i;\n continue;\n }\n if (isdigit(s[i])) {\n long long val;\n\n val = s[i] - '0';\n for (j = i + 1; j < n && isdigit(s[j]); j++)\n val = val * 10 + s[j] - '0';\n stk[idx].val = val;\n stk[idx].op = stack_entry::NONE;\n cur = &stk[idx++];\n if (op == nullptr || (op->op != stack_entry::MUL && op->op != stack_entry::DIV))\n opnd1 = cur;\n else {\n opnd2 = cur;\n idx -= 3;\n stk[idx].val = _calculate(opnd1->val, op->op, opnd2->val);\n stk[idx].op = stack_entry::NONE;\n opnd1 = &stk[idx++];\n }\n i = j;\n } else {\n stk[idx].op = tbl[(unsigned char)s[i]];\n op = &stk[idx++];\n ++i;\n }\n }\n\n op = nullptr;\n res = stk[0].val;\n for (i = 1; i < idx; i++) {\n cur = &stk[i];\n if (cur->op != stack_entry::NONE)\n op = cur;\n else {\n opnd2 = cur;\n res = _calculate(res, op->op, opnd2->val);\n }\n }\n\n return res;\n }\n};",
"memory": "19923"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\n\n int solve(string &s, int l, int r)\n {\n if (r<l)\n return 0;\n\n for (int i=r;i>=l;i--)\n {\n if (s[i] == '-')\n {\n return solve(s, l, i-1) - solve(s, i+1, r);\n }\n \n if (s[i] == '+')\n {\n return solve(s, l, i-1) + solve(s, i+1, r);\n }\n }\n\n for (int i=r;i>=l;i--)\n {\n if (s[i] == '*')\n {\n return solve(s, l, i-1) * solve(s, i+1, r);\n }\n\n if (s[i] == '/')\n {\n return solve(s, l, i-1) / solve(s, i+1, r);\n }\n }\n\n int num = 0;\n for (int i=l;i<=r;i++){\n if (s[i] == ' ') \n continue;\n num = num*10 + (s[i]-'0');\n }\n\n return num;\n }\n\npublic:\n int calculate(string s) {\n /*\n * / %\n + -\n */\n\n return solve(s, 0, s.length() - 1);\n }\n\n /*\n N is number of ops\n tc - O(N*N)\n sc - O(N)\n */ \n};",
"memory": "19923"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n class stack_entry {\n public:\n int val;\n enum op {\n NONE,\n ADD,\n SUB,\n MUL,\n DIV\n } op;\n\n stack_entry() = default;\n };\n\n int _calculate(int opnd1, enum stack_entry::op op, int opnd2) {\n int res;\n\n switch (op) {\n case stack_entry::ADD:\n res = opnd1 + opnd2;\n break;\n case stack_entry::SUB:\n res = opnd1 - opnd2;\n break;\n case stack_entry::MUL:\n res = opnd1 * opnd2;\n break;\n case stack_entry::DIV:\n res = opnd1 / opnd2;\n break;\n default:\n throw exception();\n }\n\n return res;\n }\n\npublic:\n int calculate(string s) {\n int i, j, n;\n int idx;\n int res;\n stack_entry *cur, *next;\n stack_entry *op;\n stack_entry *opnd1, *opnd2;\n vector<stack_entry> stk;\n\n static const enum stack_entry::op tbl[] = {\n [(unsigned char)'+'] = stack_entry::ADD,\n [(unsigned char)'-'] = stack_entry::SUB,\n [(unsigned char)'*'] = stack_entry::MUL,\n [(unsigned char)'/'] = stack_entry::DIV\n };\n\n n = s.length();\n\n stk.resize(n * 2);\n\n op = opnd1 = nullptr;\n i = 0;\n idx = 0;\n for (;;) {\n if (i == n)\n break;\n if (s[i] == ' ') {\n ++i;\n continue;\n }\n if (isdigit(s[i])) {\n long long val;\n\n val = s[i] - '0';\n for (j = i + 1; j < n && isdigit(s[j]); j++)\n val = val * 10 + s[j] - '0';\n stk[idx].val = val;\n stk[idx].op = stack_entry::NONE;\n cur = &stk[idx++];\n if (op == nullptr || (op->op != stack_entry::MUL && op->op != stack_entry::DIV))\n opnd1 = cur;\n else {\n opnd2 = cur;\n idx -= 3;\n stk[idx].val = _calculate(opnd1->val, op->op, opnd2->val);\n stk[idx].op = stack_entry::NONE;\n opnd1 = &stk[idx++];\n }\n i = j;\n } else {\n stk[idx].op = tbl[(unsigned char)s[i]];\n op = &stk[idx++];\n ++i;\n }\n }\n\n op = nullptr;\n res = stk[0].val;\n for (i = 1; i < idx; i++) {\n cur = &stk[i];\n if (cur->op != stack_entry::NONE)\n op = cur;\n else {\n opnd2 = cur;\n res = _calculate(res, op->op, opnd2->val);\n }\n }\n\n return res;\n }\n};",
"memory": "20336"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n class stack_entry {\n public:\n int val;\n enum op {\n NONE,\n ADD,\n SUB,\n MUL,\n DIV\n } op;\n\n stack_entry() = default;\n };\n\n int _calculate(int opnd1, enum stack_entry::op op, int opnd2) {\n int res;\n\n switch (op) {\n case stack_entry::ADD:\n res = opnd1 + opnd2;\n break;\n case stack_entry::SUB:\n res = opnd1 - opnd2;\n break;\n case stack_entry::MUL:\n res = opnd1 * opnd2;\n break;\n case stack_entry::DIV:\n res = opnd1 / opnd2;\n break;\n default:\n throw exception();\n }\n\n return res;\n }\n\npublic:\n int calculate(string s) {\n int i, j, n;\n int idx;\n int res;\n stack_entry *cur, *next;\n stack_entry *op;\n stack_entry *opnd1, *opnd2;\n vector<stack_entry> stk;\n\n static const enum stack_entry::op tbl[] = {\n [(unsigned char)'+'] = stack_entry::ADD,\n [(unsigned char)'-'] = stack_entry::SUB,\n [(unsigned char)'*'] = stack_entry::MUL,\n [(unsigned char)'/'] = stack_entry::DIV\n };\n\n n = s.length();\n\n stk.resize(n * 2);\n\n op = opnd1 = nullptr;\n i = 0;\n idx = 0;\n for (;;) {\n if (i == n)\n break;\n if (s[i] == ' ') {\n ++i;\n continue;\n }\n if (isdigit(s[i])) {\n long long val;\n\n val = s[i] - '0';\n for (j = i + 1; j < n && isdigit(s[j]); j++)\n val = val * 10 + s[j] - '0';\n stk[idx].val = val;\n stk[idx].op = stack_entry::NONE;\n cur = &stk[idx++];\n if (op == nullptr || (op->op != stack_entry::MUL && op->op != stack_entry::DIV))\n opnd1 = cur;\n else {\n opnd2 = cur;\n idx -= 3;\n stk[idx].val = _calculate(opnd1->val, op->op, opnd2->val);\n stk[idx].op = stack_entry::NONE;\n opnd1 = &stk[idx++];\n }\n i = j;\n } else {\n stk[idx].op = tbl[(unsigned char)s[i]];\n op = &stk[idx++];\n ++i;\n }\n }\n\n op = nullptr;\n res = stk[0].val;\n for (i = 1; i < idx; i++) {\n cur = &stk[i];\n if (cur->op != stack_entry::NONE)\n op = cur;\n else {\n opnd2 = cur;\n res = _calculate(res, op->op, opnd2->val);\n }\n }\n\n return res;\n }\n};",
"memory": "20336"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n vector<int> parse(string str){\n int num = 0;\n int n = str.length();\n\n bool zf = false;\n vector<int>nums;\n\n for(int i=0;i<n;i++){\n if(str[i] == ' ') continue;\n else if(str[i]>='0' && str[i]<='9'){\n num *= 10;\n num += str[i] - '0';\n if(str[i] == '0') zf = true;\n else zf = false;\n }\n else {\n nums.push_back(num);\n num = 0;\n }\n }\n if(num || zf) nums.push_back(num);\n return nums;\n }\n\n int calculate(string s) {\n vector<int> nums;\n int n = s.length();\n nums = parse(s);\n\n for(auto x: nums) cout<<x<<\" \";\n cout<<endl;\n\n stack<int>st;\n st.push(nums[0]);\n int ind = 1;\n for(int i = 0;i<n;i++){\n if(s[i] == '+' || s[i] == '-') st.push(nums[ind++]);\n else if(s[i] == '*'){\n int a = st.top();st.pop();\n int b = nums[ind++];\n st.push(a * b);\n }\n else if(s[i] == '/'){\n int a = st.top();st.pop();\n int b = nums[ind++];\n st.push(a / b);\n }\n }\n\n // reverse stack.\n stack<int>st2;\n while(!st.empty()){\n int ele = st.top();st.pop();\n st2.push(ele);\n }\n\n for(int i =0;i<n;i++){\n if(s[i] == '-'){\n int a = st2.top();st2.pop();\n int b = st2.top();st2.pop();\n st2.push(a-b);\n }\n else if(s[i] == '+'){\n int a = st2.top();st2.pop();\n int b = st2.top();st2.pop();\n st2.push(a+b);\n }\n }\n\n return st2.top();\n // return -1;\n }\n};",
"memory": "20750"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculate(string s) {\n stack<int> stk;\n int num = 0;\n bool resolve = false;\n for (char c : s) {\n if (c >= '0' and c <= '9') {\n num = num * 10 + (c - '0');\n } else if (c == ' ') {\n continue;\n } else {\n stk.push(num);\n num = 0;\n if (resolve) {\n int op2 = stk.top();\n stk.pop();\n int op = stk.top();\n stk.pop();\n int op1 = stk.top();\n stk.pop();\n cout << \"::: \" << op1 << \" \" << op << \" \" << op2 << endl;\n\n if (op == -1) {\n stk.push(op1 * op2);\n } else {\n stk.push(op1 / op2);\n }\n resolve = false;\n }\n\n if (c == '*') {\n stk.push(-1);\n resolve = true;\n } else if (c == '/') {\n stk.push(-2);\n resolve = true;\n } else if (c == '+') {\n stk.push(-3);\n } else if (c == '-') {\n stk.push(-4);\n } else {\n cout << \"unhandled\" << c << endl;\n return -1;\n }\n }\n }\n\n stk.push(num);\n num = 0;\n if (resolve) {\n int op2 = stk.top();\n stk.pop();\n int op = stk.top();\n stk.pop();\n int op1 = stk.top();\n stk.pop();\n cout << \"::: \" << op1 << \" \" << op << \" \" << op2 << endl;\n\n if (op == -1) {\n stk.push(op1 * op2);\n } else {\n stk.push(op1 / op2);\n }\n resolve = false;\n }\n vector<int> v;\n while(!stk.empty()){\n v.push_back(stk.top());\n stk.pop();\n }\n reverse(v.begin(), v.end());\n\n int res = v[0];\n\n for(int i = 1; i < v.size()-1; i+=2){\n int op = v[i], x = v[i+1];\n cout << res << \" \" << op << \" \" << x << endl;\n if(op == -3) res += x;\n else if(op == -4) res -= x;\n else cout << \"unhandled\" << endl; \n }\n\n return res;\n }\n};\n\n",
"memory": "20750"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n class stack_entry {\n public:\n int val;\n enum op {\n NONE,\n ADD,\n SUB,\n MUL,\n DIV\n } op;\n\n stack_entry() = default;\n\n stack_entry(int v, enum op operation) {\n val = v;\n op = operation;\n }\n };\n\n int _calculate(int opnd1, enum stack_entry::op op, int opnd2) {\n int res;\n\n switch (op) {\n case stack_entry::ADD:\n res = opnd1 + opnd2;\n break;\n case stack_entry::SUB:\n res = opnd1 - opnd2;\n break;\n case stack_entry::MUL:\n res = opnd1 * opnd2;\n break;\n case stack_entry::DIV:\n res = opnd1 / opnd2;\n break;\n default:\n throw exception();\n }\n\n return res;\n }\n\npublic:\n int calculate(string s) {\n int i, j, n;\n int idx;\n int res;\n stack_entry *cur, *next;\n stack_entry *op;\n stack_entry *opnd1, *opnd2;\n vector<stack_entry> stk;\n\n static const enum stack_entry::op tbl[] = {\n [(unsigned char)'+'] = stack_entry::ADD,\n [(unsigned char)'-'] = stack_entry::SUB,\n [(unsigned char)'*'] = stack_entry::MUL,\n [(unsigned char)'/'] = stack_entry::DIV\n };\n\n n = s.length();\n\n stk.resize(n * 2);\n\n op = opnd1 = nullptr;\n i = 0;\n idx = 0;\n for (;;) {\n if (i == n)\n break;\n if (s[i] == ' ') {\n ++i;\n continue;\n }\n if (isdigit(s[i])) {\n int val;\n\n for (j = i + 1; j < n && isdigit(s[j]); j++)\n ;\n val = stoi(string(begin(s) + i, begin(s) + j), nullptr, 10);\n stk[idx] = stack_entry(val, stack_entry::NONE);\n cur = &stk[idx++];\n if (op == nullptr || (op->op != stack_entry::MUL && op->op != stack_entry::DIV))\n opnd1 = cur;\n else {\n opnd2 = cur;\n idx -= 3;\n stk[idx] = stack_entry(_calculate(opnd1->val, op->op, opnd2->val), stack_entry::NONE);\n opnd1 = &stk[idx++];\n }\n i = j;\n } else {\n stk[idx] = stack_entry(-1, tbl[(unsigned char)s[i]]);\n op = &stk[idx++];\n ++i;\n }\n }\n\n op = nullptr;\n res = stk[0].val;\n for (i = 1; i < idx; i++) {\n cur = &stk[i];\n if (cur->op != stack_entry::NONE)\n op = cur;\n else {\n opnd2 = cur;\n res = _calculate(res, op->op, opnd2->val);\n }\n }\n\n return res;\n }\n};",
"memory": "21164"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n class stack_entry {\n public:\n int val;\n enum op {\n NONE,\n ADD,\n SUB,\n MUL,\n DIV\n } op;\n\n stack_entry() = default;\n\n stack_entry(int v, enum op operation) {\n val = v;\n op = operation;\n }\n };\n\n int _calculate(int opnd1, enum stack_entry::op op, int opnd2) {\n int res;\n\n switch (op) {\n case stack_entry::ADD:\n res = opnd1 + opnd2;\n break;\n case stack_entry::SUB:\n res = opnd1 - opnd2;\n break;\n case stack_entry::MUL:\n res = opnd1 * opnd2;\n break;\n case stack_entry::DIV:\n res = opnd1 / opnd2;\n break;\n default:\n throw exception();\n }\n\n return res;\n }\n\npublic:\n int calculate(string s) {\n int i, j, n;\n int idx;\n int res;\n stack_entry *cur, *next;\n stack_entry *op;\n stack_entry *opnd1, *opnd2;\n vector<stack_entry> stk;\n\n static const enum stack_entry::op tbl[] = {\n [(unsigned char)'+'] = stack_entry::ADD,\n [(unsigned char)'-'] = stack_entry::SUB,\n [(unsigned char)'*'] = stack_entry::MUL,\n [(unsigned char)'/'] = stack_entry::DIV\n };\n\n n = s.length();\n\n stk.resize(n * 2);\n\n op = opnd1 = nullptr;\n i = 0;\n idx = 0;\n for (;;) {\n if (i == n)\n break;\n if (s[i] == ' ') {\n ++i;\n continue;\n }\n if (isdigit(s[i])) {\n int val;\n\n for (j = i + 1; j < n && isdigit(s[j]); j++)\n ;\n val = stoi(string(begin(s) + i, begin(s) + j), nullptr, 10);\n stk[idx] = stack_entry(val, stack_entry::NONE);\n cur = &stk[idx++];\n if (op == nullptr || (op->op != stack_entry::MUL && op->op != stack_entry::DIV))\n opnd1 = cur;\n else {\n opnd2 = cur;\n idx -= 3;\n stk[idx] = stack_entry(_calculate(opnd1->val, op->op, opnd2->val), stack_entry::NONE);\n opnd1 = &stk[idx++];\n }\n i = j;\n } else {\n stk[idx] = stack_entry(-1, tbl[(unsigned char)s[i]]);\n op = &stk[idx++];\n ++i;\n }\n }\n\n op = nullptr;\n res = stk[0].val;\n for (i = 1; i < idx; i++) {\n cur = &stk[i];\n if (cur->op != stack_entry::NONE)\n op = cur;\n else {\n opnd2 = cur;\n res = _calculate(res, op->op, opnd2->val);\n }\n }\n\n return res;\n }\n};",
"memory": "21578"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution \n{\npublic:\n \n void help1(int num,int &ans)\n {\n ans=ans+num;\n return;\n }\n \n void help2(int num,int &ans)\n {\n ans=ans-num;\n return;\n }\n \n int calculate(string s) \n {\n vector<int>v1;\n int i,l=s.length();\n string temp=\"\";\n for(i=0;i<l;i++)\n {\n if(s[i]!='+' && s[i]!='-' && s[i]!='*' && s[i]!='/')\n {\n temp.push_back(s[i]);\n }\n else\n {\n v1.push_back(stoi(temp,nullptr,10));\n temp=\"\";\n if(s[i]=='+')\n {\n v1.push_back(-1);\n }\n else if(s[i]=='-')\n {\n v1.push_back(-2);\n }\n else if(s[i]=='*')\n {\n v1.push_back(-3);\n }\n else\n {\n v1.push_back(-4);\n }\n }\n }\n \n v1.push_back(stoi(temp,nullptr,10));\n \n vector<int>v2;\n for(i=0;i<v1.size();)\n {\n if(v1[i]!=-3 && v1[i]!=-4)\n {\n v2.push_back(v1[i]);\n i++;\n }\n else\n {\n int ms1=v2[v2.size()-1];\n v2.pop_back();\n int ms2=v1[i+1];\n if(v1[i]==-3)\n {\n v2.push_back(ms1*ms2);\n }\n else if(v1[i]==-4)\n {\n v2.push_back(ms1/ms2);\n }\n i=i+2;\n }\n }\n \n if(v2.size()==1)\n {\n return v2[0];\n }\n \n int ans=v2[0];\n for(i=1;i<v2.size(); )\n {\n if(v2[i]==-1)\n help1(v2[i+1],ans);\n else\n help2(v2[i+1],ans);\n i=i+2; \n }\n return ans;\n \n \n }\n};",
"memory": "24060"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculate(string s) {\n stack<pair<int,char>>st;\n int sign = 1;\n long long ans = 0;\n for(int i=0;i<s.length();i++){\n if(isdigit(s[i])){\n long long num = 0;\n while(i < s.length() && isdigit(s[i])){\n num = num*10 + (s[i] - '0');\n i++;\n }\n i--;\n if(!st.empty() && (st.top().second == '*' || st.top().second == '/')){\n if(st.top().second=='*'){\n st.pop();\n num*=st.top().first;\n st.pop();\n }\n else{\n st.pop();\n num = st.top().first/num;\n st.pop();\n }\n }\n st.push({num,'+'});\n }\n\n else if(s[i] == '+')st.push({-1,'+'});\n else if(s[i] == '-')st.push({-1,'-'});\n else if(s[i] == '*')st.push({-1,'*'});\n else if(s[i] == '/')st.push({-1,'/'});\n }\n vector<int>vec;\n while(!st.empty()){\n if(st.top().first == -1){\n if(st.top().second == '-')vec.push_back(-2);\n else vec.push_back(-1);\n }\n else vec.push_back(st.top().first);\n st.pop();\n }\n reverse(vec.begin(),vec.end());\n if(vec.size())\n ans = vec[0];\n for(int i=1;i<vec.size();i++){\n if(vec[i] == -1){\n ans += vec[i+1];\n }\n else{\n ans -= vec[i+1];\n }\n i++;\n }\n return ans;\n }\n};",
"memory": "24060"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculate(string s) {\n string t;\n int pre=0;\n for(int i=0;i<s.size();i++){\n if(s[i]==' ')continue;\n t+=s[i];\n \n }\n cout<<t<<endl;\n // stack<int>st;\n vector<long long int>v;\n for(int i=0;i<t.size();){\n if(t[i]=='*'){v.push_back(1);i++;}\n else if(t[i]=='+'){v.push_back(2);i++;}\n else if(t[i]=='/'){v.push_back(-1);i++;}\n else if(t[i]=='-'){v.push_back(-2); i++;}\n else {\n long long int ctn=0;\n while(i<t.size()&&t[i]>='0'&&t[i]<='9'){\n ctn=ctn*10+t[i]-'0';\n i++;\n }\n // i--;\n v.push_back(ctn);\n }\n }\n for(auto i:v)cout<<i<<\" \";\n // ////\n for(int i=1;i<v.size();){\n if(v[i]==-1){\n long long int c=v[i-1]/v[i+1];\n v[i-1]=c;\n v.erase(v.begin()+i);\n v.erase(v.begin()+i);\n }\n else if(v[i]==1){\n long long int c=v[i-1]*v[i+1];\n v[i-1]=c;\n v.erase(v.begin()+i);\n v.erase(v.begin()+i);\n }\n else i=i+2;\n }\n // for(int i=1;i<v.size();){\n \n // else i=i+2;\n // }\n // cout<<endl;\n // for(auto i:v)cout<<i<<\" \";\n // for(int i=1;i<v.size();){\n // if(v[i]==2){\n // long long int c=v[i-1]+v[i+1];\n // v[i-1]=c;\n // cout<<c<<\" 8888 \"<<endl;\n // v.erase(v.begin()+i);\n // v.erase(v.begin()+i);\n // }\n // else i=i+2;\n // }\n // cout<<endl;\n // for(auto i:v)cout<<i<<\" \";\n // for(int i=1;i<v.size();){\n // if(v[i]==-2){\n // long long int c=(v[i-1]-v[i+1]);\n // v[i-1]=c;\n // v.erase(v.begin()+i);\n // v.erase(v.begin()+i);\n // }\n // else i=i+2;\n // }\n // return v[0];\n long long int ans=0;\n for(int i=1;i<v.size();i=i+2){\n if(v[i]==2)v[i+1]=v[i+1];\n else if(v[i]==-2)v[i+1]=-v[i+1];\n }\n for(int i=0;i<v.size();i=i+2)ans+=v[i];\n return ans;\n\n }\n};",
"memory": "24474"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculate(string s) {\n stack<pair<int,bool>> st;\n\n // solve divide first and multiply first\n int i=0;\n while(i<s.size()){\n\n // case 1 : space\n if(s[i] == ' '){\n i++;\n continue;\n }\n\n // case 2 : numbers\n if(s[i]>='0' && s[i]<='9'){\n string num = \"\";\n while(i<s.size() && (s[i]>='0' && s[i]<='9')){\n num.push_back(s[i]);\n i++;\n }\n int number = stoi(num);\n st.push({number,0});\n }\n \n\n // case 3 : + -\n else if(s[i]=='+' || s[i]=='-'){\n if(s[i]=='+') st.push({1,1});\n else st.push({2,1});\n i++;\n }\n \n\n // case 4 : * /\n else{\n bool mul = s[i] == '*';\n int num1 = st.top().first;\n st.pop();\n\n while(s[i]<'0' || s[i]>'9') i++;\n string num = \"\";\n while(i<s.size() && (s[i]>='0' && s[i]<='9')){\n num.push_back(s[i]);\n i++;\n }\n int num2 = stoi(num);\n int result = mul ? num1*num2 : num1/num2;\n st.push({result,0});\n }\n }\n\n // now solve addition and subtraction\n stack<pair<int,bool>> st2;\n while(!st.empty()){\n st2.push(st.top());\n st.pop();\n }\n\n int ans = st2.top().first;\n st2.pop();\n while(!st2.empty()){\n int num1 = ans;\n bool add = st2.top().first == 1;\n st2.pop();\n int num2 = st2.top().first;\n st2.pop();\n\n ans = add ? num1 + num2 : num1 - num2;\n }\n\n return ans;\n }\n};\n\n\n\n",
"memory": "24474"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculate(string s){\n s.push_back(' ');\n stack<int> st;\n int n = s.size();\n int currNum=-1;\n for(int i=0;i<n;i++){\n if((s[i]==' '|| s[i]=='+' || s[i]=='-' || s[i]=='*' || s[i]=='/') && st.size()>1 && currNum!=-1){\n if(st.top()==-1){\n st.pop();\n int num1=st.top();\n st.pop();\n st.push(num1*currNum);\n }else if(st.top()==-2){\n st.pop();\n int num1=st.top();\n st.pop();\n st.push(num1/currNum);\n }else if(currNum!=-1) st.push(currNum);\n currNum=-1;\n }\n if(s[i]=='*'){\n if(currNum!=-1) st.push(currNum);\n st.push(-1);\n currNum=-1;\n }else if(s[i]=='/'){\n if(currNum!=-1) st.push(currNum);\n st.push(-2);\n currNum=-1;\n }else if(s[i]=='+'){\n if(currNum!=-1) st.push(currNum);\n st.push(-3);\n currNum=-1;\n }else if(s[i]=='-'){\n if(currNum!=-1) st.push(currNum);\n st.push(-4);\n currNum=-1;\n }else if(s[i]>='0' && s[i]<='9'){\n if(currNum==-1){\n currNum = (s[i]-'0');\n }else{\n currNum*=10;\n currNum+=(s[i]-'0');\n }\n }\n }\n if(currNum!=-1) st.push(currNum);\n stack<int> tempStack;\n while (!st.empty()) {\n tempStack.push(st.top());\n st.pop();\n }\n st = tempStack;\n while(st.size()>2){\n int num2 = st.top();\n st.pop();\n int op = st.top();\n st.pop();\n int num1 = st.top();\n st.pop();\n if(op==-3){\n st.push(num1+num2);\n }else{\n st.push(num2-num1);\n }\n }\n return st.top();\n}\n};",
"memory": "24888"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\n enum tokenType{\n num,\n op,\n };\n struct Token {\n long num;\n char op;\n tokenType t;\n };\n\npublic:\n int calculate(string str) {\n vector<Token> v;\n long currNum = 0;\n int fact = 1;\n\n for(auto x : str){\n if(x == ' ') continue;\n\n if(x >= '0' && x <= '9'){\n currNum = 10*currNum + x - '0';\n continue;\n }\n\n if( x == '+'){\n v.push_back(Token{ .num = fact * currNum, .t = tokenType::num });\n fact = 1;\n currNum = 0;\n continue;\n }\n \n if( x == '-'){\n v.push_back(Token{ .num = fact * currNum, .t = tokenType::num });\n fact = -1;\n currNum = 0;\n continue;\n }\n\n if( x == '*'){\n v.push_back(Token{ .num = fact * currNum, .t = tokenType::num });\n v.push_back(Token{ .op = '*', .t = tokenType::op });\n fact = 1;\n currNum = 0;\n continue;\n }\n \n if( x == '/'){\n v.push_back(Token{ .num = fact * currNum, .t = tokenType::num });\n v.push_back(Token{ .op = '/', .t = tokenType::op });\n fact = 1;\n currNum = 0;\n continue;\n }\n }\n v.push_back(Token{ .num = fact * currNum, .t = tokenType::num });\n \n\n stack<long> st;\n int i = 0;\n while(i < v.size()){\n auto & x = v[i];\n if(x.t == tokenType::num){\n st.push(x.num);\n ++i;\n continue;\n }\n\n if(x.op == '*'){\n auto val = st.top()*v[i+1].num;\n st.pop();\n st.push(val);\n i += 2;\n continue;\n }\n \n if(x.op == '/'){\n auto val = st.top()/v[i+1].num;\n st.pop();\n st.push(val);\n i += 2;\n continue;\n }\n }\n\n int ret = 0;\n while(!st.empty()){\n ret += st.top(); st.pop();\n }\n\n return ret;\n }\n \n};",
"memory": "25301"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n int endofNextNumber(const string& s, int start) {\n int i = start;\n\n while (i < s.size() && (s[i] != '+' && s[i] != '-' && s[i] != '*' && s[i] != '/' )) {\n i++;\n }\n\n return i;\n }\n\npublic:\n int calculate(string s) {\n\n std::list<int> stack;\n\n /*\n construct stack of numbers where we can just add them together at the end\n */\n\n int num = 0;\n char operation = '+';\n for (int i = 0; i <= s.size(); i++) {\n if (i < s.size() && isdigit(s[i])) {\n num = num * 10 + (s[i] - '0');\n } else if (!iswspace(s[i]) || i == s.size()) {\n if (operation == '+') {\n stack.push_back(num);\n } else if (operation == '-') {\n stack.push_back(-1 * num);\n } else if (operation == '*') {\n // we need to fetch right hand side of the operation \n auto popped = stack.back();\n stack.pop_back();\n stack.push_back(popped * num);\n } else if (operation == '/') {\n auto popped = stack.back();\n stack.pop_back();\n stack.push_back(popped / num); \n }\n operation = s[i];\n num = 0;\n }\n\n }\n\n stack.push_back(num); // add last num if exists\n\n int result = 0;\n while (stack.size() != 0) {\n cout << stack.front() << endl;\n result += stack.front();\n stack.pop_front();\n }\n\n return result;\n }\n};",
"memory": "25715"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculate(string s) {\n\n std::list<int> stack;\n\n /*\n construct stack of numbers where we can just add them together at the end\n */\n\n int num = 0;\n char operation = '+';\n for (int i = 0; i <= s.size(); i++) {\n if (i < s.size() && isdigit(s[i])) {\n num = num * 10 + (s[i] - '0');\n } else if (!iswspace(s[i]) || i == s.size()) {\n if (operation == '+') {\n stack.push_back(num);\n } else if (operation == '-') {\n stack.push_back(-1 * num);\n } else if (operation == '*') {\n // we need to fetch right hand side of the operation \n auto popped = stack.back();\n stack.pop_back();\n stack.push_back(popped * num);\n } else if (operation == '/') {\n auto popped = stack.back();\n stack.pop_back();\n stack.push_back(popped / num); \n }\n operation = s[i];\n num = 0;\n }\n\n }\n\n stack.push_back(num); // add last num if exists\n\n int result = 0;\n while (stack.size() != 0) {\n cout << stack.front() << endl;\n result += stack.front();\n stack.pop_front();\n }\n\n return result;\n }\n};",
"memory": "26129"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "#include <iostream>\n#include <stack>\n#include <string>\n#include <climits>\n\nclass Solution {\npublic:\n std::string number(const std::string& s, int ind) {\n std::string no = \"\";\n for (int i = ind + 1; i < s.size(); i++) {\n if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/') {\n break;\n }\n no = no + s[i];\n }\n return no;\n }\n\n int calculate(std::string old) {\n std::string s = \"\";\n for (char c : old) {\n if (c != ' ') {\n s += c;\n }\n }\n\n std::stack<std::string> st;\n std::string no = \"\";\n int ind = -1;\n for (int i = 0; i < s.size(); i++) {\n if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/') {\n ind = i;\n break;\n }\n no = no + s[i];\n }\n st.push(no);\n int i = ind;\n while (i < s.size()) {\n if (s[i] == '+' || s[i] == '-') {\n std::string a = number(s, i);\n st.push(s[i] + a);\n i = i + a.size() + 1;\n } else {\n std::string a = number(s, i);\n std::string b = st.top();\n st.pop();\n long long res;\n if (s[i] == '/') {\n res = static_cast<long long>(std::stoi(b)) / std::stoi(a);\n } else {\n res = static_cast<long long>(std::stoi(b)) * std::stoi(a);\n }\n res = std::max(static_cast<long long>(INT_MIN), std::min(res, static_cast<long long>(INT_MAX)));\n st.push(std::to_string(static_cast<int>(res)));\n i = i + a.size() + 1;\n }\n }\n long long sum = 0;\n while (!st.empty()) {\n sum += std::stoi(st.top());\n st.pop();\n }\n sum = std::max(static_cast<long long>(INT_MIN), std::min(sum, static_cast<long long>(INT_MAX)));\n return static_cast<int>(sum);\n }\n};",
"memory": "26543"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n enum class op_t {\n ADD, SUB, MUL, DIV, VAL\n };\n\n struct ele_t {\n op_t op;\n int val;\n };\n\n int calculate(string s) {\n vector<ele_t> v;\n string curr;\n for (char c : s) {\n if (c == ' ') continue;\n\n if (isdigit(c)) {\n curr += c;\n } else {\n v.push_back((ele_t) { .op = op_t::VAL, .val = stoi(curr) });\n curr = \"\";\n switch (c) {\n case '+': \n v.push_back((ele_t) { .op = op_t::ADD, .val = 0 });\n break;\n case '-': \n v.push_back((ele_t) { .op = op_t::SUB, .val = 0 });\n break;\n case '*': \n v.push_back((ele_t) { .op = op_t::MUL, .val = 0 });\n break;\n default: \n v.push_back((ele_t) { .op = op_t::DIV, .val = 0 });\n break;\n }\n }\n }\n if (curr != \"\") {\n v.push_back((ele_t) { .op = op_t::VAL, .val = stoi(curr) });\n }\n\n vector<int> tmp = { v[0].val };\n int i = 1;\n while (i < v.size()) {\n op_t op = v[i].op;\n int right = v[i + 1].val;\n int left = tmp.back();\n switch (op) {\n case op_t::ADD:\n tmp.push_back(right);\n break;\n case op_t::SUB:\n tmp.push_back(-right);\n break;\n case op_t::MUL:\n tmp.pop_back();\n tmp.push_back(left * right);\n break;\n default:\n tmp.pop_back();\n tmp.push_back(left / right);\n break;\n }\n i += 2;\n }\n int ans = 0;\n for (auto v : tmp) ans += v;\n return ans;\n \n }\n};",
"memory": "26956"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n string remove_space(string s){\n string tmp = \"\";\n for(char c: s){\n if(c != ' ') tmp += c;\n }\n return tmp;\n }\n\n int calculate(string s) {\n \n stack<string> st;\n \n int i = 0;\n\n s = remove_space(s);\n int n = s.length();\n\n while(i < n){\n if(s[i] == '*' || s[i] == '/'){\n cout<<st.top()<<\"\\n\";\n int lhs = stoi(st.top());\n st.pop();\n int rhs = 0;\n string tmp = \"\";\n bool mult = (s[i] == '*')? true : false;\n i++;\n while(i < n && (s[i] >= '0' && s[i] <= '9')){\n tmp += s[i++];\n }\n rhs = stoi(tmp);\n int ans = 0;\n if (mult) ans = lhs * rhs; \n else ans = lhs/rhs;\n st.push(to_string(ans));\n }\n else if (s[i] == '+' || s[i] == '-'){\n // string a = \"\";\n // a += s[i];\n // st.push(a);\n bool add = (s[i] == '+')? true : false;\n i++;\n string tmp = \"\";\n while(i < n && (s[i] >= '0' && s[i] <= '9')){\n tmp += s[i++];\n }\n if(add) st.push(tmp);\n else{\n int num = -1 * stoi(tmp);\n st.push(to_string(num));\n }\n }\n else if(s[i] >= '0' && s[i] <= '9'){\n string tmp = \"\";\n while(i < n && (s[i] >= '0' && s[i] <= '9')){\n tmp += s[i++];\n }\n // int num = stoi(tmp);\n st.push(tmp);\n }\n }\n\n int ans = 0;\n bool add = false;\n bool start = true;\n while(!st.empty()){\n string top = st.top();\n ans += stoi(top);\n st.pop();\n }\n return ans;\n\n }\n};",
"memory": "26956"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "typedef struct {\n int value;\n char symbol;\n} Item;\n\nclass Solution {\npublic:\n int calculate(string s) {\n vector<Item> stack;\n int i = 0;\n while (i < s.size()) {\n if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/') {\n stack.push_back(Item{.symbol=s[i]});\n i ++;\n continue;\n }\n if (s[i] == ' ') {\n i += 1;\n continue;\n }\n int j = i;\n long v = 0;\n while ( j < s.size() && s[j] >= '0' && s[j] <= '9') {\n v = v * 10 + s[j] - '0';\n j++;\n }\n i = j;\n Item item;\n item.value = v;\n stack.push_back(item);\n }\n\n deque<Item> q;\n i = 0;\n while (i < stack.size()) {\n Item item = stack[i];\n if (item.symbol == '*' || item.symbol == '/') {\n Item pre = stack[i-1];\n Item next = stack[i+1];\n int v1 = q.back().value;\n int v2 = next.value;\n int res;\n if (item.symbol == '*') res = v1 * v2;\n else res = v1 / v2;\n Item n;\n n.value = res;\n q.pop_back();\n q.push_back(n);\n // cout <<\"push abck q: \" << n.value;\n i = i + 2;\n } else {\n q.push_back(item);\n i ++;\n }\n }\n int ans = 0;\n while (!q.empty()) {\n Item item = q.front();\n // cout <<\"item: \" << item.symbol << \"; \" << item.value << endl;\n q.pop_front();\n if (item.symbol == '+') {\n Item next = q.front();\n // cout <<\"next: \" << next.symbol << \"; \" << next.value << endl;\n q.pop_front();\n ans = ans + next.value;\n }\n else if (item.symbol == '-') {\n Item next = q.front();\n q.pop_front();\n ans = ans - next.value;\n } else {\n ans = item.value;\n }\n }\n return ans;\n }\n};",
"memory": "27370"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculate(string s) {\n int ans = 0;\n string num = \"\";\n char op;\n\n int pos1 = 0;\n int i = 0;\n while (i < s.length()) {\n char c = s[i];\n if (c == ' ') {i++; continue;}\n if (c == '+' || c == '-') { pos1 = i+1; i++;}\n else if (c == '*' || c == '/') {\n while (s[pos1] > '9' || s[pos1] < '0') pos1++;\n string num1 = s.substr(pos1, i - pos1);\n int j = i + 1;\n while (s[j] > '9' || s[j] < '0') j++;\n int pos2 = j;\n while (s[j] <= '9' && s[j] >= '0') j++;\n string num2 = s.substr(pos2, j - pos2);\n int res = 0;\n if (c == '*') res = stoi(num1) * stoi(num2);\n else res = stoi(num1) / stoi(num2);\n s = s.substr(0, pos1) + to_string(res) + s.substr(j, s.length() - j);\n i = pos1 + to_string(res).length();\n }\n else i++;\n }\n\n op = 'b';\n for (int i = 0; i < s.length(); i++) {\n char c = s[i];\n if (c == ' ') continue;\n if (c >= '0' && c <= '9') num += c;\n else {\n if (num == \"\") {\n op = c;\n continue;\n }\n if (op == '+' || op == 'b') ans += stoi(num);\n else if (op == '-') ans -= stoi(num);\n // else if (op == '*') ans *= stoi(num);\n // else if (op == '/') ans /= stoi(num);\n op = c;\n num = \"\";\n }\n }\n if (op == '+' || op == 'b') ans += stoi(num);\n else if (op == '-') ans -= stoi(num);\n return ans;\n }\n};",
"memory": "27784"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculate(string s) {\n for(int i = 0; i < s.size(); ++i){\n if(s[i] == '*'){\n string num1;\n string num2;\n int tmp1 = i-1;\n while(tmp1 >= 0 && s[tmp1] != '*' && s[tmp1] != '+' && s[tmp1] != '-' && s[tmp1] != '/'){\n num1.insert(num1.begin(), s[tmp1]);\n s.erase(s.begin()+tmp1);\n i--;\n tmp1--;\n }\n int tmp2 = i+1;\n while(tmp2 < s.size() && s[tmp2] != '*' && s[tmp2] != '+' && s[tmp2] != '-' && s[tmp2] != '/'){\n num2.push_back(s[tmp2]);\n s.erase(s.begin()+tmp2);\n }\n int prod = stoi(num1)*stoi(num2);\n string firstHalf = s.substr(0, i);\n string secondHalf = s.substr(i+1, s.size()-1);\n s = firstHalf + to_string(prod) + secondHalf;\n }else if(s[i] == '/'){\n string num1;\n string num2;\n int tmp1 = i-1;\n while(tmp1 >= 0 && s[tmp1] != '*' && s[tmp1] != '+' && s[tmp1] != '-' && s[tmp1] != '/'){\n num1.insert(num1.begin(), s[tmp1]);\n s.erase(s.begin()+tmp1);\n i--;\n tmp1--;\n }\n int tmp2 = i+1;\n while(tmp2 < s.size() && s[tmp2] != '*' && s[tmp2] != '+' && s[tmp2] != '-' && s[tmp2] != '/'){\n num2.push_back(s[tmp2]);\n s.erase(s.begin()+tmp2);\n }\n int prod = stoi(num1)/stoi(num2);\n string firstHalf = s.substr(0, i);\n string secondHalf = s.substr(i+1, s.size()-1);\n s = firstHalf + to_string(prod) + secondHalf;\n }\n }\n\n long long ans = 0;\n int sum = 1;\n int minus = 0;\n string tmp = \"\";\n for(int i = 0; i < s.size(); ++i){\n if(s[i] != '+' && s[i] != '-'){\n tmp += s[i];\n }\n if(s[i] == '+' && sum == 1){\n ans += stoi(tmp);\n tmp = \"\";\n sum = 1;\n minus = 0;\n }else if(s[i] == '+' && minus == 1){\n ans -= stoi(tmp);\n tmp = \"\";\n sum = 1;\n minus = 0;\n }else if(s[i] == '-' && sum == 1){\n ans += stoi(tmp);\n tmp = \"\";\n sum = 0;\n minus = 1;\n }else if(s[i] == '-' && minus == 1){\n ans -= stoi(tmp);\n tmp = \"\";\n sum = 0;\n minus = 1;\n }\n if(i == s.size()-1){\n if(sum == 1){\n ans += stoi(tmp);\n }else{\n ans -= stoi(tmp);\n }\n }\n }\n return ans;\n }\n};",
"memory": "28198"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculate(string s) {\n for(int i = 0; i < s.size(); ++i){\n if(s[i] == '*'){\n string num1;\n string num2;\n int tmp1 = i-1;\n while(tmp1 >= 0 && s[tmp1] != '*' && s[tmp1] != '+' && s[tmp1] != '-' && s[tmp1] != '/'){\n num1.insert(num1.begin(), s[tmp1]);\n s.erase(s.begin()+tmp1);\n i--;\n tmp1--;\n }\n int tmp2 = i+1;\n while(tmp2 < s.size() && s[tmp2] != '*' && s[tmp2] != '+' && s[tmp2] != '-' && s[tmp2] != '/'){\n num2.push_back(s[tmp2]);\n s.erase(s.begin()+tmp2);\n }\n int prod = stoi(num1)*stoi(num2);\n string firstHalf = s.substr(0, i);\n string secondHalf = s.substr(i+1, s.size()-1);\n s = firstHalf + to_string(prod) + secondHalf;\n }else if(s[i] == '/'){\n string num1;\n string num2;\n int tmp1 = i-1;\n while(tmp1 >= 0 && s[tmp1] != '*' && s[tmp1] != '+' && s[tmp1] != '-' && s[tmp1] != '/'){\n num1.insert(num1.begin(), s[tmp1]);\n s.erase(s.begin()+tmp1);\n i--;\n tmp1--;\n }\n int tmp2 = i+1;\n while(tmp2 < s.size() && s[tmp2] != '*' && s[tmp2] != '+' && s[tmp2] != '-' && s[tmp2] != '/'){\n num2.push_back(s[tmp2]);\n s.erase(s.begin()+tmp2);\n }\n int prod = stoi(num1)/stoi(num2);\n string firstHalf = s.substr(0, i);\n string secondHalf = s.substr(i+1, s.size()-1);\n s = firstHalf + to_string(prod) + secondHalf;\n }\n }\n\n long long ans = 0;\n int sum = 1;\n int minus = 0;\n string tmp = \"\";\n for(int i = 0; i < s.size(); ++i){\n if(s[i] != '+' && s[i] != '-'){\n tmp += s[i];\n }\n if(s[i] == '+' && sum == 1){\n ans += stoi(tmp);\n tmp = \"\";\n sum = 1;\n minus = 0;\n }else if(s[i] == '+' && minus == 1){\n ans -= stoi(tmp);\n tmp = \"\";\n sum = 1;\n minus = 0;\n }else if(s[i] == '-' && sum == 1){\n ans += stoi(tmp);\n tmp = \"\";\n sum = 0;\n minus = 1;\n }else if(s[i] == '-' && minus == 1){\n ans -= stoi(tmp);\n tmp = \"\";\n sum = 0;\n minus = 1;\n }\n if(i == s.size()-1){\n if(sum == 1){\n ans += stoi(tmp);\n }else{\n ans -= stoi(tmp);\n }\n }\n }\n return ans;\n }\n};",
"memory": "28611"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculate(string s) {\n for(int i = 0; i < s.size(); ++i){\n if(s[i] == '*'){\n string num1;\n string num2;\n int tmp1 = i-1;\n while(tmp1 >= 0 && s[tmp1] != '*' && s[tmp1] != '+' && s[tmp1] != '-' && s[tmp1] != '/'){\n num1.insert(num1.begin(), s[tmp1]);\n s.erase(s.begin()+tmp1);\n i--;\n tmp1--;\n }\n int tmp2 = i+1;\n while(tmp2 < s.size() && s[tmp2] != '*' && s[tmp2] != '+' && s[tmp2] != '-' && s[tmp2] != '/'){\n num2.push_back(s[tmp2]);\n s.erase(s.begin()+tmp2);\n }\n int prod = stoi(num1)*stoi(num2);\n string firstHalf = s.substr(0, i);\n string secondHalf = s.substr(i+1, s.size()-1);\n s = firstHalf + to_string(prod) + secondHalf;\n }else if(s[i] == '/'){\n string num1;\n string num2;\n int tmp1 = i-1;\n while(tmp1 >= 0 && s[tmp1] != '*' && s[tmp1] != '+' && s[tmp1] != '-' && s[tmp1] != '/'){\n num1.insert(num1.begin(), s[tmp1]);\n s.erase(s.begin()+tmp1);\n i--;\n tmp1--;\n }\n int tmp2 = i+1;\n while(tmp2 < s.size() && s[tmp2] != '*' && s[tmp2] != '+' && s[tmp2] != '-' && s[tmp2] != '/'){\n num2.push_back(s[tmp2]);\n s.erase(s.begin()+tmp2);\n }\n int prod = stoi(num1)/stoi(num2);\n string firstHalf = s.substr(0, i);\n string secondHalf = s.substr(i+1, s.size()-1);\n s = firstHalf + to_string(prod) + secondHalf;\n }\n }\n\n long long ans = 0;\n int sum = 1;\n int minus = 0;\n string tmp = \"\";\n for(int i = 0; i < s.size(); ++i){\n if(s[i] != '+' && s[i] != '-'){\n tmp += s[i];\n }\n if(s[i] == '+' && sum == 1){\n ans += stoi(tmp);\n tmp = \"\";\n sum = 1;\n minus = 0;\n }else if(s[i] == '+' && minus == 1){\n ans -= stoi(tmp);\n tmp = \"\";\n sum = 1;\n minus = 0;\n }else if(s[i] == '-' && sum == 1){\n ans += stoi(tmp);\n tmp = \"\";\n sum = 0;\n minus = 1;\n }else if(s[i] == '-' && minus == 1){\n ans -= stoi(tmp);\n tmp = \"\";\n sum = 0;\n minus = 1;\n }\n if(i == s.size()-1){\n if(sum == 1){\n ans += stoi(tmp);\n }else{\n ans -= stoi(tmp);\n }\n }\n }\n return ans;\n }\n};",
"memory": "28611"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculate(string s) {\n vector<string> t = toPostfix(s);\n stack<int> stk;\n\n for (const string x : t){\n if (x.empty()){\n break;\n }\n \n if (x[0] >= '0' && x[0] <= '9'){\n stk.emplace(toInt(x));\n }else{\n int a = stk.top();\n stk.pop();\n int b = stk.top();\n stk.pop();\n\n switch(x[0]){\n case '+':\n stk.emplace(b + a);\n break; \n case '-':\n stk.emplace(b - a);\n break;\n case '*':\n stk.emplace(b * a);\n break; \n case '/':\n stk.emplace(b / a);\n break; \n }\n }\n }\n \n return stk.top();\n\n }\nprivate:\n vector<string> toPostfix(string s){\n vector<string> t(s.size());\n int i = 0;\n stack<char> stk;\n map<char, int> priority = {{'+', 1}, {'-', 1},\n {'*', 2}, {'/', 2}};\n\n for (const char c : s){\n if (c >= '0' && c <= '9'){\n t[i] += c;\n }else if (c == '+' || c == '-' || c =='*' || c == '/'){\n i++;\n while(!stk.empty() && priority[stk.top()] >= priority[c]){\n t[i++] = stk.top();\n stk.pop();\n }\n stk.emplace(c);\n }\n }\n ++i;\n\n while(!stk.empty()){\n t[i++] = stk.top();\n stk.pop();\n }\n\n return t;\n }\n\n int toInt(string s){\n int x = 0;\n \n for (const char c : s){\n x = x * 10 - '0' + c;\n }\n\n return x;\n }\n};",
"memory": "29025"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculate(string s) {\n vector<string> t = toPostfix(s);\n stack<int> stk;\n\n for (const string x : t){\n if (x.empty()){\n break;\n }\n if (x[0] >= '0' && x[0] <= '9'){\n stk.emplace(toInt(x));\n }else{\n int a = stk.top();\n stk.pop();\n int b = stk.top();\n stk.pop();\n\n switch(x[0]){\n case '+':\n stk.emplace(b + a);\n break; \n case '-':\n stk.emplace(b - a);\n break;\n case '*':\n stk.emplace(b * a);\n break; \n case '/':\n stk.emplace(b / a);\n break; \n }\n }\n }\n \n return stk.top();\n\n }\nprivate:\n vector<string> toPostfix(string s){\n vector<string> t(s.size());\n int i = 0;\n stack<char> stk;\n map<char, int> priority = {{'+', 1}, {'-', 1},\n {'*', 2}, {'/', 2}};\n\n for (const char c : s){\n if (c >= '0' && c <= '9'){\n t[i] += c;\n }else if (c == '+' || c == '-' || c =='*' || c == '/'){\n i++;\n while(!stk.empty() && priority[stk.top()] >= priority[c]){\n t[i++] = stk.top();\n stk.pop();\n }\n stk.emplace(c);\n }\n }\n ++i;\n\n while(!stk.empty()){\n t[i++] = stk.top();\n stk.pop();\n }\n\n return t;\n }\n\n int toInt(string s){\n int x = 0;\n \n for (const char c : s){\n x = x * 10 - '0' + c;\n }\n\n return x;\n }\n};",
"memory": "29439"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculate(string s) {\n vector<string> t = toPostfix(s);\n stack<int> stk;\n\n for (const string x : t){\n if (x.empty()){\n break;\n }\n if (x[0] >= '0' && x[0] <= '9'){\n stk.emplace(toInt(x));\n }else{\n int a = stk.top();\n stk.pop();\n int b = stk.top();\n stk.pop();\n\n switch(x[0]){\n case '+':\n stk.emplace(b + a);\n break; \n case '-':\n stk.emplace(b - a);\n break;\n case '*':\n stk.emplace(b * a);\n break; \n case '/':\n stk.emplace(b / a);\n break; \n }\n }\n }\n \n return stk.top();\n\n }\nprivate:\n vector<string> toPostfix(string s){\n vector<string> t(s.size());\n int i = 0;\n stack<char> stk;\n map<char, int> priority = {{'+', 1}, {'-', 1},\n {'*', 2}, {'/', 2}};\n\n for (const char c : s){\n if (c >= '0' && c <= '9'){\n t[i] += c;\n }else if (c == '+' || c == '-' || c =='*' || c == '/'){\n i++;\n while(!stk.empty() && priority[stk.top()] >= priority[c]){\n t[i++] += stk.top();\n stk.pop();\n }\n stk.emplace(c);\n }\n }\n ++i;\n\n while(!stk.empty()){\n t[i++] += stk.top();\n stk.pop();\n }\n\n return t;\n }\n\n int toInt(string s){\n int x = 0;\n \n for (const char c : s){\n x = x * 10 - '0' + c;\n }\n\n return x;\n }\n};",
"memory": "29853"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n enum class token_type {\n NUM,\n OP\n };\n struct token {\n int num;\n char op;\n token_type type;\n\n token(int n) : num(n), op(0), type(token_type::NUM) {}\n token(char c) : num(0), op(c), type(token_type::OP) {}\n };\n\n int calculate(string s) {\n vector<token> tokens;\n string num;\n for(char c: s) {\n if (c == ' ') continue;\n if (isdigit(c)) num.push_back(c);\n else {\n if (!num.empty()) tokens.push_back(stoi(num));\n tokens.push_back(c);\n num.clear();\n }\n }\n if (!num.empty()) tokens.push_back(stoi(num));\n\n vector<int> nums;\n vector<char> ops;\n int i = 0;\n while(i < tokens.size()) {\n token t = tokens[i];\n if (t.type == token_type::NUM) nums.push_back(t.num);\n else if (t.op == '+' || t.op == '-') ops.push_back(t.op);\n else {\n int opnd1 = nums.back(); nums.pop_back();\n int opnd2 = tokens[i+1].num;\n if (t.op == '*') nums.push_back(opnd1 * opnd2);\n else nums.push_back(opnd1 / opnd2);\n i++;\n }\n i++;\n }\n int front = 0;\n for(char op: ops) {\n if (op == '+') nums[front+1] += nums[front];\n if (op == '-') nums[front+1] = nums[front] - nums[front+1];\n front++;\n }\n return nums[front];\n }\n};",
"memory": "30266"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "#include <cctype>\n#include <string>\nusing namespace std;\n\nenum class OperatorType {\n ADD,\n SUBTRACT,\n MULTIPLY,\n DIVIDE,\n INVALID\n};\nOperatorType getOperator(char c) {\n switch (c) {\n case '+':\n return OperatorType::ADD;\n case '-':\n return OperatorType::SUBTRACT;\n case '*':\n return OperatorType::MULTIPLY;\n case '/':\n return OperatorType::DIVIDE;\n default:\n cout << \"Invalid operator!\" << endl;\n return OperatorType::INVALID;\n }\n}\n\nstruct Node {\n int val; // Used for type=NUMBER\n OperatorType op; // Used for type=OPERATOR\n Node* next;\n Node(int x) : val(x), next(nullptr) {}\n Node(OperatorType x) : op(x), next(nullptr) {}\n Node(char x) : op(getOperator(x)), next(nullptr) {} // For convenience\n};\n\nclass Solution {\npublic:\n int calculate(string s) {\n string currInt = \"\";\n Node* head = nullptr;\n Node* curr = nullptr;\n for (char c : s) {\n if (isspace(c)) continue;\n if (isdigit(c)) {\n currInt += c;\n continue;\n }\n if (head == nullptr) {\n head = new Node(stoi(currInt));\n head->next = new Node(c);\n curr = head->next;\n currInt = \"\";\n continue;\n }\n curr->next = new Node(stoi(currInt));\n curr = curr->next;\n curr->next =new Node(c);\n curr = curr->next;\n currInt = \"\";\n }\n if (head == nullptr) return stoi(s);\n curr->next = new Node(stoi(currInt));\n curr = curr->next;\n\n // Multiply and divide first\n Node* p1 = head;\n Node* p2 = head->next;\n Node* p3 = head->next->next;\n while (p3 != nullptr) {\n if (p2->op == OperatorType::ADD || p2->op == OperatorType::SUBTRACT) {\n p1 = p3;\n } else if (p2->op == OperatorType::MULTIPLY) {\n p1->val = p1->val * p3->val;\n p1->next = p3->next;\n delete p2;\n delete p3;\n } else if (p2->op == OperatorType::DIVIDE) {\n p1->val = (int) p1->val / p3->val;\n p1->next = p3->next;\n delete p2;\n delete p3;\n } else {\n cout << \"Invalid operation!\" << endl;\n break;\n }\n if (p1->next == nullptr) break;\n p2 = p1->next;\n p3 = p1->next->next;\n }\n if (head->next == nullptr) return head->val;\n\n // Add and subtract\n p1 = head, p2 = head->next, p3 = head->next->next;\n while (p3 != nullptr) {\n if (p2->op == OperatorType::ADD) {\n p1->val = p1->val + p3->val;\n p1->next = p3->next;\n delete p2;\n delete p3;\n } else if (p2->op == OperatorType::SUBTRACT) {\n p1->val = p1->val - p3->val;\n p1->next = p3->next;\n delete p2;\n delete p3;\n } else {\n cout << \"Invalid operation.\" << endl;\n break;\n }\n if (p1->next == nullptr) break;\n p2 = p1->next;\n p3 = p1->next->next;\n }\n return head->val;\n }\n};",
"memory": "30680"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "struct Op {\n Op(int n, int o): num(n), op(o) {} \n int num;\n int op; // 0: num, 1: +, 2: -, 3: *, 4: /\n};\n\nclass Solution {\npublic:\n int calculate(string s) {\n vector<Op> seq;\n \n for (char c : s) {\n if (c == '*') {\n seq.emplace_back(0, 3);\n } else if (c == '/') {\n seq.emplace_back(0, 4);\n } else if (c == '+') {\n seq.emplace_back(0, 1);\n } else if (c == '-') {\n seq.emplace_back(0, 2);\n } else if (c <= '9' && c >= '0') {\n if (seq.empty() || seq.back().op != 0) {\n seq.emplace_back(c - '0', 0);\n } else {\n seq.back().num = seq.back().num * 10 + (c - '0');\n }\n }\n }\n\n\n int lastop = 5;\n vector<Op> processed;\n for (const Op& op : seq) {\n if (op.op == 0) {\n if (lastop < 5) {\n Op left = processed.back();\n processed.pop_back();\n if (lastop == 3) {\n processed.emplace_back(left.num * op.num, 0);\n } else {\n processed.emplace_back(left.num / op.num, 0);\n }\n lastop = 5;\n } else {\n processed.push_back(op);\n }\n } else if (op.op <= 2) {\n processed.push_back(op);\n } else {\n lastop = op.op;\n }\n }\n\n int num = 0;\n\n lastop = 1;\n for (int i = 0; i < processed.size(); ++i) {\n if (processed[i].op > 0) {\n lastop = processed[i].op;\n } else {\n if (lastop == 1) {\n num += processed[i].num;\n } else {\n num -= processed[i].num;\n }\n }\n }\n return num;\n }\n};",
"memory": "31094"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n enum class op_t {\n ADD, SUB, MUL, DIV, VAL\n };\n\n struct ele_t {\n op_t op;\n int val;\n };\n\n int calculate(string s) {\n vector<ele_t> v;\n string curr;\n for (char c : s) {\n if (c == ' ') continue;\n\n if (isdigit(c)) {\n curr += c;\n } else {\n v.push_back((ele_t) { .op = op_t::VAL, .val = stoi(curr) });\n curr = \"\";\n switch (c) {\n case '+': \n v.push_back((ele_t) { .op = op_t::ADD, .val = 0 });\n break;\n case '-': \n v.push_back((ele_t) { .op = op_t::SUB, .val = 0 });\n break;\n case '*': \n v.push_back((ele_t) { .op = op_t::MUL, .val = 0 });\n break;\n default: \n v.push_back((ele_t) { .op = op_t::DIV, .val = 0 });\n break;\n }\n }\n }\n if (curr != \"\") {\n v.push_back((ele_t) { .op = op_t::VAL, .val = stoi(curr) });\n }\n\n vector<ele_t> v1;\n int i = 0;\n while (i < v.size()) {\n if (v[i].op != op_t::MUL && v[i].op != op_t::DIV) {\n v1.push_back(v[i]);\n ++i;\n } else if (v[i].op == op_t::MUL) {\n int result = v1.back().val * v[i + 1].val;\n v1.pop_back();\n v1.push_back((ele_t) { .op = op_t::VAL, .val = result });\n i += 2;\n } else {\n int result = v1.back().val / v[i + 1].val;\n v1.pop_back();\n v1.push_back((ele_t) { .op = op_t::VAL, .val = result });\n i += 2;\n }\n }\n\n if (v1.size() == 1) return v1[0].val;\n\n int ans = v1[0].val;\n for (int i = 1; i + 1 < v1.size(); i += 2) {\n int num1 = v1[i + 1].val;\n if (v1[i].op == op_t::ADD) {\n ans += num1;\n } else {\n ans -= num1;\n }\n }\n return ans;\n \n }\n};",
"memory": "31508"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "int stoi(string &s) {\n int ret = 0;\n for(auto i: s) {\n ret = ret*10 + (i-'0');\n }\n return ret;\n}\n\nclass Solution {\npublic:\n int calculate(string s) {\n string p;\n deque<string> st;\n for(auto i: s) {\n if(i>='0' && i<='9') p.push_back(i);\n else if(i=='+' || i=='-' || i=='*' || i=='/') {\n if(st.size() && (st.back()==\"*\" || st.back()==\"/\")) {\n string temp = st.back();\n st.pop_back();\n string val = st.back();\n st.pop_back();\n if(temp == \"*\") st.push_back(to_string(stoi(val) * stoi(p)));\n else st.push_back(to_string(stoi(val) / stoi(p)));\n }\n else {\n st.push_back(p);\n }\n p=\"\";\n p.push_back(i);\n st.push_back(p);\n p=\"\";\n }\n }\n if(st.size() && (st.back()==\"*\" || st.back()==\"/\")) {\n string temp = st.back();\n st.pop_back();\n string val = st.back();\n st.pop_back();\n if(temp == \"*\") st.push_back(to_string(stoi(val) * stoi(p)));\n else st.push_back(to_string(stoi(val) / stoi(p)));\n }\n else st.push_back(p);\n int val = stoi(st.front());\n st.pop_front();\n while(st.size()) {\n string op = st.front();\n st.pop_front();\n int v = stoi(st.front());\n st.pop_front();\n if(op==\"+\") val += v;\n else if(op==\"-\") val -= v;\n else if(op==\"*\") val*=v;\n else val /= v;\n }\n return val;\n }\n};",
"memory": "31921"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\n\n std::vector<variant<int, char>> parseString(string s) {\n int n = s.size();\n \n std:string number_string;\n std::vector<variant<int, char>> number_and_symbols;\n \n for (int i{0}; i < n; ++i) {\n if (s[i] == ' ') continue;\n char c_shifted = s[i] - '0';\n if (c_shifted >= 0 && c_shifted < 10) {\n number_string += s[i];\n continue;\n }\n // At this point this is a symbol check if we need to add a number\n if (number_string.size() > 0) {\n number_and_symbols.push_back(stoi(number_string));\n number_string.clear();\n } \n number_and_symbols.push_back(s[i]);\n }\n\n if (number_string.size() > 0) {\n number_and_symbols.push_back(stoi(number_string));\n number_string.clear();\n } \n return number_and_symbols;\n }\n \n void print_vector(const vector<variant<int, char>>& values) {\n for (int i{0}; i < values.size(); ++i) {\n if (holds_alternative<int>(values[i])) {\n std::cout << get<int>(values[i]) << \" \";\n }\n if (holds_alternative<char>(values[i])) {\n std::cout << get<char>(values[i]) << \" \";\n }\n }\n std::cout << std::endl;\n }\n \n int do_operation(int lhs, int rhs, char operation) {\n switch (operation) {\n case '*':\n return lhs * rhs;\n case '-':\n return lhs - rhs;\n case '/':\n return lhs / rhs;\n case '+':\n return lhs + rhs;\n }\n return 0;\n }\n\n std::vector<variant<int, char>> reduce(const std::vector<variant<int, char>>& values, char operand1, char operand2) {\n // If it is a number then add parse \n std::vector<variant<int, char>> results;\n optional<char> last_symbol;\n for (int i{0}; i < values.size(); ++i) {\n if (holds_alternative<int>(values[i])) {\n int n = get<int>(values[i]);\n if (last_symbol.has_value() && last_symbol.value() == operand1) {\n int last_number = get<int>(results.back());\n results.back() = do_operation(last_number, n, operand1);\n last_symbol.reset();\n continue;\n }\n if (last_symbol.has_value() && last_symbol.value() == operand2) {\n int last_number = get<int>(results.back());\n results.back() = do_operation(last_number, n, operand2);\n last_symbol.reset();\n continue;\n } \n \n // If last symbol is empty just add the value to results\n results.push_back(n);\n } else {\n // this is a symbol so either add it to last symbol or add it to results\n char c = get<char>(values[i]);\n if (c == operand1 || c == operand2) {\n last_symbol = c;\n } else {\n results.push_back(c);\n }\n }\n }\n return results;\n }\n \npublic:\n int calculate(string s) {\n int n = s.size();\n \n const auto values = parseString(s);\n // print_vector(values);\n const auto results1 = reduce(values, '*', '/');\n const auto results2 = reduce(results1, '+', '-');\n // print_vector(results1);\n // print_vector(results2);\n return get<int>(results2.back());\n }\n \n // traverse:\n // skip if empty\n // if number add to number stack\n // if symbol then grab previous number\n // we can build a tree of expressions and make sure that we do multiplications and divisions first\n // after we do all multiplications and divitions then lets do sums and substractions\n \n};",
"memory": "32335"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n // order of operation + 3 * 2 2\n int calculate(string s) \n {\n string current_number =\"\";\n queue<string> operations;\n\n for(int i =0; i < s.length() ; i++)\n {\n if(s[i]==' ')\n continue;\n if(s[i] >= '0' && s[i] <= '9')\n {\n current_number.push_back(s[i]);\n }\n else if(s[i]== '*' || s[i] == '/')\n {\n string next_number = \"\";\n int count=i+1;\n while((s[count] >='0' && s[count]<='9' )|| s[count]==' ')\n {\n if(s[count] != ' ')\n next_number.push_back(s[count]);\n count++;\n }\n\n string number;\n if(s[i] =='*')\n number = to_string(stoi(current_number) * stoi(next_number));\n else\n number = to_string(stoi(current_number) / stoi(next_number));\n\n i = count-1;\n current_number = number;\n next_number=\"\";\n }\n else if(s[i] == '+'|| s[i] == '-' )\n {\n operations.push(current_number);\n if(s[i]=='+')\n operations.push(\"+\");\n else\n operations.push(\"-\");\n current_number=\"\";\n }\n }\n\n if(current_number!=\"\")\n operations.push(current_number);\n\n int result = stoi(operations.front());\n operations.pop();\n while(!operations.empty())\n {\n string oper = operations.front();\n operations.pop();\n\n int second = stoi(operations.front());\n operations.pop();\n\n if(oper==\"+\")\n result+= second;\n else\n result-= second;\n }\n return result;\n }\n};",
"memory": "32749"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n // order of operation + 3 * 2 2\n int calculate(string s) \n {\n string current_number =\"\";\n queue<string> operations;\n\n for(int i =0; i < s.length() ; i++)\n {\n if(s[i]==' ')\n continue;\n if(s[i] >= '0' && s[i] <= '9')\n {\n current_number.push_back(s[i]);\n }\n else if(s[i]== '*' || s[i] == '/')\n {\n string next_number = \"\";\n int count=i+1;\n while((s[count] >='0' && s[count]<='9' )|| s[count]==' ')\n {\n if(s[count] != ' ')\n next_number.push_back(s[count]);\n count++;\n }\n\n string number;\n if(s[i] =='*')\n number = to_string(stoi(current_number) * stoi(next_number));\n else\n number = to_string(stoi(current_number) / stoi(next_number));\n\n i = count-1;\n current_number = number;\n next_number=\"\";\n }\n else if(s[i] == '+'|| s[i] == '-' )\n {\n operations.push(current_number);\n if(s[i]=='+')\n operations.push(\"+\");\n else\n operations.push(\"-\");\n current_number=\"\";\n }\n }\n\n if(current_number!=\"\")\n operations.push(current_number);\n\n int result = stoll(operations.front());\n operations.pop();\n while(!operations.empty())\n {\n string oper = operations.front();\n operations.pop();\n\n int second = stoll(operations.front());\n operations.pop();\n\n if(oper==\"+\")\n result+= second;\n else\n result-= second;\n }\n return result;\n }\n};",
"memory": "33163"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "#define ADD \"+\"\n#define SUB \"-\"\n#define MUL \"*\"\n#define DIV \"/\"\nclass Solution {\npublic:\n int calculate(string A) {\n stack<string> S;\n int it = 0;\n int n = A.size();\n bool neg = false;\n while (it < n) {\n string cur(1, A[it]); \n if (cur == \" \") {\n it++;\n continue;\n }\n if (cur == ADD || cur == SUB || cur == MUL || cur == DIV) {\n assert(!S.empty());\n if (cur == SUB) {\n cur = ADD;\n neg = true;\n } \n S.push(cur);\n it++;\n continue;\n } else {\n string op = \"\";\n while (it < n && A[it] >= '0' && A[it] <= '9') {\n op += A[it];\n it++;\n }\n // cout << it;\n if (op != \"\") {\n if (!S.empty() && (S.top() == MUL || S.top() == DIV)) {\n int op1 = stoi(op);\n string oper = S.top();\n S.pop();\n int op2 = stoi(S.top());\n S.pop();\n int res = oper == DIV ? op2/op1 : op1*op2; \n S.push(to_string(res));\n } else {\n string tmp;\n if (neg) {\n tmp = \"-\";\n tmp += op;\n }\n neg ? S.push(tmp) : S.push(op);\n neg = false;\n }\n }\n }\n }\n while (S.size()>1) {\n int res = 0;\n int op1 = stoi(S.top());\n cout << \"op1: \" << op1; \n S.pop();\n string op = S.top();\n cout << \", op: \" << op;\n S.pop();\n int op2 = stoi(S.top());\n cout << \", op2: \" << op2; \n S.pop();\n res = op1+op2;\n S.push(to_string(res));\n }\n // cout << S.top();\n return stoi(S.top());\n }\n};",
"memory": "36059"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool check(string i) {\n if(i != \"+\" && i != \"-\" && i != \"*\" && i != \"/\") return true;\n return false;\n }\n void resolve(deque<string>& st) {\n string t = st.back();\n st.pop_back();\n string op = st.back();\n st.pop_back();\n string prev = st.back();\n st.pop_back();\n if(op == \"*\") {\n st.push_back(to_string(stoi(prev) * stoi(t)));\n }\n else if(op == \"/\") {\n st.push_back(to_string(stoi(prev) / stoi(t)));\n }\n else{\n st.push_back(prev); st.push_back(op); st.push_back(t);\n }\n }\n int calculate(string s) {\n deque<string> st;\n bool fin = false;\n for(auto &ch:s) {\n string i = \"\";\n i += ch;\n cout<<i<<endl;\n if(i == \" \") continue;\n if(st.empty()){\n st.push_back(i);\n continue;\n }\n if(!check(i)) {\n if(fin) {\n resolve(st);\n fin = false;\n }\n if(i == \"*\" || i == \"/\") fin = true;\n st.push_back(i);\n }\n else {\n string t = st.back();\n if(check(t)) {\n st.pop_back();\n st.push_back(to_string(stoi(t) * 10 + stoi(i)));\n }\n else st.push_back(i);\n }\n \n }\n if(st.size() >= 3) resolve(st);\n int ans = stoi(st.front());\n cout<<ans<<endl;\n string op;\n st.pop_front();\n while(!st.empty()) {\n cout<<st.front()<<endl;\n if(!check(st.front())) op = st.front();\n else{\n if(op == \"+\") ans += stoi(st.front());\n else if(op == \"-\") ans -= stoi(st.front());\n \n }\n st.pop_front();\n }\n return ans;\n }\n};",
"memory": "36473"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool check(string i) {\n if(i != \"+\" && i != \"-\" && i != \"*\" && i != \"/\") return true;\n return false;\n }\n void resolve(deque<string>& st) {\n string t = st.back();\n st.pop_back();\n string op = st.back();\n st.pop_back();\n string prev = st.back();\n st.pop_back();\n if(op == \"*\") {\n st.push_back(to_string(stoi(prev) * stoi(t)));\n }\n else if(op == \"/\") {\n st.push_back(to_string(stoi(prev) / stoi(t)));\n }\n else{\n st.push_back(prev); st.push_back(op); st.push_back(t);\n }\n }\n int calculate(string s) {\n deque<string> st;\n bool fin = false;\n for(auto &ch:s) {\n string i = \"\";\n i += ch;\n // cout<<i<<endl;\n if(i == \" \") continue;\n if(st.empty()){\n st.push_back(i);\n continue;\n }\n if(!check(i)) {\n if(fin) {\n resolve(st);\n fin = false;\n }\n if(i == \"*\" || i == \"/\") fin = true;\n st.push_back(i);\n }\n else {\n string t = st.back();\n if(check(t)) {\n st.pop_back();\n st.push_back(to_string(stoi(t) * 10 + stoi(i)));\n }\n else st.push_back(i);\n }\n \n }\n if(st.size() >= 3) resolve(st);\n int ans = stoi(st.front());\n // cout<<ans<<endl;\n string op;\n st.pop_front();\n while(!st.empty()) {\n // cout<<st.front()<<endl;\n if(!check(st.front())) op = st.front();\n else{\n if(op == \"+\") ans += stoi(st.front());\n else if(op == \"-\") ans -= stoi(st.front());\n \n }\n st.pop_front();\n }\n return ans;\n }\n};",
"memory": "36473"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool check(string i) {\n if(i != \"+\" && i != \"-\" && i != \"*\" && i != \"/\") return true;\n return false;\n }\n void resolve(deque<string>& st) {\n string t = st.back();\n st.pop_back();\n string op = st.back();\n st.pop_back();\n string prev = st.back();\n st.pop_back();\n if(op == \"*\") {\n st.push_back(to_string(stoi(prev) * stoi(t)));\n }\n else if(op == \"/\") {\n st.push_back(to_string(stoi(prev) / stoi(t)));\n }\n else{\n st.push_back(prev); st.push_back(op); st.push_back(t);\n }\n }\n int calculate(string s) {\n deque<string> st;\n bool fin = false;\n for(auto &ch:s) {\n string i = \"\";\n i += ch;\n // cout<<i<<endl;\n if(i == \" \") continue;\n if(st.empty()){\n st.push_back(i);\n continue;\n }\n if(!check(i)) {\n if(fin) {\n resolve(st);\n fin = false;\n }\n if(i == \"*\" || i == \"/\") fin = true;\n st.push_back(i);\n }\n else {\n string t = st.back();\n if(check(t)) {\n st.pop_back();\n st.push_back(to_string(stoi(t) * 10 + stoi(i)));\n }\n else st.push_back(i);\n }\n \n }\n if(st.size() >= 3) resolve(st);\n int ans = stoi(st.front());\n // cout<<ans<<endl;\n string op;\n st.pop_front();\n while(!st.empty()) {\n // cout<<st.front()<<endl;\n if(!check(st.front())) op = st.front();\n else{\n if(op == \"+\") ans += stoi(st.front());\n else if(op == \"-\") ans -= stoi(st.front());\n \n }\n st.pop_front();\n }\n return ans;\n }\n};",
"memory": "36886"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculate(string s) {\n string str = \"\";\n for(auto it: s) {\n if(it != ' ') str += it;\n }\n str += '/';\n deque<string> dq;\n int start = 0;\n string currOp = \" \";\n\n for(int i=0; i<str.size(); i++) {\n char it = str[i];\n if(it == '+' || it == '-' || it == '*' || it == '/') {\n string sub = str.substr(start, i - start);\n dq.push_back(sub);\n if(currOp == \"*\" || currOp == \"/\") {\n int n2 = stoi(dq.back());\n dq.pop_back();\n dq.pop_back();\n int n1 = stoi(dq.back());\n dq.pop_back();\n if(currOp == \"*\") dq.push_back(to_string((int)(n1 * n2)));\n else dq.push_back(to_string((int)(n1 / n2)));\n }\n currOp.assign(1, it);\n start = i+1;\n dq.push_back(currOp);\n }\n }\n dq.pop_back();\n\n while(dq.size() != 1) {\n int n1 = stoi(dq.front());\n dq.pop_front();\n string op = dq.front();\n dq.pop_front();\n int n2 = stoi(dq.front());\n dq.pop_front();\n if(op == \"+\") dq.push_front(to_string(n1+n2));\n else dq.push_front(to_string(n1-n2));\n }\n\n return stoi(dq.front());\n }\n};",
"memory": "37300"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Expression {\npublic:\n virtual ~Expression() {};\n virtual long long Operate(long long left = 0, long long right = 0) {\n return 0;\n };\n virtual bool IsBinaryOperator() {\n return false;\n };\n virtual char Name() {\n return ' ';\n };\n};\n\nclass Number : public Expression {\npublic: \n Number(long long num) : num(num) {\n }\n\n ~Number() override {\n }\n\n bool IsBinaryOperator() override {\n return false;\n }\n\n long long Operate(long long left = 0, long long right = 0) override {\n return num;\n }\n\n char Name() override {\n return '_';\n }\nprivate:\n long long num;\n};\n\nclass Operand : public Expression {\npublic:\n Operand(char operation) : operation(operation) {\n }\n\n ~Operand() override {\n };\n\n bool IsBinaryOperator() override {\n return true;\n }\n\n long long Operate(long long left = 0, long long right = 0) override {\n switch (operation)\n {\n case '-':\n return left - right;\n break;\n case '+':\n return left + right;\n break;\n case '*':\n return left * right;\n break;\n case '/':\n return left / right;\n break;\n default:\n return 0;\n break;\n }\n }\n\n char Name() override {\n return operation;\n }\nprivate:\n char operation;\n};\n\n\nclass Solution {\npublic:\n \n int calculate(string s) {\n int idx = 0;\n int n = s.size();\n\n std::vector<Expression*> vec;\n\n while (idx < n) {\n if (s[idx] == ' ') {\n ++idx;\n continue;\n }\n if (!isdigit(s[idx])) {\n vec.push_back(new Operand(s[idx]));\n ++idx;\n continue;\n }\n\n long long curr = 0;\n while (idx < n && isdigit(s[idx])) {\n curr = curr * 10 + (s[idx] - '0');\n ++idx;\n }\n\n if (!vec.empty() && vec.back()->IsBinaryOperator() && (vec.back()->Name() == '*' || vec.back()->Name() == '/')) {\n int size = vec.size() - 1;\n curr = vec.back()->Operate(vec[size - 1]->Operate(), curr);\n vec.pop_back();\n vec.pop_back();\n }\n\n vec.push_back(new Number(curr));\n }\n\n long long res = vec[0]->Operate();\n for (int i = 1; i < vec.size(); i += 2) {\n res = vec[i]->Operate(res, vec[i + 1]->Operate());\n }\n return res;\n }\n};",
"memory": "37714"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n class stack_entry {\n public:\n int val;\n enum op {\n NONE,\n ADD,\n SUB,\n MUL,\n DIV\n } op;\n\n stack_entry(int v, enum op operation) {\n val = v;\n op = operation;\n }\n };\n\n int _calculate(int opnd1, enum stack_entry::op op, int opnd2) {\n int res;\n\n switch (op) {\n case stack_entry::ADD:\n res = opnd1 + opnd2;\n break;\n case stack_entry::SUB:\n res = opnd1 - opnd2;\n break;\n case stack_entry::MUL:\n res = opnd1 * opnd2;\n break;\n case stack_entry::DIV:\n res = opnd1 / opnd2;\n break;\n default:\n throw exception();\n }\n\n return res;\n }\n\npublic:\n int calculate(string s) {\n int i, j, n;\n int res;\n stack_entry *cur, *next;\n stack_entry *op;\n stack_entry *opnd1, *opnd2;\n vector<stack_entry *> stk;\n\n static const enum stack_entry::op tbl[] = {\n [(unsigned char)'+'] = stack_entry::ADD,\n [(unsigned char)'-'] = stack_entry::SUB,\n [(unsigned char)'*'] = stack_entry::MUL,\n [(unsigned char)'/'] = stack_entry::DIV\n };\n\n n = s.length();\n\n op = opnd1 = nullptr;\n i = 0;\n for (;;) {\n if (i == n)\n break;\n if (s[i] == ' ') {\n ++i;\n continue;\n }\n if (isdigit(s[i])) {\n int val;\n\n for (j = i + 1; j < n && isdigit(s[j]); j++)\n ;\n val = stoi(string(begin(s) + i, begin(s) + j), nullptr, 10);\n cur = new stack_entry(val, stack_entry::NONE);\n stk.push_back(cur);\n if (op == nullptr || (op->op != stack_entry::MUL && op->op != stack_entry::DIV))\n opnd1 = cur;\n else {\n opnd2 = cur;\n cur = new stack_entry(_calculate(opnd1->val, op->op, opnd2->val), stack_entry::NONE);\n delete opnd1;\n delete op;\n delete opnd2;\n stk.resize(stk.size() - 3);\n stk.push_back(cur);\n opnd1 = cur;\n }\n i = j;\n } else {\n op = new stack_entry(-1, tbl[(unsigned char)s[i]]);\n stk.push_back(op);\n ++i;\n }\n }\n\n n = stk.size();\n\n op = nullptr;\n res = stk[0]->val;\n for (i = 1; i < n; i++) {\n cur = stk[i];\n if (cur->op != stack_entry::NONE)\n op = cur;\n else {\n opnd2 = cur;\n res = _calculate(res, op->op, opnd2->val);\n }\n }\n\n return res;\n }\n};",
"memory": "38128"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n class stack_entry {\n public:\n int val;\n enum op {\n NONE,\n ADD,\n SUB,\n MUL,\n DIV\n } op;\n\n stack_entry(int v, enum op operation) {\n val = v;\n op = operation;\n }\n };\n\n int _calculate(int opnd1, enum stack_entry::op op, int opnd2) {\n int res;\n\n switch (op) {\n case stack_entry::ADD:\n res = opnd1 + opnd2;\n break;\n case stack_entry::SUB:\n res = opnd1 - opnd2;\n break;\n case stack_entry::MUL:\n res = opnd1 * opnd2;\n break;\n case stack_entry::DIV:\n res = opnd1 / opnd2;\n break;\n default:\n throw exception();\n }\n\n return res;\n }\n\npublic:\n int calculate(string s) {\n int i, j, n;\n int res;\n stack_entry *cur, *next;\n stack_entry *op;\n stack_entry *opnd1, *opnd2;\n vector<stack_entry *> stk;\n\n static const enum stack_entry::op tbl[] = {\n [(unsigned char)'+'] = stack_entry::ADD,\n [(unsigned char)'-'] = stack_entry::SUB,\n [(unsigned char)'*'] = stack_entry::MUL,\n [(unsigned char)'/'] = stack_entry::DIV\n };\n\n n = s.length();\n\n op = opnd1 = nullptr;\n i = 0;\n for (;;) {\n if (i == n)\n break;\n if (s[i] == ' ') {\n ++i;\n continue;\n }\n if (isdigit(s[i])) {\n int val;\n\n for (j = i + 1; j < n && isdigit(s[j]); j++)\n ;\n val = stoi(string(begin(s) + i, begin(s) + j), nullptr, 10);\n cur = new stack_entry(val, stack_entry::NONE);\n stk.push_back(cur);\n if (op == nullptr || (op->op != stack_entry::MUL && op->op != stack_entry::DIV))\n opnd1 = cur;\n else {\n opnd2 = cur;\n cur = new stack_entry(_calculate(opnd1->val, op->op, opnd2->val), stack_entry::NONE);\n delete opnd1;\n delete op;\n delete opnd2;\n stk.resize(stk.size() - 3);\n stk.push_back(cur);\n opnd1 = cur;\n }\n i = j;\n } else {\n op = new stack_entry(-1, tbl[(unsigned char)s[i]]);\n stk.push_back(op);\n ++i;\n }\n }\n\n n = stk.size();\n\n op = nullptr;\n res = stk[0]->val;\n for (i = 1; i < n; i++) {\n cur = stk[i];\n if (cur->op != stack_entry::NONE)\n op = cur;\n else {\n opnd2 = cur;\n res = _calculate(res, op->op, opnd2->val);\n }\n }\n\n return res;\n }\n};",
"memory": "38541"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n class stack_entry {\n public:\n int val;\n enum op {\n NONE,\n ADD,\n SUB,\n MUL,\n DIV\n } op;\n\n stack_entry(int v, enum op operation) {\n val = v;\n op = operation;\n }\n };\n\n int _calculate(int opnd1, enum stack_entry::op op, int opnd2) {\n int res;\n\n switch (op) {\n case stack_entry::ADD:\n res = opnd1 + opnd2;\n break;\n case stack_entry::SUB:\n res = opnd1 - opnd2;\n break;\n case stack_entry::MUL:\n res = opnd1 * opnd2;\n break;\n case stack_entry::DIV:\n res = opnd1 / opnd2;\n break;\n default:\n throw exception();\n }\n\n return res;\n }\n\npublic:\n int calculate(string s) {\n int i, j, n;\n int res;\n stack_entry *cur, *next;\n stack_entry *op;\n stack_entry *opnd1, *opnd2;\n vector<stack_entry *> stk;\n\n static const enum stack_entry::op tbl[] = {\n [(unsigned char)'+'] = stack_entry::ADD,\n [(unsigned char)'-'] = stack_entry::SUB,\n [(unsigned char)'*'] = stack_entry::MUL,\n [(unsigned char)'/'] = stack_entry::DIV\n };\n\n n = s.length();\n\n op = opnd1 = nullptr;\n i = 0;\n for (;;) {\n if (i == n)\n break;\n if (s[i] == ' ') {\n ++i;\n continue;\n }\n if (isdigit(s[i])) {\n int val;\n\n for (j = i + 1; j < n && isdigit(s[j]); j++)\n ;\n val = stoi(string(begin(s) + i, begin(s) + j), nullptr, 10);\n cur = new stack_entry(val, stack_entry::NONE);\n stk.push_back(cur);\n if (op == nullptr || (op->op != stack_entry::MUL && op->op != stack_entry::DIV))\n opnd1 = cur;\n else {\n opnd2 = cur;\n cur = new stack_entry(_calculate(opnd1->val, op->op, opnd2->val), stack_entry::NONE);\n delete opnd1;\n delete op;\n delete opnd2;\n stk.resize(stk.size() - 3);\n stk.push_back(cur);\n opnd1 = cur;\n }\n i = j;\n } else {\n op = new stack_entry(-1, tbl[(unsigned char)s[i]]);\n stk.push_back(op);\n ++i;\n }\n }\n for (i = 0; i < stk.size(); i++) {\n if (stk[i]->op == stack_entry::NONE)\n cout << \"Value \" << stk[i]->val << endl;\n else\n cout << \"Operation \" << stk[i]->op << endl;\n }\n\n n = stk.size();\n\n op = nullptr;\n res = stk[0]->val;\n for (i = 1; i < n; i++) {\n cur = stk[i];\n if (cur->op != stack_entry::NONE) {\n cout << \"Operation = \" << cur->op << endl;\n op = cur;\n } else {\n opnd2 = cur;\n cout << \"Operand 2 = \" << cur->val << endl;\n res = _calculate(res, op->op, opnd2->val);\n cout << \"res = \" << res << endl;\n }\n }\n\n return res;\n }\n};",
"memory": "38955"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n class stack_entry {\n public:\n int val;\n enum op {\n NONE,\n ADD,\n SUB,\n MUL,\n DIV\n } op;\n\n stack_entry(int v, enum op operation) {\n val = v;\n op = operation;\n }\n };\n\n int _calculate(int opnd1, enum stack_entry::op op, int opnd2) {\n int res;\n\n switch (op) {\n case stack_entry::ADD:\n res = opnd1 + opnd2;\n break;\n case stack_entry::SUB:\n res = opnd1 - opnd2;\n break;\n case stack_entry::MUL:\n res = opnd1 * opnd2;\n break;\n case stack_entry::DIV:\n res = opnd1 / opnd2;\n break;\n default:\n throw exception();\n }\n\n return res;\n }\n\npublic:\n int calculate(string s) {\n int i, j, n;\n int res;\n stack_entry *cur, *next;\n stack_entry *op;\n stack_entry *opnd1, *opnd2;\n vector<stack_entry *> stk;\n\n static const enum stack_entry::op tbl[] = {\n [(unsigned char)'+'] = stack_entry::ADD,\n [(unsigned char)'-'] = stack_entry::SUB,\n [(unsigned char)'*'] = stack_entry::MUL,\n [(unsigned char)'/'] = stack_entry::DIV\n };\n\n n = s.length();\n\n op = opnd1 = nullptr;\n i = 0;\n for (;;) {\n if (i == n)\n break;\n if (s[i] == ' ') {\n ++i;\n continue;\n }\n if (isdigit(s[i])) {\n int val;\n\n for (j = i + 1; j < n && isdigit(s[j]); j++)\n ;\n val = stoi(string(begin(s) + i, begin(s) + j), nullptr, 10);\n cur = new stack_entry(val, stack_entry::NONE);\n stk.push_back(cur);\n if (op == nullptr || (op->op != stack_entry::MUL && op->op != stack_entry::DIV))\n opnd1 = cur;\n else {\n opnd2 = cur;\n cur = new stack_entry(_calculate(opnd1->val, op->op, opnd2->val), stack_entry::NONE);\n delete opnd1;\n delete op;\n delete opnd2;\n stk.resize(stk.size() - 3);\n stk.push_back(cur);\n opnd1 = cur;\n }\n i = j;\n } else {\n op = new stack_entry(-1, tbl[(unsigned char)s[i]]);\n stk.push_back(op);\n ++i;\n }\n }\n\n n = stk.size();\n\n op = nullptr;\n res = stk[0]->val;\n for (i = 1; i < n; i++) {\n cur = stk[i];\n if (cur->op != stack_entry::NONE)\n op = cur;\n else {\n opnd2 = cur;\n res = _calculate(res, op->op, opnd2->val);\n }\n }\n\n return res;\n }\n};",
"memory": "38955"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\n struct Element {\n bool is_operator;\n union {\n char _operator;\n unsigned long _operand;\n };\n };\n\n list<Element>::iterator perform_op(list<Element> & expression, list<Element>::iterator iter) {\n unsigned long new_operand;\n if (iter->_operator == '*') {\n new_operand = prev(iter)->_operand * next(iter)->_operand;\n } else if (iter->_operator == '/') {\n new_operand = prev(iter)->_operand / next(iter)->_operand;\n } else if (iter->_operator == '+') {\n new_operand = prev(iter)->_operand + next(iter)->_operand;\n } else if (iter->_operator == '-') {\n new_operand = prev(iter)->_operand - next(iter)->_operand;\n }\n --iter;\n expression.erase(next(iter, 2));\n expression.erase(next(iter));\n iter->_operand = new_operand;\n return iter;\n }\n\npublic:\n int calculate(string s) {\n list<Element> expression;\n string num;\n for (int i = 0; i < s.size(); ++i) {\n if (s[i] >= '0' && s[i] <= '9') {\n num += s[i];\n } else {\n if (!num.empty()) {\n // cout << \"pushing \" << num << '\\n';\n expression.push_back({.is_operator = false, ._operand = stoul(num)});\n num.clear();\n }\n\n if (s[i] != ' ') {\n expression.push_back({.is_operator = true, ._operator = s[i]});\n }\n }\n }\n if (!num.empty()) {\n // cout << \"pushing \" << num << '\\n';\n expression.push_back({.is_operator = false, ._operand = stoul(num)});\n num.clear();\n }\n\n // for (int i = 0; i < expression.size(); ++i) {\n // if (expression[i].is_operator) {\n // cout << expression[i]._operator;\n // } else {\n // cout << expression[i]._operand;\n // }\n // }\n // cout << '\\n';\n\n // multiplication and division pass\n for (auto iter = expression.begin(); iter != expression.end(); ++iter) {\n if (iter->is_operator && (iter->_operator == '*' || iter->_operator == '/')) {\n iter = perform_op(expression, iter);\n }\n }\n\n // for (int i = 0; i < expression.size(); ++i) {\n // if (expression[i].is_operator) {\n // cout << expression[i]._operator;\n // } else {\n // cout << expression[i]._operand;\n // }\n // }\n // cout << '\\n';\n\n // addition and subtraction pass\n for (auto iter = expression.begin(); iter != expression.end(); ++iter) {\n if (iter->is_operator && (iter->_operator == '+' || iter->_operator == '-')) {\n iter = perform_op(expression, iter);\n }\n }\n\n // for (int i = 0; i < expression.size(); ++i) {\n // if (expression[i].is_operator) {\n // cout << expression[i]._operator;\n // } else {\n // cout << expression[i]._operand;\n // }\n // }\n // cout << '\\n';\n\n return expression.front()._operand;\n }\n};",
"memory": "39369"
} |
227 | <p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
<p>The integer division should truncate toward zero.</p>
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculate(string s) {\n // build calc graph\n // 1st: tokenize: N / N\n // 2nd: evaluate: N\n //\n enum Op {\n plus,\n minus,\n mult,\n div\n };\n auto map = unordered_map<char, Op>({{'+', plus}, {'-', minus}, {'*', mult}, {'/', div}});\n auto vec = list<variant<int, Op>>();\n auto number = -1;\n for(auto c: s) {\n switch(c) {\n case ' ':\n continue;\n case '+':\n case '-':\n case '*':\n case '/': {\n if(number != -1) {\n vec.push_back(number);\n number = -1;\n }\n\n vec.push_back(map[c]);\n break;\n }\n default:\n if(number == -1) {\n number = c - '0';\n } else {\n number *= 10;\n number += c - '0';\n }\n break;\n }\n }\n\n if(number != -1) {\n vec.push_back(number);\n number = -1;\n }\n\n // 2 eval mul and div\n for(auto iter = vec.begin(); iter != vec.end(); ++iter) {\n if(holds_alternative<Op>(*iter)) {\n auto left = get<int>(*prev(iter, 1));\n auto right = get<int>(*next(iter, 1));\n if(get<Op>(*iter) == mult) {\n auto pr = prev(iter, 1);\n *prev(iter, 1) = left * right;\n vec.erase(iter, next(iter, 2));\n iter = pr;\n } else if(get<Op>(*iter) == div) {\n auto pr = prev(iter, 1);\n *prev(iter, 1) = left / right;\n vec.erase(iter, next(iter, 2));\n iter = pr;\n }\n } \n }\n\n // 3 eval the rest\n for(auto iter = vec.begin(); iter != vec.end(); ++iter) {\n if(holds_alternative<Op>(*iter)) {\n auto left = get<int>(*prev(iter, 1));\n auto right = get<int>(*next(iter, 1));\n if(get<Op>(*iter) == plus) {\n auto pr = prev(iter, 1);\n *prev(iter, 1) = left + right;\n vec.erase(iter, next(iter, 2));\n iter = pr;\n } else if(get<Op>(*iter) == minus) {\n auto pr = prev(iter, 1);\n *prev(iter, 1) = left - right;\n vec.erase(iter, next(iter, 2));\n iter = pr;\n }\n } \n }\n\n return get<int>(vec.front());\n }\n};",
"memory": "39783"
} |
228 | <p>You are given a <strong>sorted unique</strong> integer array <code>nums</code>.</p>
<p>A <strong>range</strong> <code>[a,b]</code> is the set of all integers from <code>a</code> to <code>b</code> (inclusive).</p>
<p>Return <em>the <strong>smallest sorted</strong> list of ranges that <strong>cover all the numbers in the array exactly</strong></em>. That is, each element of <code>nums</code> is covered by exactly one of the ranges, and there is no integer <code>x</code> such that <code>x</code> is in one of the ranges but not in <code>nums</code>.</p>
<p>Each range <code>[a,b]</code> in the list should be output as:</p>
<ul>
<li><code>"a->b"</code> if <code>a != b</code></li>
<li><code>"a"</code> if <code>a == b</code></li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,2,4,5,7]
<strong>Output:</strong> ["0->2","4->5","7"]
<strong>Explanation:</strong> The ranges are:
[0,2] --> "0->2"
[4,5] --> "4->5"
[7,7] --> "7"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,2,3,4,6,8,9]
<strong>Output:</strong> ["0","2->4","6","8->9"]
<strong>Explanation:</strong> The ranges are:
[0,0] --> "0"
[2,4] --> "2->4"
[6,6] --> "6"
[8,9] --> "8->9"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= nums.length <= 20</code></li>
<li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li>All the values of <code>nums</code> are <strong>unique</strong>.</li>
<li><code>nums</code> is sorted in ascending order.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> summaryRanges(vector<int>& nums) {\n vector<string> result;\n int n = nums.size();\n if (n == 0) return result; // Handle empty input case\n \n int start = nums[0]; // Initialize the start of the first range\n \n for (int i = 1; i <= n; i++) {\n // Check if current number is not consecutive or we reached the end of the array\n if (i == n || nums[i] != nums[i - 1] + 1) {\n if (start == nums[i - 1]) {\n result.push_back(to_string(start)); // Single number range\n } else {\n result.push_back(to_string(start) + \"->\" + to_string(nums[i - 1])); // Range from start to current number\n }\n if (i < n) {\n start = nums[i]; // Start a new range\n }\n }\n }\n \n return result;\n }\n};\n",
"memory": "8200"
} |
228 | <p>You are given a <strong>sorted unique</strong> integer array <code>nums</code>.</p>
<p>A <strong>range</strong> <code>[a,b]</code> is the set of all integers from <code>a</code> to <code>b</code> (inclusive).</p>
<p>Return <em>the <strong>smallest sorted</strong> list of ranges that <strong>cover all the numbers in the array exactly</strong></em>. That is, each element of <code>nums</code> is covered by exactly one of the ranges, and there is no integer <code>x</code> such that <code>x</code> is in one of the ranges but not in <code>nums</code>.</p>
<p>Each range <code>[a,b]</code> in the list should be output as:</p>
<ul>
<li><code>"a->b"</code> if <code>a != b</code></li>
<li><code>"a"</code> if <code>a == b</code></li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,2,4,5,7]
<strong>Output:</strong> ["0->2","4->5","7"]
<strong>Explanation:</strong> The ranges are:
[0,2] --> "0->2"
[4,5] --> "4->5"
[7,7] --> "7"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,2,3,4,6,8,9]
<strong>Output:</strong> ["0","2->4","6","8->9"]
<strong>Explanation:</strong> The ranges are:
[0,0] --> "0"
[2,4] --> "2->4"
[6,6] --> "6"
[8,9] --> "8->9"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= nums.length <= 20</code></li>
<li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li>All the values of <code>nums</code> are <strong>unique</strong>.</li>
<li><code>nums</code> is sorted in ascending order.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> summaryRanges(vector<int>& nums) {\n vector<string> ans;\n int i = 0,j=0;\n while(i < nums.size()){\n j = i;\n while(j < nums.size()-1 && nums[j+1] == nums[j] + 1) j++;\n if(i == j)ans.push_back(to_string(nums[i]));\n else ans.push_back(to_string(nums[i]) + \"->\" + to_string(nums[j]));\n i = j+1;\n }\n return ans;\n \n }\n};",
"memory": "8300"
} |
228 | <p>You are given a <strong>sorted unique</strong> integer array <code>nums</code>.</p>
<p>A <strong>range</strong> <code>[a,b]</code> is the set of all integers from <code>a</code> to <code>b</code> (inclusive).</p>
<p>Return <em>the <strong>smallest sorted</strong> list of ranges that <strong>cover all the numbers in the array exactly</strong></em>. That is, each element of <code>nums</code> is covered by exactly one of the ranges, and there is no integer <code>x</code> such that <code>x</code> is in one of the ranges but not in <code>nums</code>.</p>
<p>Each range <code>[a,b]</code> in the list should be output as:</p>
<ul>
<li><code>"a->b"</code> if <code>a != b</code></li>
<li><code>"a"</code> if <code>a == b</code></li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,2,4,5,7]
<strong>Output:</strong> ["0->2","4->5","7"]
<strong>Explanation:</strong> The ranges are:
[0,2] --> "0->2"
[4,5] --> "4->5"
[7,7] --> "7"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,2,3,4,6,8,9]
<strong>Output:</strong> ["0","2->4","6","8->9"]
<strong>Explanation:</strong> The ranges are:
[0,0] --> "0"
[2,4] --> "2->4"
[6,6] --> "6"
[8,9] --> "8->9"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= nums.length <= 20</code></li>
<li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li>All the values of <code>nums</code> are <strong>unique</strong>.</li>
<li><code>nums</code> is sorted in ascending order.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> summaryRanges(vector<int>& nums) {\n\n vector<string> ans;\n string temp = \"\";\n int n = nums.size();\n bool flag = 0;\n for (int i = 0; i < n; i++) {\n\n int x = nums[i];\n temp = to_string(nums[i]);\n flag = 0;\n while (i < n - 1 and x + 1 == nums[i + 1]) {\n i++;\n x++;\n flag = 1;\n }\n if (flag) {\n temp += \"->\" + to_string(nums[i]);\n ans.push_back(temp);\n } else {\n ans.push_back(temp);\n }\n }\n return ans;\n }\n};",
"memory": "8300"
} |
228 | <p>You are given a <strong>sorted unique</strong> integer array <code>nums</code>.</p>
<p>A <strong>range</strong> <code>[a,b]</code> is the set of all integers from <code>a</code> to <code>b</code> (inclusive).</p>
<p>Return <em>the <strong>smallest sorted</strong> list of ranges that <strong>cover all the numbers in the array exactly</strong></em>. That is, each element of <code>nums</code> is covered by exactly one of the ranges, and there is no integer <code>x</code> such that <code>x</code> is in one of the ranges but not in <code>nums</code>.</p>
<p>Each range <code>[a,b]</code> in the list should be output as:</p>
<ul>
<li><code>"a->b"</code> if <code>a != b</code></li>
<li><code>"a"</code> if <code>a == b</code></li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,2,4,5,7]
<strong>Output:</strong> ["0->2","4->5","7"]
<strong>Explanation:</strong> The ranges are:
[0,2] --> "0->2"
[4,5] --> "4->5"
[7,7] --> "7"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,2,3,4,6,8,9]
<strong>Output:</strong> ["0","2->4","6","8->9"]
<strong>Explanation:</strong> The ranges are:
[0,0] --> "0"
[2,4] --> "2->4"
[6,6] --> "6"
[8,9] --> "8->9"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= nums.length <= 20</code></li>
<li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li>All the values of <code>nums</code> are <strong>unique</strong>.</li>
<li><code>nums</code> is sorted in ascending order.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> summaryRanges(vector<int>& nums) {\n vector<string> res;\n if(nums.empty())\n {\n return res;\n }\n int s=nums[0];\n for(int i=1;i<=nums.size();i++)\n {\n if(i==nums.size()||nums[i]!=nums[i-1]+1)\n {\n if(s!=nums[i-1])\n res.push_back(to_string(s)+\"->\"+to_string(nums[i-1]));\n else\n res.push_back(to_string(s));\n if(i<nums.size())\n s=nums[i];\n }\n }\n return res;\n }\n};",
"memory": "8400"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.