Title: #224 - Basic Calculator - LeetCode JavaScript Solutions
Open Graph Title: #224 - Basic Calculator - LeetCode JavaScript Solutions
X Title: #224 - Basic Calculator - LeetCode JavaScript Solutions
Description: Given a string s representing a valid expression, implement a basic calculator to evaluate it, and return the result of the evaluation. Note...
Open Graph Description: Given a string s representing a valid expression, implement a basic calculator to evaluate it, and return the result of the evaluation. Note...
X Description: Given a string s representing a valid expression, implement a basic calculator to evaluate it, and return the result of the evaluation. Note...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/basic-calculator
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/basic-calculator","name":"#224 - Basic Calculator - LeetCode JavaScript Solutions","description":"Given a string s representing a valid expression, implement a basic calculator to evaluate it, and return the result of the evaluation. Note...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * @param {string} s\n * @return {number}\n */\nvar calculate = function(s) {\n const stack = [];\n let result = 0;\n\n for (let i = 0, sign = 1; i < s.length; i += 1) {\n if (s[i] >= '0' && s[i] <= '9') {\n let value = 0;\n while (s[i] >= '0' && s[i] <= '9') {\n value = (value * 10) + (s[i] - '0');\n i += 1;\n }\n result += value * sign;\n i -= 1;\n } else if (s[i] === '+') {\n sign = 1;\n } else if (s[i] === '-') {\n sign = -1;\n } else if (s[i] === '(') {\n stack.push(result, sign);\n result = 0;\n sign = 1;\n } else if (s[i] === ')') {\n result = stack.pop() * result;\n result += stack.pop();\n }\n }\n\n return result;\n};","keywords":"LeetCode 224, #224 - Basic Calculator, Hard, JavaScript solution","learningResourceType":"Code","isAccessibleForFree":true,"educationalLevel":"intermediate","interactivityType":"mixed"}
| author | LeetCodeJavascript.com |
| og:type | article |
| og:image | https://leetcodejavascript.com/og-image.jpg |
| og:site_name | LeetCode JavaScript Solutions |
| twitter:card | summary_large_image |
| twitter:url | https://leetcodejavascript.com/solutions/basic-calculator |
| twitter:image | https://leetcodejavascript.com/og-image.jpg |
| twitter:creator | @joshcrozier |
| theme-color | #1f2937 |
Links:
| LeetCodeJavascript.com | https://leetcodejavascript.com/ |
| Star on GitHub | https://github.com/JoshCrozier/leetcode-javascript |
| Back to all solutions | https://leetcodejavascript.com |
| View on LeetCode | https://leetcode.com/problems/basic-calculator/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/0224-basic-calculator.js |
| String | https://leetcodejavascript.com/tags/string |
| Math | https://leetcodejavascript.com/tags/math |
| Stack | https://leetcodejavascript.com/tags/stack |
| Recursion | https://leetcodejavascript.com/tags/recursion |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow