From 944168338cac6a37e2376ba3a4c9dc1bd6437586 Mon Sep 17 00:00:00 2001 From: SquidSpirit Date: Tue, 25 Feb 2025 23:31:25 +0800 Subject: [PATCH] feat: 34_find_first_and_last_position_of_element_in_sorted_array --- .../main.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 34_find_first_and_last_position_of_element_in_sorted_array/main.cpp diff --git a/34_find_first_and_last_position_of_element_in_sorted_array/main.cpp b/34_find_first_and_last_position_of_element_in_sorted_array/main.cpp new file mode 100644 index 0000000..219c422 --- /dev/null +++ b/34_find_first_and_last_position_of_element_in_sorted_array/main.cpp @@ -0,0 +1,16 @@ +#include +using namespace std; + +class Solution { + public: + vector searchRange(vector& nums, int target) { + if (!binary_search(nums.begin(), nums.end(), target)) { + return {-1, -1}; + } + + auto first = lower_bound(nums.begin(), nums.end(), target); + auto second = upper_bound(nums.begin(), nums.end(), target); + + return {(int)(first - nums.begin()), (int)(second - nums.begin() - 1)}; + } +};