leetcode

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

commit 09a5f47ff26ee85aa41e7b238cec78a8825575ec
parent 5747d61b9b206396e516aeafedfc90598dcc366c
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Tue, 21 Feb 2023 22:43:07 +0100

Daily Problem

Diffstat:
AProblems/0540.cpp | 15+++++++++++++++
MREADME.md | 1+
2 files changed, 16 insertions(+), 0 deletions(-)

diff --git a/Problems/0540.cpp b/Problems/0540.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + int singleNonDuplicate(vector<int> &nums) { + int low = 0, high = nums.size() - 1; + while (low < high) { + int mid = low + (high - low) / 2; + if ((mid % 2 == 0 && nums[mid] == nums[mid + 1]) || + (mid % 2 == 1 && nums[mid] == nums[mid - 1])) + low = mid + 1; + else + high = mid; + } + return nums[low]; + } +}; diff --git a/README.md b/README.md @@ -252,6 +252,7 @@ for solving problems. | 0520 | Easy | [Detect Capital](Problems/0520.cpp) | | 0530 | Easy | [Minimum Absolute Difference in BST](Problems/0530.cpp) | | 0538 | Medium | [Convert BST to Greater Tree](Problems/0538.cpp) | +| 0540 | Medium | [Single Element in a Sorted Array](Problems/0540.cpp) | | 0542 | Medium | [01 Matrix](Problems/0542.cpp) | | 0543 | Easy | [Diameter of Binary Tree](Problems/0543.cpp) | | 0547 | Medium | [Number of Provinces](Problems/0547.cpp) |