LeetCode 162 Find Peak Element
2015-12-03 09:10
239 查看
题目描述
A peak element is an element that is greater than its neighbors.Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.
The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.
You may imagine that num[-1] = num
= -∞.
For example, in array [1, 2, 3, 1], 3 is a peak element and your function should return the index number 2.
click to show spoilers.
Note:
Your solution should be in logarithmic complexity.
分析
要log的时间复杂度,要用到二分查找。代码
[code] public static int findPeakElement(int[] nums) { int low = 0; int high = nums.length - 1; while (low < high) { int mid = low + (high - low) / 2; if (nums[mid] > nums[mid + 1]) { high = mid; } else { low = mid + 1; } } return low; }
相关文章推荐
- Java可读取操作系统的配置
- cnki搜索候选
- java继承
- 利用反射导入导出Excel
- 性能测试问题总结3
- Myeclipse 2013 注册破解Craker 亲测可以
- phpstudy 下开启openssl
- ulipad python相关设置
- C++11 在析构函数中执行lambda表达式(std::function)捕获this指针的陷阱
- Win7操作系统的客户端操作用友响应慢.
- exec函数族
- 自定义 URL Scheme 完全指南
- 学习路上之dubbo配置文件的标签
- 角色及权限
- bootstrap提供了六种列表效果
- 1053. Path of Equal Weight (30) DFS
- XXX.axf: Error: L6218E: Undefined symbol xxx (referred from xxxx.o).
- iOS开发-中文转拼音
- Linux学习笔记(二、硬件设备文件名)
- 电脑音量图标不见了