【刷题-LeetCode】162 Find Peak Element
- 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.
Follow up: Your solution should be in logarithmic complexity.
解法1 线性扫描
解法2 二分查找
- nums[mid] > nums[mid+1]:nums[mid+1]一定不是峰值,[l, mid]搜索
 - nums[mid] <= nums[mid+1]:nums[mid+1]可能是峰值,在[mid+1, r]搜索
 
class Solution {
public:
    int findPeakElement(vector<int>& nums) {
        int l = 0, r = nums.size()-1;
        while(l < r){
            int mid = (l+r)/2;
            if(nums[mid] > nums[mid+1])r=mid;
            else l = mid+1;
        }
        return l;
    }
};
												
											【刷题-LeetCode】162 Find Peak Element的更多相关文章
- 【leetcode刷题笔记】Find Peak Element
		
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
 - LeetCode 162. Find Peak Element (找到峰值)
		
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
 - (二分查找  拓展) 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 ...
 - [LeetCode] 162. Find Peak Element 查找峰值元素
		
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
 - LeetCode 162 Find Peak Element
		
Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...
 - 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] ≠ ...
 - ✡   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 162 Find Peak Element(二分法)
		
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
 - LeetCode 162.Find Peak Element(M)(P)
		
题目: A peak element is an element that is greater than its neighbors. Given an input array where num[ ...
 - LeetCode——162. Find Peak Element
		
一.题目链接: https://leetcode.com/problems/find-peak-element/ 二.题目大意: 给定一个长度为N的一维数组,数组是无序的,要求找到数组中的极大值(或局 ...
 
随机推荐
- Tornado WEB服务器框架 Epoll-- 【模板】
			
4.2 使用模板 1. 路径与渲染 使用模板,需要仿照静态文件路径设置一样,向web.Application类的构造函数传递一个名为template_path的参数来告诉Tornado从文件系统的一个 ...
 - idea tomcat 热部署
			
一有改动就在页面显示改动的东西就称为idea tomcat 热部署,开发的时候热部署一定要是demo:war exploded状态,若是在demo:war状态,怎么配置都不可能热部署,然后还要做如下的 ...
 - C# 使用Fluent API 创建自己的DSL
			
DSL(Domain Specified Language)领域专用语言是描述特定领域问题的语言,听起来很唬人,其实不是什么高深的东西.看一下下面的代码: using FlunetApiDemo; v ...
 - input type 使用
			
type属性值 hidden: 隐藏. text:文本 search:搜索 tel url email password:密码 date:日期选择器 month:月份选择器 week:周选择器 tim ...
 - docker启动报错:Failed to Setup IP tables: Unable to enable SKIP DNAT rule
			
Creating network "kafka_default" with the default driverERROR: Failed to Setup IP tables: ...
 - SpringBoot整合Swagger框架 ,并设置接口请求头token默认值
			
引入maven依赖 <!-- swagger2--> <dependency> <groupId>io.springfox</groupId> &l ...
 - C++ 获取函数耗时
			
C++ 记录耗时 #include <sys/timeb.h> #include <stdio.h> long long getSystemTime() { struct ti ...
 - 【LeetCode】198. House Robber 打家劫舍 解题报告(Java & Python)
			
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 递归 + 记忆化 动态规划 优化动态规划空间 ...
 - 【LeetCode】31. Next Permutation 解题报告(Python & C++)
			
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 逆序数字交换再翻转 库函数 日期 题目地址:http ...
 - 【LeetCode】894. All Possible Full Binary Trees 解题报告(Python & C++)
			
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...