{"id":8937,"date":"2021-11-29T00:47:01","date_gmt":"2021-11-29T08:47:01","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8937"},"modified":"2021-11-29T00:48:19","modified_gmt":"2021-11-29T08:48:19","slug":"leetcode-81-search-in-rotated-sorted-array-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-81-search-in-rotated-sorted-array-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 81. Search in Rotated Sorted Array II"},"content":{"rendered":"\n<p>There is an integer array&nbsp;<code>nums<\/code>&nbsp;sorted in non-decreasing order (not necessarily with&nbsp;<strong>distinct<\/strong>&nbsp;values).<\/p>\n\n\n\n<p>Before being passed to your function,&nbsp;<code>nums<\/code>&nbsp;is&nbsp;<strong>rotated<\/strong>&nbsp;at an unknown pivot index&nbsp;<code>k<\/code>&nbsp;(<code>0 &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,4,4,5,6,6,7]<\/code>&nbsp;might be rotated at pivot index&nbsp;<code>5<\/code>&nbsp;and become&nbsp;<code>[4,5,6,6,7,0,1,2,4,4]<\/code>.<\/p>\n\n\n\n<p>Given the array&nbsp;<code>nums<\/code>&nbsp;<strong>after<\/strong>&nbsp;the rotation and an integer&nbsp;<code>target<\/code>, return&nbsp;<code>true<\/code><em>&nbsp;if&nbsp;<\/em><code>target<\/code><em>&nbsp;is in&nbsp;<\/em><code>nums<\/code><em>, or&nbsp;<\/em><code>false<\/code><em>&nbsp;if it is not in&nbsp;<\/em><code>nums<\/code><em>.<\/em><\/p>\n\n\n\n<p>You must decrease the overall operation steps as much as possible.<\/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 = [2,5,6,0,0,1,2], target = 0\n<strong>Output:<\/strong> true\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 = [2,5,6,0,0,1,2], target = 3\n<strong>Output:<\/strong> false\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><code>nums<\/code>&nbsp;is guaranteed to be rotated at some pivot.<\/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 or divide and conquer<\/strong><\/h2>\n\n\n\n<p>If current range is ordered, use binary search, Otherwise, divide and conquer.<\/p>\n\n\n\n<p>Time complexity: O(logn) best, O(n) worst<br>Space complexity: O(logn)<\/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  bool search(vector<int>& A, int target) {\n    return search(A, 0, A.size() - 1, target);\n  }\nprivate:\n  bool search(vector<int>& A, int l, int r, int target) {\n    if (l > r) return 0;\n    if (l == r) return target == A[l];\n\n    int mid = l + (r - l) \/ 2;\n\n    if (A[l] < A[mid] &#038;&#038; A[mid] < A[r])\n      return (target <= A[mid]) ? search(A, l, mid, target) : search(A, mid + 1, r, target);\n    else\n      return search(A, l, mid, target) || search(A, mid + 1, r, target);\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There is an integer array&nbsp;nums&nbsp;sorted in non-decreasing order (not necessarily with&nbsp;distinct&nbsp;values). Before being passed to your function,&nbsp;nums&nbsp;is&nbsp;rotated&nbsp;at an unknown pivot index&nbsp;k&nbsp;(0 &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":[20,52,177,702],"class_list":["post-8937","post","type-post","status-publish","format-standard","hentry","category-binary-search","tag-array","tag-binary-search","tag-medium","tag-rotated","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8937","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=8937"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8937\/revisions"}],"predecessor-version":[{"id":8939,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8937\/revisions\/8939"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8937"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8937"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8937"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}