{"id":8152,"date":"2024-04-15T15:44:58","date_gmt":"2024-04-15T07:44:58","guid":{"rendered":"https:\/\/white-night.club\/?p=8152"},"modified":"2024-04-15T15:45:21","modified_gmt":"2024-04-15T07:45:21","slug":"ad185","status":"publish","type":"post","link":"https:\/\/white-night.club\/index.php\/2024\/04\/15\/ad185\/","title":{"rendered":"852. \u5c71\u8109\u6570\u7ec4\u7684\u5cf0\u9876\u7d22\u5f15"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode.cn\/problems\/peak-index-in-a-mountain-array\/description\/\">https:\/\/leetcode.cn\/problems\/peak-index-in-a-mountain-array\/description\/<\/a><\/p>\n\n\n\n<p>\u867d\u7136\u662f\u4e2d\u7b49\u9898\uff0c\u4f46\u5b9e\u9645\u4e0a\u5c31\u662f\u904d\u5386\u6570\u7ec4\u627e\u6700\u5927\u503c\u3002<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7\">Go<\/span><span role=\"button\" tabindex=\"0\" data-code=\"func peakIndexInMountainArray(arr []int) int {\n    max,index:=0,0\n    for i:=0;i&lt;len(arr);i++{\n        if arr[i]&gt;max{\n            max,index=arr[i],i\n        }\n    }\n    return index\n}\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">func<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">peakIndexInMountainArray<\/span><span style=\"color: #D4D4D4\">(arr []<\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">) <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #9CDCFE\">max<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #9CDCFE\">index<\/span><span style=\"color: #D4D4D4\">:=<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">i<\/span><span style=\"color: #D4D4D4\">:=<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">;i&lt;<\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(arr);i++{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> arr[i]&gt;max{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #9CDCFE\">max<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #9CDCFE\">index<\/span><span style=\"color: #D4D4D4\">=arr[i],i<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> index<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u76f4\u5230\u6211\u770b\u5230\u9898\u76ee\u8981\u6c42\u5fc5\u987b\u4f7f\u7528O(logn)\u7684\u7b97\u6cd5\uff0c\u90a3\u80af\u5b9a\u662f\u8981\u4e8c\u5206\u4e86\u3002\u4e0d\u8fc7\u592a\u4e45\u6ca1\u5199\u90fd\u4e0d\u77e5\u9053\u81ea\u5df1\u5199\u7684\u7b97\u4e0d\u7b97\u4e8c\u5206\u4e86\u3002<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7\">Go<\/span><span role=\"button\" tabindex=\"0\" data-code=\"func peakIndexInMountainArray(arr []int) int {\n    left,right:=0,len(arr)\n    mid:=right\/2\n    for left&lt;right{\n        if arr[mid]&gt;arr[mid-1]&amp;&amp;arr[mid]&gt;arr[mid+1]{\n            break\n        }else if arr[mid]&lt;arr[mid+1]{\n            left=mid\n        }else if arr[mid-1]&gt;arr[mid]{\n            right=mid\n        }\n        mid=(left+right)\/2\n    }\n    return mid\n}\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">func<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">peakIndexInMountainArray<\/span><span style=\"color: #D4D4D4\">(arr []<\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">) <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #9CDCFE\">left<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #9CDCFE\">right<\/span><span style=\"color: #D4D4D4\">:=<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(arr)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #9CDCFE\">mid<\/span><span style=\"color: #D4D4D4\">:=right\/<\/span><span style=\"color: #B5CEA8\">2<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> left&lt;right{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> arr[mid]&gt;arr[mid-<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">]&amp;&amp;arr[mid]&gt;arr[mid+<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">]{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">break<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        }<\/span><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> arr[mid]&lt;arr[mid+<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">]{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #9CDCFE\">left<\/span><span style=\"color: #D4D4D4\">=mid<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        }<\/span><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> arr[mid-<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">]&gt;arr[mid]{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #9CDCFE\">right<\/span><span style=\"color: #D4D4D4\">=mid<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #9CDCFE\">mid<\/span><span style=\"color: #D4D4D4\">=(left+right)\/<\/span><span style=\"color: #B5CEA8\">2<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> mid<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span><\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u4e8c\u5206<\/p>\n","protected":false},"author":1,"featured_media":7932,"comment_status":"open","ping_status":"","sticky":false,"template":"base","format":"standard","meta":{"footnotes":""},"categories":[42,25],"tags":[38],"_links":{"self":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/8152"}],"collection":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/comments?post=8152"}],"version-history":[{"count":0,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/8152\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media\/7932"}],"wp:attachment":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media?parent=8152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/categories?post=8152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/tags?post=8152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}