leetcode

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

commit 5fd55bcf1031021e7ce5dba8f0e8fb833a9d7262
parent 17aabb25ba1a5be13448189c828e6741748b99c3
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Sat, 21 Jan 2023 15:54:42 +0100

LeetCode 75 I: Day 1

Diffstat:
MProblems/1480.cpp | 10++++++++++
AProblems/1991.cpp | 12++++++++++++
MREADME.md | 1+
3 files changed, 23 insertions(+), 0 deletions(-)

diff --git a/Problems/1480.cpp b/Problems/1480.cpp @@ -7,3 +7,13 @@ public: return res; } }; + +// using lambda function +class Solution { +public: + vector<int> runningSum(vector<int>& nums) { + int acc = 0; + for_each(nums.begin(), nums.end(), [&acc](int &a) { a = acc+=a; }); + return nums; + } +}; diff --git a/Problems/1991.cpp b/Problems/1991.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + int findMiddleIndex(vector<int> &nums) { + int left = 0; + int right = accumulate(nums.begin(), nums.end(), 0); + for (int i = 0; i < nums.size(); left += nums[i++]) { + right -= nums[i]; + if (left == right) return i; + } + return -1; + } +}; diff --git a/README.md b/README.md @@ -292,6 +292,7 @@ for solving problems. | 1962 | Medium | [Remove Stones to Minimize the Total](Problems/1962.cpp) | | 1971 | Easy | [Find if Path Exists in Graph](Problems/1971.cpp) | | 1976 | Medium | [Number of Ways to Arrive at Destination](Problems/1976.cpp) | +| 1991 | Easy | [Find the Middle Index in Array](Problems/1991.cpp) | | 2039 | Medium | [The Time When the Network Becomes Idle](Problems/2039.cpp) | | 2073 | Easy | [Time Needed to Buy Tickets](Problems/2073.cpp) | | 2085 | Easy | [Count Common Words With One Occurrence](Problems/2085.cpp) |