↓ 跳过正文
  1. Posts/

leetcode162. Find Peak Element (O(lgn)复杂度寻找峰值)

·448 字·1 分钟

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.

click to show spoilers.

**Note:**Your solution should be in logarithmic complexity.

思路:

为什么能lgn。。。

首先要想明白,这样的峰值是一定存在的(因为最左最右已经负无穷了。

如果中间元素的值,比它右边相邻元素的小,说明什么呢?

说明右边区间一定存在峰值。

为什么?

现在a[mid]<a[mid+1]

如果a[mid+1]>a[mid+2],那么 a[mid+1]就是峰值

如果a[mid+1]<=a[mid+2],那么可以继续缩小区间,到[mid+2,n-1]

由于a[n]为负无穷,因此至少会出现一个 a[x]>a[x+1]的情况,此时a[x]就是峰值。

想清楚了这点就好办了。二分就好。

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2017年04月14日 星期五 20时15分01秒
 4File Name :162.cpp
 5************************************************ */
 6class Solution {
 7
 8public:
 9
10    int findPeakElement(vector<int>& nums) {
11        int n = nums.size();
12	int l = 0 ;
13	int r = n-1;
14	while (l<=r)
15	{
16	    if (l==r) return l;
17	    int mid = (l+r)>>1;
18	    if (nums[mid]<nums[mid+1])
19		l = mid + 1;
20	    else r = mid;
21	}
22
23
24    }
25
26};

相关文章

leetcode 33. Search in Rotated Sorted Array (无重复数的旋转数组找定值)

·372 字·1 分钟
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no duplicate exists in the array. 思路:找规律。。。二分。。。 0 1 2 3 4 5 6 1 2 3 4 5 6 0 2 3 4 5 6 0 1 3 4 5 6 0 1 2 4 5 6 0 1 2 3 5 6 0 1 2 3 4 6 0 1 2 3 4 5 观察发现。。。a[mid]<a[r]的时候,后半段有序;

leetcode 34. Search for a Range (二分,找到一段值为tar的区间)

·333 字·1 分钟
Given an array of integers sorted in ascending order, find the starting and ending position of a given target value. Your algorithm’s runtime complexity must be in the order of O(log n). If the target is not found in the array, return [-1, -1]. For example, Given [5, 7, 7, 8, 8, 10] and target value 8, return [3, 4]. 思路:二分。。。 我好像根本不会二分啊??? 二分查找

leetcode 81. Search in Rotated Sorted Array II (有重复元素的旋转数组找给定值)

·333 字·1 分钟
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Write a function to determine if a given target is in the array. The array may contain duplicates. 好像阿里一面的时候问过。。。 思路:肯定是二分。。。不过由于有重复元素。。。所以很恶心。。。

leetcode 74. Search a 2D Matrix

·183 字·1 分钟
题目链接 题意:给一个二维数组。。。每一行每一列都分别递增。。问某个value是否出现过。。。 思路:单调。。显然二分。。。唯一的技巧是从右上角开始搜。 1/* *********************************************** 2Author :111qqz 3Created Time :2017年03月09日 星期四 19时03分07秒 4File Name :74.cpp 5************************************************ */ 6class Solution { 7 8public: 9 10 bool searchMatrix(vector<vector<int>>& matrix, int target) { 11 12 int n = matrix.size(); 13 if (n==0) return false; 14 int m = matrix[0].size(); 15 if (m==0) return false; 16 int row = 0 ; 17 int col = m-1; 18 while (col>=0&&row<n) 19 { 20 if (matrix[row][col]==target) return true; 21 else 22 if (matrix[row][col]>target) col--; 23 else row++; 24 } 25 return false; 26 27 } 28 29};