{"id":8793,"date":"2021-11-26T17:51:32","date_gmt":"2021-11-27T01:51:32","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8793"},"modified":"2021-11-26T17:51:58","modified_gmt":"2021-11-27T01:51:58","slug":"leetcode-33-search-in-rotated-sorted-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-33-search-in-rotated-sorted-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 33. Search in Rotated Sorted Array"},"content":{"rendered":"\n<p>There is an integer array&nbsp;<code>nums<\/code>&nbsp;sorted in ascending order (with&nbsp;<strong>distinct<\/strong>&nbsp;values).<\/p>\n\n\n\n<p>Prior to being passed to your function,&nbsp;<code>nums<\/code>&nbsp;is&nbsp;<strong>possibly rotated<\/strong>&nbsp;at an unknown pivot index&nbsp;<code>k<\/code>&nbsp;(<code>1 &lt;= k &lt; nums.length<\/code>) such that the resulting array is&nbsp;<code>[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]<\/code>&nbsp;(<strong>0-indexed<\/strong>). For example,&nbsp;<code>[0,1,2,4,5,6,7]<\/code>&nbsp;might be rotated at pivot index&nbsp;<code>3<\/code>&nbsp;and become&nbsp;<code>[4,5,6,7,0,1,2]<\/code>.<\/p>\n\n\n\n<p>Given the array&nbsp;<code>nums<\/code>&nbsp;<strong>after<\/strong>&nbsp;the possible rotation and an integer&nbsp;<code>target<\/code>, return&nbsp;<em>the index of&nbsp;<\/em><code>target<\/code><em>&nbsp;if it is in&nbsp;<\/em><code>nums<\/code><em>, or&nbsp;<\/em><code>-1<\/code><em>&nbsp;if it is not in&nbsp;<\/em><code>nums<\/code>.<\/p>\n\n\n\n<p>You must 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 = [4,5,6,7,0,1,2], target = 0\n<strong>Output:<\/strong> 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 = [4,5,6,7,0,1,2], target = 3\n<strong>Output:<\/strong> -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 = [1], target = 0\n<strong>Output:<\/strong> -1\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 5000<\/code><\/li><li><code>-10<sup>4<\/sup>&nbsp;&lt;= nums[i] &lt;= 10<sup>4<\/sup><\/code><\/li><li>All values of&nbsp;<code>nums<\/code>&nbsp;are&nbsp;<strong>unique<\/strong>.<\/li><li><code>nums<\/code>&nbsp;is an ascending array that is possibly rotated.<\/li><li><code>-10<sup>4<\/sup>&nbsp;&lt;= target &lt;= 10<sup>4<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Binary Search<\/strong><\/h2>\n\n\n\n<p>If the current range [l, r] is ordered, reduce to normal binary search. Otherwise, determine the range to search next by comparing target and nums[0].<\/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  int search(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      int x = (nums[m] < nums[0]) == (target < nums[0])\n              ? nums[m]\n              : target < nums[0] ? INT_MIN : INT_MAX;\n      if (x < target)\n        l = m + 1;\n      else if (x > target)\n        r = m;\n      else\n        return m;\n    }\n    return -1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There is an integer array&nbsp;nums&nbsp;sorted in ascending order (with&nbsp;distinct&nbsp;values). Prior to being passed to your function,&nbsp;nums&nbsp;is&nbsp;possibly rotated&nbsp;at an unknown pivot index&nbsp;k&nbsp;(1 &lt;= k &lt; nums.length)&#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,177,702],"class_list":["post-8793","post","type-post","status-publish","format-standard","hentry","category-binary-search","tag-binary-search","tag-medium","tag-rotated","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8793","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=8793"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8793\/revisions"}],"predecessor-version":[{"id":8795,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8793\/revisions\/8795"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8793"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}