162. Find Peak Element (Array; Divide-and-Conquer)
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.
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)的更多相关文章
- 【LeetCode】162. Find Peak Element 解题报告(Python)
[LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...
- 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] ≠ ...
- 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 OJ 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 (找到峰值)
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 ...
- 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 ...
随机推荐
- Linux 实现与宿主机共享文件夹 Centos7
(选用的系统 centos7) 01,添加共享目录,右键虚拟机 => 设置 => 选项 => 共享文件夹(只有在虚拟机为关机状态才能添加) 02,开机,查看当前虚拟机的共享目录 =& ...
- 利用STM32CubeMX来生成USB_HID_Mouse工程
硬件开发板:STM32F103C8 软件平台 好了现在开始利用STM32CubeMX来生成我们的工程 1.新建工程 选择MCU的型号 选择选择时钟 开启usb的模块 选择USB的类 配置时钟树(主要是 ...
- 用JS 和 jQery获取屏幕的高度和宽度
用的时候,网上找了下,放在一起,方便以后查阅 document.body.clientWidth document.body.offsetWidth(包括线宽)//网页可见区域宽 document.b ...
- django日志配置
直接参考这篇,很详细:https://www.cnblogs.com/changqing8023/p/9639769.html 补充一点:日志文件打开时,中文乱码,要在handler中设置编码格式,' ...
- elasticsearch相关
- CSS样式学习-1
一.分类 ①内联,写在标签中,写法是style="样式属性".优先级最高. 优点:控制精确.缺点:代码重用性差,范围小. 例如: <div style="font- ...
- 检查nginx后端real server脚本,实现发现宕机剔除,恢复服务自动加入功能
#!/bin/bash #Author: Liang WeiCheng ip_array=($(grep "server 10.112.84" /etc/nginx/nginx.c ...
- nginx压缩,缓存
https://www.darrenfang.com/2015/01/setting-up-http-cache-and-gzip-with-nginx/ https://www.linuxdashe ...
- java自定义抛出的异常Exception
package com.zhanzhuang.exception; public class CustomizeException { public static void main(String[] ...
- NSMapTable
跟NSDictionary用法差不多,不过区别是NSMapTable可以设置内存选项,例如可以设置key跟value的内存属性(weak/strong),从而避免内存泄露. 例如这个+ weakToW ...