leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit 986a9d99f8a15599817a3fc25a19ff9c092276e1
parent ea7e108287fbb30d6445aa397b9414f9fedefc4d
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Thu,  3 Aug 2023 22:19:43 +0200

Random Problem

Diffstat:
AProblems/0241.cpp | 27+++++++++++++++++++++++++++
MREADME.md | 1+
2 files changed, 28 insertions(+), 0 deletions(-)

diff --git a/Problems/0241.cpp b/Problems/0241.cpp @@ -0,0 +1,27 @@ +class Solution { + int operate(char op, int a, int b) { + switch (op) { + case '+': return a + b; + case '-': return a - b; + case '*': return a * b; + } + return 0; + } + + vector<int> rec(const string &expression, int start, int end) { + vector<int> res; + for (int i = start; i < end; i++) { + if (isdigit(expression[i])) continue; + for (auto n1 : rec(expression, start, i)) + for (auto n2 : rec(expression, i + 1, end)) + res.push_back(operate(expression[i], n1, n2)); + } + if (res.empty()) return {stoi(expression.substr(start, end - start))}; + return res; + } + +public: + vector<int> diffWaysToCompute(const string &expression) { + return rec(expression, 0, expression.size()); + } +}; diff --git a/README.md b/README.md @@ -213,6 +213,7 @@ for solving problems. | 0238 | Medium | [Product of Array Except Self](Problems/0238.cpp) | | 0239 | Hard | [Sliding Window Maximum](Problems/0239.cpp) | | 0240 | Medium | [Search a 2D Matrix II](Problems/0240.cpp) | +| 0241 | Medium | [Different Ways to Add Parentheses](Problems/0241.cpp) | | 0242 | Easy | [Valid Anagram](Problems/0242.cpp) | | 0257 | Easy | [Binary Tree Paths](Problems/0257.cpp) | | 0258 | Easy | [Add Digits](Problems/0258.cpp) |