{"id":2834,"date":"2020-01-31T15:33:18","date_gmt":"2020-01-31T07:33:18","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=2834"},"modified":"2020-01-31T15:33:18","modified_gmt":"2020-01-31T07:33:18","slug":"%e3%80%90leetcode%e3%80%910033-%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":"http:\/\/www.sniper97.cn\/index.php\/note\/algorithm\/2834\/","title":{"rendered":"\u3010LeetCode\u30110033 \u641c\u7d22\u65cb\u8f6c\u6392\u5e8f\u6570\u7ec4"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/search-in-rotated-sorted-array\/\">https:\/\/leetcode-cn.com\/problems\/search-in-rotated-sorted-array\/<\/a><\/p>\n\n\n<pre class=\"wp-block-preformatted\"># -*- coding:utf-8 -*-\n<em>\n<\/em>class Solution(object):\n    def search(self, nums, target):\n        <em>\"\"\"\n        <\/em><strong><em>:type<\/em><\/strong><em> nums: List[int]\n        <\/em><strong><em>:type<\/em><\/strong><em> target: int\n        <\/em><strong><em>:rtype<\/em><\/strong><em>: int\n        \"\"\"\n        <\/em>if len(nums) == 0:\n            return -1\n        if len(nums) == 1:\n            return 0 if nums[0] == target else -1\n        def find_max(m, n):\n            <em>\"\"\"\u5bfb\u627e\u6700\u5927\u503c\"\"\"\n            <\/em>if m >= n:\n                return m\n            mid = (m + n) \/\/ 2\n            if nums[mid] > nums[mid + 1]:\n                return mid\n            elif nums[mid] >= nums[0]:\n                return find_max(mid + 1, n)\n            else:\n                return find_max(m, mid - 1)\n        def binary_search(m, n):\n            <em>\"\"\"\n            \u4e8c\u5206\u67e5\u627e\n            <\/em><strong><em>:param<\/em><\/strong><em> m:\n            <\/em><strong><em>:param<\/em><\/strong><em> n:\n            <\/em><strong><em>:return<\/em><\/strong><em>:\n            \"\"\"\n            <\/em>if m > n:\n                return -1\n            mid = (m + n) \/\/ 2\n            if nums[mid] == target:\n                return mid\n            elif nums[mid] &lt; target:\n                return binary_search(mid + 1, n)\n            else:\n                return binary_search(m, mid - 1)\n        max_idx = find_max(0, len(nums) - 1)\n        return binary_search(0, max_idx) if binary_search(0, max_idx) != -1 else binary_search(max_idx + 1,\n                                                                                               len(nums) - 1)\nif __name__ == '__main__':\n    nums = [8, 9, 2, 3, 4]\n    print(Solution().search(nums, 9))\n<\/pre>\n\n\n<p><strong>\u601d\u8def<\/strong>\uff1a\u8fd9\u9053\u9898\u56e0\u4e3a\u9650\u5236lgn\uff0c\u6240\u4ee5\u4e00\u5b9a\u662f\u4e8c\u5206\u641c\u7d22\u3002<\/p>\n\n\n<p>\u9996\u5148\u4e8c\u5206\u641c\u7d22\u6700\u5927\u503c\uff0c\u7136\u540e\u5206\u522b\u4ee5\u6700\u5927\u503c\u4e3a\u5206\u754c\uff0c\u5bf9\u4e24\u4e2a\u5217\u8868\u8fdb\u884c\u4e8c\u5206\u641c\u7d22\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/leetcode-cn.com\/problems\/search-in-rotated [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[10],"tags":[],"views":1592,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2834"}],"collection":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2834"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2834\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2834"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}