剑指 Offer II 069:山峰数组的顶部
This commit is contained in:
parent
c0df40dc06
commit
8971dbfb6b
101
src/main/java/leetcode/editor/cn/B1IidL.java
Normal file
101
src/main/java/leetcode/editor/cn/B1IidL.java
Normal file
@ -0,0 +1,101 @@
|
||||
//符合下列属性的数组 arr 称为 山峰数组(山脉数组) :
|
||||
//
|
||||
//
|
||||
// arr.length >= 3
|
||||
// 存在 i(0 < i < arr.length - 1)使得:
|
||||
//
|
||||
// arr[0] < arr[1] < ... arr[i-1] < arr[i]
|
||||
// arr[i] > arr[i+1] > ... > arr[arr.length - 1]
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 给定由整数组成的山峰数组 arr ,返回任何满足 arr[0] < arr[1] < ... arr[i - 1] < arr[i] > arr[i +
|
||||
//1] > ... > arr[arr.length - 1] 的下标 i ,即山峰顶部。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:arr = [0,1,0]
|
||||
//输出:1
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:arr = [1,3,5,4,2]
|
||||
//输出:2
|
||||
//
|
||||
//
|
||||
// 示例 3:
|
||||
//
|
||||
//
|
||||
//输入:arr = [0,10,5,2]
|
||||
//输出:1
|
||||
//
|
||||
//
|
||||
// 示例 4:
|
||||
//
|
||||
//
|
||||
//输入:arr = [3,4,5,1]
|
||||
//输出:2
|
||||
//
|
||||
//
|
||||
// 示例 5:
|
||||
//
|
||||
//
|
||||
//输入:arr = [24,69,100,99,79,78,67,36,26,19]
|
||||
//输出:2
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 3 <= arr.length <= 10⁴
|
||||
// 0 <= arr[i] <= 10⁶
|
||||
// 题目数据保证 arr 是一个山脉数组
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 进阶:很容易想到时间复杂度 O(n) 的解决方案,你可以设计一个 O(log(n)) 的解决方案吗?
|
||||
//
|
||||
//
|
||||
//
|
||||
// 注意:本题与主站 852 题相同:https://leetcode-cn.com/problems/peak-index-in-a-mountain-
|
||||
//array/
|
||||
// Related Topics 数组 二分查找 👍 35 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
//剑指 Offer II 069:山峰数组的顶部
|
||||
class B1IidL {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new B1IidL().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int peakIndexInMountainArray(int[] arr) {
|
||||
int n = arr.length;
|
||||
int l = 0, r = n - 2;
|
||||
while (l < r) {
|
||||
int mid = l + r >> 1;
|
||||
if (arr[mid] > arr[mid + 1]) {
|
||||
r = mid;
|
||||
} else {
|
||||
l = mid + 1;
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
File diff suppressed because one or more lines are too long
69
src/main/java/leetcode/editor/cn/doc/content/B1IidL.md
Normal file
69
src/main/java/leetcode/editor/cn/doc/content/B1IidL.md
Normal file
@ -0,0 +1,69 @@
|
||||
<p>符合下列属性的数组 <code>arr</code> 称为 <strong>山峰数组</strong>(<strong>山脉数组)</strong> :</p>
|
||||
|
||||
<ul>
|
||||
<li><code>arr.length >= 3</code></li>
|
||||
<li>存在 <code>i</code>(<code>0 < i < arr.length - 1</code>)使得:
|
||||
<ul>
|
||||
<li><code>arr[0] < arr[1] < ... arr[i-1] < arr[i] </code></li>
|
||||
<li><code>arr[i] > arr[i+1] > ... > arr[arr.length - 1]</code></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p>给定由整数组成的山峰数组 <code>arr</code> ,返回任何满足 <code>arr[0] < arr[1] < ... arr[i - 1] < arr[i] > arr[i + 1] > ... > arr[arr.length - 1]</code> 的下标 <code>i</code> ,即山峰顶部。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = [0,1,0]
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = [1,3,5,4,2]
|
||||
<strong>输出:2</strong>
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = [0,10,5,2]
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = [3,4,5,1]
|
||||
<strong>输出:</strong>2
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 5:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = [24,69,100,99,79,78,67,36,26,19]
|
||||
<strong>输出:</strong>2
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= arr.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= arr[i] <= 10<sup>6</sup></code></li>
|
||||
<li>题目数据保证 <code>arr</code> 是一个山脉数组</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>进阶:</strong>很容易想到时间复杂度 <code>O(n)</code> 的解决方案,你可以设计一个 <code>O(log(n))</code> 的解决方案吗?</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><meta charset="UTF-8" />注意:本题与主站 852 题相同:<a href="https://leetcode-cn.com/problems/peak-index-in-a-mountain-array/">https://leetcode-cn.com/problems/peak-index-in-a-mountain-array/</a></p>
|
||||
<div><div>Related Topics</div><div><li>数组</li><li>二分查找</li></div></div><br><div><li>👍 35</li><li>👎 0</li></div>
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user