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[n] = -∞.

For example, in array [1, 2, 3, 1], 3 is a peak element and your function should return the index number 2.

Note:

Your solution should be in logarithmic complexity.

思路:log time,所以用二分法。二分法无非是左值、中间值、右值的比较。

如果mid<mid-1,那么下一次判断left~mid-1;(此时mid相当于-∞)

如果mid<mid+1,那么下一次判断mid+1~right(此时mid相当于-∞)

class Solution {
public:
int findPeakElement(vector<int>& nums) {
return binarySearch(nums,,nums.size()-);
} int binarySearch(vector<int>& nums, int start, int end){
if(start == end) return start;
if(start+ == end){
if(nums[start]>nums[end]) return start;
else return end;
} int mid = start + (end-start)/;
if(nums[mid]<nums[mid-]){
return binarySearch(nums,start,mid-);
}
else if(nums[mid]<nums[mid+]){
return binarySearch(nums,mid+,end);
}
else{
return mid;
}
}
};

162. Find Peak Element (Array; Divide-and-Conquer)的更多相关文章

  1. 【LeetCode】162. Find Peak Element 解题报告(Python)

    [LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...

  2. LeetCode 162 Find Peak Element

    Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...

  3. Java for LeetCode 162 Find Peak Element

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  4. 162. Find Peak Element

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  5. ✡ leetcode 162. Find Peak Element --------- java

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  6. LeetCode OJ 162. Find Peak Element

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  7. LeetCode 162. Find Peak Element (找到峰值)

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  8. (二分查找 拓展) leetcode 162. Find Peak Element && lintcode 75. Find Peak Element

    A peak element is an element that is greater than its neighbors. Given an input array nums, where nu ...

  9. Find Peak Element(ARRAY - Devide-and-Conquer)

    QUESTION A peak element is an element that is greater than its neighbors. Given an input array where ...

随机推荐

  1. js处理数据库时间格式/Date(1332919782070)/

    js处理数据库时间格式 数据库返回时间格式:/Date(1332919782070)/ 方法: function ChangeDateFormat(val) { if (val != null) { ...

  2. react-native 安卓支持 gif动态图

    需要在android/app/build.gradle文件中添加模块 //这一行没有的话得加上才行 compile "com.facebook.fresco:fresco:1.5.0&quo ...

  3. Photoshop 辅助线和标尺的使用技巧

    1.拖动辅助线时按住Alt键可以在水平辅助线和垂直辅助线之间切换.按住Alt键点击一条已经存在的垂直辅助线可以把它转为水平辅助线,反之亦然. 注意:辅助线是通过从标尺中拖出而建立的,所以要确保标尺是打 ...

  4. 【JEECG技术文档】JEECG 组织机构导入V3.7

    1.功能介绍 组织机构导入 提供组织机构模版导入功能,使用户更快速的创建组织机构 要使用组织机构导入功能需要完成以下步骤: 1. 下载模版excel 2. 填写组织机构信息 3. 点击导入-选择文件- ...

  5. Java 判断当前系统为Window或者Linux

    public static boolean isOSLinux() {         Properties prop = System.getProperties();         String ...

  6. crontab使用说明及例子程序

    http://blog.csdn.net/yygydjkthh/article/details/7845639 http://walkerqt.blog.51cto.com/1310630/16901 ...

  7. linux查询硬件信息

    硬件信息查询 sudo dmidecode -t baseboard

  8. Emac

    https://emacs-china.org/ 启动 console模式的emacs,$emacs -nw 学习参考: 1)<21天精通emacs> 2)Emacs常用命令简集. 3)& ...

  9. linux 3.10 的又一次hung

    最近又遇到一次hung,dmesg中堆栈如下: [176223.913270] ------------[ cut here ]------------ [ PID: at net/sched/sch ...

  10. mybatis中事务简单使用

    一步: 事务只用在service层方法上加 @Transactional(propagation = Propagation.REQUIRED)       :发现如果没有它,增加执行-->1/ ...