{"id":8797,"date":"2021-11-26T18:02:50","date_gmt":"2021-11-27T02:02:50","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8797"},"modified":"2021-11-26T18:03:26","modified_gmt":"2021-11-27T02:03:26","slug":"leetcode-34-find-first-and-last-position-of-element-in-sorted-array-2","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-34-find-first-and-last-position-of-element-in-sorted-array-2\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 34. Find First and Last Position of Element in Sorted Array"},"content":{"rendered":"\n<p>Given an array of integers&nbsp;<code>nums<\/code>&nbsp;sorted in non-decreasing order, find the starting and ending position of a given&nbsp;<code>target<\/code>&nbsp;value.<\/p>\n\n\n\n<p>If&nbsp;<code>target<\/code>&nbsp;is not found in the array, return&nbsp;<code>[-1, -1]<\/code>.<\/p>\n\n\n\n<p>You must&nbsp;write an algorithm with&nbsp;<code>O(log n)<\/code>&nbsp;runtime complexity.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [5,7,7,8,8,10], target = 8\n<strong>Output:<\/strong> [3,4]\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [5,7,7,8,8,10], target = 6\n<strong>Output:<\/strong> [-1,-1]\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [], target = 0\n<strong>Output:<\/strong> [-1,-1]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>0 &lt;= nums.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>-10<sup>9<\/sup>&nbsp;&lt;= nums[i]&nbsp;&lt;= 10<sup>9<\/sup><\/code><\/li><li><code>nums<\/code>&nbsp;is a non-decreasing array.<\/li><li><code>-10<sup>9<\/sup>&nbsp;&lt;= target&nbsp;&lt;= 10<sup>9<\/sup><\/code><\/li><\/ul>\n\n\n\n<p><strong>Solution: Binary Search<\/strong><\/p>\n\n\n\n<p>Use lower_bound to find first<br>Use upper_bound to find last + 1<\/p>\n\n\n\n<p>Time complexity: O(logn)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<int> searchRange(vector<int>& nums, int target) {\n    auto it = lower_bound(begin(nums), end(nums), target);\n    int l = (it == end(nums) || *it != target) ? -1 : it - begin(nums);\n    auto it2 = upper_bound(begin(nums), end(nums), target);\n    int r = (it2 == begin(nums) || *prev(it2) != target) ? -1 : prev(it2) - begin(nums);\n    return {l, r};\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of integers&nbsp;nums&nbsp;sorted in non-decreasing order, find the starting and ending position of a given&nbsp;target&nbsp;value. If&nbsp;target&nbsp;is not found in the array, return&nbsp;[-1, -1].&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[149],"tags":[52],"class_list":["post-8797","post","type-post","status-publish","format-standard","hentry","category-binary-search","tag-binary-search","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8797","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8797"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8797\/revisions"}],"predecessor-version":[{"id":8800,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8797\/revisions\/8800"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8797"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8797"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8797"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}