leetcode

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

commit b67b7c23858f740b38aa0b25e68d39035f8c2db4
parent 5a2766461d2ac2761e311719d3a4a15f679af1f6
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Sun,  4 Jun 2023 13:50:26 +0200

Random Problem

Diffstat:
AProblems/0228.cpp | 22++++++++++++++++++++++
MREADME.md | 1+
2 files changed, 23 insertions(+), 0 deletions(-)

diff --git a/Problems/0228.cpp b/Problems/0228.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + vector<string> summaryRanges(vector<int> &nums) { + if (!nums.size()) return {}; + vector<string> res; + int start = 0; + for (int i = 1; i < nums.size(); i++) { + if (nums[i] == nums[i - 1] + 1) continue; + if (i - start > 1) + res.push_back(to_string(nums[start]) + "->" + to_string(nums[i - 1])); + else + res.push_back(to_string(nums[start])); + start = i; + } + if (nums.size() - start > 1) + res.push_back(to_string(nums[start]) + "->" + to_string(nums.back())); + else + res.push_back(to_string(nums[start])); + + return res; + } +}; diff --git a/README.md b/README.md @@ -199,6 +199,7 @@ for solving problems. | 0225 | Easy | [Implement Stack using Queues](Problems/0225.cpp) | | 0226 | Easy | [Invert Binary Tree](Problems/0226.cpp) | | 0227 | Medium | [Basic Calculator II](Problems/0227.cpp) | +| 0228 | Easy | [Summary Ranges](Problems/0228.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) |