leetcode

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

commit ddeb5116a28a5787281927daa366ca2ef1f90cbf
parent aaf0d36bf2738d925161020a41bc304b2d0a4592
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Tue, 21 Feb 2023 21:40:26 +0100

LeetCode 75 II: Day 18

Diffstat:
AProblems/0227.cpp | 19+++++++++++++++++++
AProblems/0735.cpp | 12++++++++++++
MREADME.md | 2++
3 files changed, 33 insertions(+), 0 deletions(-)

diff --git a/Problems/0227.cpp b/Problems/0227.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + int calculate(string s) { + stringstream ss("+" + s); + char op; + int n, last, ans = 0; + while (ss >> op >> n) { + if (op == '+' || op == '-') { + n = op == '+' ? n : -n; + ans += n; + } else { + n = op == '*' ? last * n : last / n; + ans = ans - last + n; + } + last = n; + } + return ans; + } +}; diff --git a/Problems/0735.cpp b/Problems/0735.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + vector<int> asteroidCollision(vector<int>& asteroids) { + vector<int> st; + for (int aster : asteroids) { + while (!st.empty() && st.back() > 0 && st.back() < -aster) st.pop_back(); + if (st.empty() || aster > 0 || st.back() < 0) st.push_back(aster); + else if (aster < 0 && st.back() == -aster) st.pop_back(); + } + return st; + } +}; diff --git a/README.md b/README.md @@ -168,6 +168,7 @@ for solving problems. | 0222 | Medium | [Count Complete Tree Nodes](Problems/0222.cpp) | | 0223 | Medium | [Rectangle Area](Problems/0223.cpp) | | 0226 | Easy | [Invert Binary Tree](Problems/0226.cpp) | +| 0227 | Medium | [Basic Calculator II](Problems/0227.cpp) | | 0231 | Easy | [Power of Two](Problems/0231.cpp) | | 0232 | Easy | [Implement Queue using Stacks](Problems/0232.cpp) | | 0234 | Easy | [Palindrome Linked List](Problems/0234.cpp) | @@ -287,6 +288,7 @@ for solving problems. | 0714 | Medium | [Best Time to Buy and Sell Stock with Transaction Fee](Problems/0714.cpp) | | 0724 | Easy | [Find Pivot Index](Problems/0724.cpp) | | 0733 | Easy | [Flood Fill](Problems/0733.cpp) | +| 0735 | Medium | [Asteroid Collision](Problems/0735.cpp) | | 0739 | Medium | [Daily Temperatures](Problems/0739.cpp) | | 0740 | Medium | [Delete and Earn](Problems/0740.cpp) | | 0743 | Medium | [Network Delay Time](Problems/0743.cpp) |