{"id":5455,"date":"2019-08-20T14:12:46","date_gmt":"2019-08-20T21:12:46","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5455"},"modified":"2019-08-20T14:20:41","modified_gmt":"2019-08-20T21:20:41","slug":"leetcode-34-find-first-and-last-position-of-element-in-sorted-array","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\/","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 ascending order, find the starting and ending position of a given&nbsp;<code>target<\/code>&nbsp;value.<\/p>\n\n\n\n<p>Your algorithm&#8217;s runtime complexity must be in the order of&nbsp;<em>O<\/em>(log&nbsp;<em>n<\/em>).<\/p>\n\n\n\n<p>If the target is not found in the array, return&nbsp;<code>[-1, -1]<\/code>.<\/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 = [<code>5,7,7,8,8,10]<\/code>, target = 8\n<strong>Output:<\/strong> [3,4]<\/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 = [<code>5,7,7,8,8,10]<\/code>, target = 6\n<strong>Output:<\/strong> [-1,-1]<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Binary Search<\/strong><\/h2>\n\n\n\n<p>Basically this problem asks you to implement lower_bound and upper_bound using binary search.<\/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    return {firstPos(nums, target), lastPos(nums, target)};\n  }\nprivate:\n  int firstPos(const vector<int>& nums, int target) {\n    int l = 0;\n    int r = nums.size();\n    while (l < r) {\n      int m = l + (r - l) \/ 2;      \n      if (nums[m] >= target) {\n        r = m;\n      } else {\n        l = m + 1;\n      }\n    }\n    \n    if (l == nums.size() || nums[l] != target) return -1;\n    return l;    \n  }\n\n  int lastPos(const vector<int>& nums, int target) {\n    int l = 0;\n    int r = nums.size();\n    while (l < r) {\n      int m = l + (r - l) \/ 2;      \n      if (nums[m] > target) {\n        r = m;\n      } else {\n        l = m + 1;\n      }\n    }\n    \/\/ l points to the first element this is greater than target.\n    --l;        \n    if (l < 0 || nums[l] != target) return -1;\n    return l;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of integers&nbsp;nums&nbsp;sorted in ascending order, find the starting and ending position of a given&nbsp;target&nbsp;value. Your algorithm&#8217;s runtime complexity must be in the&#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":[20,52,459],"class_list":["post-5455","post","type-post","status-publish","format-standard","hentry","category-binary-search","tag-array","tag-binary-search","tag-ologn","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5455","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=5455"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5455\/revisions"}],"predecessor-version":[{"id":5460,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5455\/revisions\/5460"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5455"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5455"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}