(二分查找 拓展) 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 nums[i] ≠ nums[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 nums[-1] = nums[n] = -∞.
Example 1:
Input: nums =[1,2,3,1]
Output: 2
Explanation: 3 is a peak element and your function should return the index number 2.
Example 2:
Input: nums =[1,2,1,3,5,6,4]
Output: 1 or 5
Explanation: Your function can return either index number 1 where the peak element is 2,
or index number 5 where the peak element is 6.
Note:
Your solution should be in logarithmic complexity.
-----------------------------------------------------------------------------------------------------------------------------------------
这个在leetcode上用O(n)算法可以过,但是在lintcode 上会超时。
可以用二分查找,二分查找有递归写法和迭代写法。
C++:迭代
class Solution {
public:
    int findPeakElement(vector<int>& nums) {
        if(nums.size() == ) return -;
        int left = ,right = nums.size() - ;
        int mid;
        while(left < right){
            mid = left + (right - left)/;
            if(nums[mid] > nums[mid + ]) right = mid;  //mid不会加1,因为它本身也有可能是“山顶”。
            else left = mid + ;  //会加1 的,因为nums[mid]肯定不是“山顶”,所以忽略它。
        }
        return left;
    }
};
详情见官方题解:https://leetcode.com/articles/find-peak-element/
另一个迭代解法(在lintcode上):
class Solution {
public:
    /**
     * @param A: An integers array.
     * @return: return any of peek positions.
     */
    int findPeak(vector<int> &A) {
        // write your code here
        if(A.size() == ) return -;
        int l = ,r = A.size() - ;
        int mid;
        while(l < r){
            mid = l + (r - l)/;
            if(A[mid] < A[mid - ])
                r = mid;
            else if(A[mid] < A[mid + ])
                l = mid + ;
            else
                return mid;
        }
        //mid = A[l] > A[r] ? l:r;
        //return mid;
    }
};
也有一个解法:
class Solution {
public:
    int findPeakElement(vector<int>& nums) {
        if(nums.size() == )
            return -;
        int left = ;
        int right = nums.size() - ;
        while(left + < right){
            int mid = left + (right - left)/;
            if(nums[mid] > nums[mid + ]) right = mid;
            else if(nums[mid] < nums[mid + ]) left = mid;
            //else return mid;
        }
        int mid = nums[left] > nums[right] ? left:right;
        return mid;
    }
};
(二分查找 拓展) leetcode 162. Find Peak Element && lintcode 75. Find Peak Element的更多相关文章
- (二分查找 拓展) leetcode 34. Find First and Last Position of Element in Sorted Array && lintcode 61. Search for a Range
		
Given an array of integers nums sorted in ascending order, find the starting and ending position of ...
 - (二分查找 拓展) leetcode 69. Sqrt(x)
		
Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a no ...
 - (二分查找 拓展) leetcode278. First Bad Version
		
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
 - lintcode  75 Find Peak Element
		
Hi 大家,这道题是lintcode上的find peak element的题,不是leecode的那道, 这两道题是有区别的,这道题的题目中说明了:只有左右两侧的数都小于某个元素,这种才是峰值, 而 ...
 - C#LeetCode刷题-二分查找
		
二分查找篇 # 题名 刷题 通过率 难度 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)-该题未达最优解 30 ...
 - LeetCode 162 Find Peak Element
		
Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...
 - ✡   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] ≠ ...
 - Leetcode 35 Search Insert Position 二分查找(二分下标)
		
基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题... 题意:在一个有序数列中,要插入数target,找出插入的位置. 楼主在这里更新了<二分查找综述>第 ...
 - [LeetCode] #167# Two Sum II : 数组/二分查找/双指针
		
一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...
 
随机推荐
- 弹性布局--flex方向
			
flex方向 flex方向由flex-direction特性决定,用于定义弹性布局模式.flex-direction共有4种模式:从左向右.从右向左.从上往下.从下往上. 主轴 主轴的起点与终点定义了 ...
 - go语言打造p2p网络
			
传送门: 柏链项目学院 就像1000个人眼中有1000个哈姆雷特一样,每个人眼中的区块链也是不一样的!作为技术人员眼中的区块链就是将各种技术的融合,包括密码学,p2p网络,分布式共识机制以及博弈论等. ...
 - css之overflow应用
			
overflow应用的两个小例子: 1.单行文本出现省略号的情况 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional// ...
 - ztree搜索节点并展开
			
web <div class="zTreeC"> <div class="searchL" lay-filter="searchL& ...
 - 【博客导航】Nico博客导航汇总
			
摘要 介绍本博客关注的内容大类.任务.工具方法及链接,提供Nico博文导航. 导航汇总 [博客导航]Nico博客导航汇总 [导航]信息检索导航 [导航]Python相关 [导航]读书导航 [导航]FP ...
 - ASP.NET基础知识汇总之WebConfig自定义节点详细介绍
			
之前介绍过Webconfig的具体知识ASP.NET基础知识汇总之WebConfig各节点介绍.今天准备封装一个ConfigHelper类,涉及到了自定义节点的东东,平时虽然一直用,但也没有系统的总结 ...
 - scrapy formRequest 表单提交
			
scrapy.FormRequest 主要用于提交表单数据 先来看一下源码 参数: formdata (dict or iterable of tuples) – is a dictionary ( ...
 - 3Openwrt自定义CGI实现 前后端交互
			
https://www.cnblogs.com/163yun/p/9834993.html 安装uhttpd. 在编译openwrt前,输入make memuconfig,查找Network -> ...
 - OSI模型网络七层协议
			
物理层 物理层是OSI的第一层,它虽然处于最底层,却是整个开放系统的基础.物理层为设备之间的数据通信提供传输媒体及互连设备,为数据传输提供可靠的环境. 1.1媒体和互连设备 物理层的媒体包括架空明线. ...
 - VUE  简单属性操作
			
在main.js内配置路由及相应模板 import Vue from 'vue' import App from './App' // 引入router路由 import Router from 'v ...