zoukankan      html  css  js  c++  java
  • LeetCode153 Find Minimum in Rotated Sorted Array. LeetCode162 Find Peak Element

    二分法相关

    153. Find Minimum in Rotated Sorted Array

    Suppose a sorted array 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).

    Find the minimum element.

    You may assume no duplicate exists in the array. (Medium)

    分析:

    根据旋转排序数组性质,问题转化为find first element which is bigger than nums[nums.size() - 1];

    代码:

     1 class Solution {
     2 public:
     3     int findMin(vector<int>& nums) {
     4         int start = 0, end = nums.size() - 1;
     5         int target = nums[nums.size() - 1];
     6         while(start + 1 < end){
     7             int mid = start + (end - start) / 2;
     8             if(nums[mid] > target){
     9                 start = mid;
    10             }
    11             else{
    12                 end = mid;
    13             }
    14         }
    15         if(nums[start] < target){
    16             return nums[start];
    17         }
    18         return nums[end];
    19     }
    20 };

    162. Find Peak Element

    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. (Medium)

    分析:

    因为题目假设了num[-1] = num[n] = - 所以对于nums[mid]来讲,其与左右元素之间无非只有三种情况:

    (1) nums[mid-1] < nums[mid] && nums[mid] > nums[mid+1] ,此时mid即为peak element;

    (2) nums[mid-1] < nums[mid] && nums[mid] < nums[mid+1],此时删除数组左半部分仍然可保证有peak element, mid = start;

    (3)nums[mid-1] > nums[mid] && nums[mid] > nums[mid+1], 此时删除数组左半部分仍然可保证有peak element,mid = end;

    (4)在波谷位置,随意左右均可。

     

     1 class Solution {
     2 public:
     3     int findPeakElement(vector<int>& nums) {
     4         int start = 0, end = nums.size() - 1;
     5         while(start + 1 < end ){
     6             int mid = start + (end - start) / 2;
     7             if(nums[mid - 1] < nums[mid] && nums[mid] > nums[mid+1]){
     8                 return mid;
     9             }
    10             else if(nums[mid - 1] < nums[mid] && nums[mid] < nums[mid + 1]){
    11                 start = mid;
    12             }
    13             else{
    14                 end = mid;
    15             }
    16         }
    17         if(nums[start] > nums[end]){
    18             return start;
    19         }
    20         return end;
    21     }
    22 };

     

     

     

     
  • 相关阅读:
    php笔记之数组
    php笔记之函数
    vue笔记之路由
    <QT>:使用QUdpSocket进行UDP通信
    Linux下用ssh在远程Xserver上显示图形界面
    <学习QT>在QListWidget中实现每个单元项显示文字在上图片在下
    const限定符以及顶层const和底层const的理解
    了解X window以及相关知识
    Linux下使用Eclipse C/C++生成创建并调用动态库(.so)
    Linux下搭载Xwindow(Xlib)开发环境
  • 原文地址:https://www.cnblogs.com/wangxiaobao/p/6185058.html
Copyright © 2011-2022 走看看