{"id":1447,"date":"2026-02-27T09:42:33","date_gmt":"2026-02-27T01:42:33","guid":{"rendered":"http:\/\/www.tgwttt.xyz\/?p=1447"},"modified":"2026-02-27T09:42:33","modified_gmt":"2026-02-27T01:42:33","slug":"%e6%90%9c%e7%b4%a2%e6%97%8b%e8%bd%ac%e6%8e%92%e5%ba%8f%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/www.tgwttt.xyz\/?p=1447","title":{"rendered":"\u641c\u7d22\u65cb\u8f6c\u6392\u5e8f\u6570\u7ec4"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode.cn\/problems\/search-in-rotated-sorted-array\/description\/\">33. \u641c\u7d22\u65cb\u8f6c\u6392\u5e8f\u6570\u7ec4 &#8211; \u529b\u6263\uff08LeetCode\uff09<\/a><\/p>\n\n\n\n<p>\u6574\u6570\u6570\u7ec4&nbsp;<code>nums<\/code>&nbsp;\u6309\u5347\u5e8f\u6392\u5217\uff0c\u6570\u7ec4\u4e2d\u7684\u503c&nbsp;<strong>\u4e92\u4e0d\u76f8\u540c<\/strong>&nbsp;\u3002<\/p>\n\n\n\n<p>\u5728\u4f20\u9012\u7ed9\u51fd\u6570\u4e4b\u524d\uff0c<code>nums<\/code>&nbsp;\u5728\u9884\u5148\u672a\u77e5\u7684\u67d0\u4e2a\u4e0b\u6807&nbsp;<code>k<\/code>\uff08<code>0 &lt;= k &lt; nums.length<\/code>\uff09\u4e0a\u8fdb\u884c\u4e86&nbsp;<strong>\u5411\u5de6\u65cb\u8f6c<\/strong>\uff0c\u4f7f\u6570\u7ec4\u53d8\u4e3a&nbsp;<code>[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]<\/code>\uff08\u4e0b\u6807&nbsp;<strong>\u4ece 0 \u5f00\u59cb<\/strong>&nbsp;\u8ba1\u6570\uff09\u3002\u4f8b\u5982\uff0c&nbsp;<code>[0,1,2,4,5,6,7]<\/code>&nbsp;\u4e0b\u6807&nbsp;<code>3<\/code>&nbsp;\u4e0a\u5411\u5de6\u65cb\u8f6c\u540e\u53ef\u80fd\u53d8\u4e3a&nbsp;<code>[4,5,6,7,0,1,2]<\/code>&nbsp;\u3002<\/p>\n\n\n\n<p>\u7ed9\u4f60&nbsp;<strong>\u65cb\u8f6c\u540e<\/strong>&nbsp;\u7684\u6570\u7ec4&nbsp;<code>nums<\/code>&nbsp;\u548c\u4e00\u4e2a\u6574\u6570&nbsp;<code>target<\/code>&nbsp;\uff0c\u5982\u679c&nbsp;<code>nums<\/code>&nbsp;\u4e2d\u5b58\u5728\u8fd9\u4e2a\u76ee\u6807\u503c&nbsp;<code>target<\/code>&nbsp;\uff0c\u5219\u8fd4\u56de\u5b83\u7684\u4e0b\u6807\uff0c\u5426\u5219\u8fd4\u56de&nbsp;<code>-1<\/code>&nbsp;\u3002<\/p>\n\n\n\n<p>\u4f60\u5fc5\u987b\u8bbe\u8ba1\u4e00\u4e2a\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a&nbsp;<code>O(log n)<\/code>&nbsp;\u7684\u7b97\u6cd5\u89e3\u51b3\u6b64\u95ee\u9898\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>nums = [4,5,6,7,0,1,2], target = 0\n<strong>\u8f93\u51fa\uff1a<\/strong>4\n<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b&nbsp;2\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>nums = [4,5,6,7,0,1,2], target = 3\n<strong>\u8f93\u51fa\uff1a<\/strong>-1<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>nums = [1], target = 0\n<strong>\u8f93\u51fa\uff1a<\/strong>-1\n<\/pre>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>1 &lt;= nums.length &lt;= 5000<\/code><\/li>\n\n\n\n<li><code>-10<sup>4<\/sup> &lt;= nums[i] &lt;= 10<sup>4<\/sup><\/code><\/li>\n\n\n\n<li><code>nums<\/code>\u00a0\u4e2d\u7684\u6bcf\u4e2a\u503c\u90fd\u00a0<strong>\u72ec\u4e00\u65e0\u4e8c<\/strong><\/li>\n\n\n\n<li>\u9898\u76ee\u6570\u636e\u4fdd\u8bc1\u00a0<code>nums<\/code>\u00a0\u5728\u9884\u5148\u672a\u77e5\u7684\u67d0\u4e2a\u4e0b\u6807\u4e0a\u8fdb\u884c\u4e86\u65cb\u8f6c<\/li>\n\n\n\n<li><code>-10<sup>4<\/sup> &lt;= target &lt;= 10<sup>4<\/sup><\/code><\/li>\n<\/ul>\n\n\n\n<p>\u5728\u505a\u8fd9\u4e2a\u9898\u4e4b\u524d\uff0c\u6211\u4eec\u9996\u5148\u6765\u5b66\u4e60\u4e00\u4e0b\u4e8c\u5206<\/p>\n\n\n\n<p>\u4e8c\u5206\u67e5\u627e<br>\u5f53\u6211\u4eec\u8981\u4ece\u4e00\u4e2a\u5e8f\u5217\u4e2d\u67e5\u627e\u4e00\u4e2a\u5143\u7d20\u7684\u65f6\u5019\uff0c\u6700\u5feb\u60f3\u5230\u7684\u65b9\u6cd5\u5c31\u662f\u66b4\u529b\u67e5\u627e\u6cd5\uff08\u5373\uff1a\u4ece\u524d\u5230\u540e\u4f9d\u6b21\u67e5\u627e\uff09\u3002\u4f46\u8fd9\u79cd\u65b9\u6cd5\u8fc7\u4e8e\u65e0\u8111\uff0c\u9002\u7528\u4e8e\u5143\u7d20\u8f83\u5c11\u7684\u65f6\u5019\uff0c\u4e00\u65e6\u5143\u7d20\u4e2a\u6570\u591a\u8d77\u6765\uff0c\u6548\u7387\u662f\u975e\u5e38\u4f4e\u4e0b\uff0c\u6240\u4ee5\u5728\u5b9e\u9645\u4e2d\u8fd9\u79cd\u67e5\u627e\u7684\u65b9\u6cd5\u662f\u88ab\u6452\u5f03\u7684\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u5c31\u4e0d\u5f97\u4e0d\u4ecb\u7ecd\u4e00\u79cd\u7b80\u5355\u4e14\u6548\u7387\u8f83\u9ad8\u7684\u67e5\u627e\u65b9\u6cd5\u4e86\uff1a\u4e8c\u5206\u67e5\u627e\u6cd5\uff0c\u53c8\u79f0\u6298\u534a\u67e5\u627e\u6cd5\u3002\u4f46\u8be5\u65b9\u6cd5\u662f\u5efa\u7acb\u5728\u6709\u5e8f\u7684\u524d\u63d0\u4e0b\u7684\u3002<\/p>\n\n\n\n<p>\u4e8c\u5206\u67e5\u627e\u6cd5\u7684\u524d\u63d0\u6761\u4ef6\u662f\uff1a\u67e5\u627e\u7684\u5e8f\u5217\u5fc5\u987b\u662f\u6709\u5e8f\u7684\u3002\u5373\u8be5\u5e8f\u5217\u4e2d\u7684\u6240\u6709\u5143\u7d20\u90fd\u662f\u6309\u7167\u5347\u5e8f\u6216\u8005\u964d\u5e8f\u6392\u5217\u597d\u7684\uff0c\u5143\u7d20\u4e0e\u5143\u7d20\u53ea\u95f4\u7684\u5dee\u503c\u867d\u7136\u662f\u968f\u673a\u7684\uff0c\u4f46\u59cb\u7ec8\u662f\u5728\u9012\u589e\u6216\u9012\u51cf\u3002<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int check(int num&#91;],int size,int t){\n    int left=0,right=size-1;\t\t  \/\/ \u5b9a\u4e49\u4e86t\u5728\u5de6\u95ed\u53f3\u95ed\u7684\u533a\u95f4\u5185\uff0c&#91;left, right]\n    while(left&lt;=right){\t\t\t\t  \/\/\u5f53left==right\u65f6\uff0c\u533a\u95f4&#91;left, right]\u4ecd\u7136\u6709\u6548\n        int mid=left+((right-left)\/2);\/\/\u7b49\u540c\u4e8e(left+right)\/2\uff0c\u9632\u6b62\u6ea2\u51fa\n        if(num&#91;mid]>t){\n            right=mid-1;\t  \t\t \/\/t\u5728\u5de6\u533a\u95f4\uff0c\u6240\u4ee5\u7b54\u6848\u5728&#91;left, mid-1]\n        }else if(num&#91;mid]&lt;t){\n            left=mid+1;\t\t        \/\/t\u5728\u53f3\u533a\u95f4\uff0c\u6240\u4ee5\u7b54\u6848\u5728&#91;mid+1,right]\n        }else{\t\t\t  \t     \t\/\/num&#91;mid]==t\uff0c\u627e\u5230\u7b54\u6848\n            return mid;\n        }\n    }\n    return -1;\t\t\t        \t\/\/\u6ca1\u6709\u627e\u5230\u76ee\u6807\u503c\n}<\/code><\/pre>\n\n\n\n<p>\u7b80\u5355\u7684\u5b66\u4e60\u4e86\u4e00\u4e0b\u4e8c\u5206\u6211\u4eec\u6765\u770b\u63a5\u4e0b\u6765\u8fd9\u9053\u9898\uff1a<\/p>\n\n\n\n<div class=\"wp-block-group is-vertical is-layout-flex wp-container-core-group-is-layout-8cf370e7 wp-block-group-is-layout-flex\">\n<p>\u5bf9\u4e8e\u6709\u5e8f\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e8c\u5206\u67e5\u627e\u7684\u65b9\u6cd5\u67e5\u627e\u5143\u7d20\u3002<\/p>\n\n\n\n<p>\u4f46\u662f\u8fd9\u9053\u9898\u4e2d\uff0c\u6570\u7ec4\u672c\u8eab\u4e0d\u662f\u6709\u5e8f\u7684\uff0c\u8fdb\u884c\u65cb\u8f6c\u540e\u53ea\u4fdd\u8bc1\u4e86\u6570\u7ec4\u7684\u5c40\u90e8\u662f\u6709\u5e8f\u7684\uff0c\u8fd9\u8fd8\u80fd\u8fdb\u884c\u4e8c\u5206\u67e5\u627e\u5417\uff1f\u7b54\u6848\u662f\u53ef\u4ee5\u7684\u3002<\/p>\n\n\n\n<p>\u53ef\u4ee5\u53d1\u73b0\u7684\u662f\uff0c\u6211\u4eec\u5c06\u6570\u7ec4\u4ece\u4e2d\u95f4\u5206\u5f00\u6210\u5de6\u53f3\u4e24\u90e8\u5206\u7684\u65f6\u5019\uff0c\u4e00\u5b9a\u6709\u4e00\u90e8\u5206\u7684\u6570\u7ec4\u662f\u6709\u5e8f\u7684\u3002\u62ff\u793a\u4f8b\u6765\u770b\uff0c\u6211\u4eec\u4ece 6 \u8fd9\u4e2a\u4f4d\u7f6e\u5206\u5f00\u4ee5\u540e\u6570\u7ec4\u53d8\u6210\u4e86 [4, 5, 6] \u548c [7, 0, 1, 2] \u4e24\u4e2a\u90e8\u5206\uff0c\u5176\u4e2d\u5de6\u8fb9 [4, 5, 6] \u8fd9\u4e2a\u90e8\u5206\u7684\u6570\u7ec4\u662f\u6709\u5e8f\u7684\uff0c\u5176\u4ed6\u4e5f\u662f\u5982\u6b64\u3002<\/p>\n\n\n\n<p>\u8fd9\u542f\u793a\u6211\u4eec\u53ef\u4ee5\u5728\u5e38\u89c4\u4e8c\u5206\u67e5\u627e\u7684\u65f6\u5019\u67e5\u770b\u5f53\u524d mid \u4e3a\u5206\u5272\u4f4d\u7f6e\u5206\u5272\u51fa\u6765\u7684\u4e24\u4e2a\u90e8\u5206 [l, mid] \u548c [mid + 1, r] \u54ea\u4e2a\u90e8\u5206\u662f\u6709\u5e8f\u7684\uff0c\u5e76\u6839\u636e\u6709\u5e8f\u7684\u90a3\u4e2a\u90e8\u5206\u786e\u5b9a\u6211\u4eec\u8be5\u5982\u4f55\u6539\u53d8\u4e8c\u5206\u67e5\u627e\u7684\u4e0a\u4e0b\u754c\uff0c\u56e0\u4e3a\u6211\u4eec\u80fd\u591f\u6839\u636e\u6709\u5e8f\u7684\u90a3\u90e8\u5206\u5224\u65ad\u51fa target \u5728\u4e0d\u5728\u8fd9\u4e2a\u90e8\u5206\uff1a<\/p>\n\n\n\n<p>\u5982\u679c [l, mid &#8211; 1] \u662f\u6709\u5e8f\u6570\u7ec4\uff0c\u4e14 target \u7684\u5927\u5c0f\u6ee1\u8db3 [nums[l],nums[mid])\uff0c\u5219\u6211\u4eec\u5e94\u8be5\u5c06\u641c\u7d22\u8303\u56f4\u7f29\u5c0f\u81f3 [l, mid &#8211; 1]\uff0c\u5426\u5219\u5728 [mid + 1, r] \u4e2d\u5bfb\u627e\u3002<br>\u5982\u679c [mid, r] \u662f\u6709\u5e8f\u6570\u7ec4\uff0c\u4e14 target \u7684\u5927\u5c0f\u6ee1\u8db3 [nums[mid+1],nums[r]]\uff0c\u5219\u6211\u4eec\u5e94\u8be5\u5c06\u641c\u7d22\u8303\u56f4\u7f29\u5c0f\u81f3 [mid + 1, r]\uff0c\u5426\u5219\u5728 [l, mid &#8211; 1] \u4e2d\u5bfb\u627e\u3002<\/p>\n\n\n\n<p>\u53c2\u8003\u7b54\u6848\u5982\u4e0b\uff1a<\/p>\n<\/div>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\npublic:\n    int search(vector&lt;int>&amp; nums, int target) {\n        \/\/\u4e8c\u5206\u67e5\u627e\n        int  l=0,r=nums.size()-1;\n        if(r==0)return nums&#91;0]==target?0:-1;\n        while(l&lt;=r){\n            int mid=(l+r)\/2;\n           if(target==nums&#91;mid])return mid;\n            \/\/\u5224\u65ad\u5347\u964d\u5e8f\n           if(l&lt;=mid&amp;&amp;nums&#91;l]&lt;=nums&#91;mid]){\n                \/\/\u5de6\u534a\u90e8\u5206\u6709\u5e8f\n                if(target>=nums&#91;l]&amp;&amp;target&lt;nums&#91;mid]){\n                    r=mid-1;\n                }else{\n                    l=mid+1;\n                }\n           }else{\n                \/\/\u53f3\u534a\u90e8\u5206\u6709\u5e8f\n                if(target>nums&#91;mid]&amp;&amp;target&lt;=nums&#91;r]){\n                    l=mid+1;\n                }else{\n                    r=mid-1;\n                }\n           }\n        }\n        return -1;\n    }\n};<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"827\" height=\"712\" src=\"http:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2026\/02\/image.png\" alt=\"\" class=\"wp-image-1448\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2026\/02\/image.png 827w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2026\/02\/image-300x258.png 300w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2026\/02\/image-768x661.png 768w\" sizes=\"auto, (max-width: 827px) 100vw, 827px\" \/><\/figure>\n\n\n\n<p>\u4eca\u5929\u7684\u66f4\u65b0\u5c31\u5230\u8fd9\u91cc\uff0c\u5982\u6709\u9519\u8bef\u6b22\u8fce\u6307\u51fa\uff0c\u8c22\u8c22\u5927\u5bb6<\/p>\n","protected":false},"excerpt":{"rendered":"<p>33. \u641c\u7d22\u65cb\u8f6c\u6392\u5e8f\u6570\u7ec4 &#8211; \u529b\u6263\uff08LeetCode\uff09 \u6574\u6570\u6570\u7ec4&nbsp;nums&nbsp;\u6309&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-1447","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1447","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1447"}],"version-history":[{"count":1,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1447\/revisions"}],"predecessor-version":[{"id":1449,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1447\/revisions\/1449"}],"wp:attachment":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1447"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}