zoukankan      html  css  js  c++  java
  • Searching in a rotated and sorted array

    Given a sorted array that has been rotated serveral times. Write code to find an element in this array. You may assume that the array was originally sorted in increasing order.

    思路:first to know that to ratate an array multiple times is no different than to rotate it just once.

    No matter how the array has been rotated, it would fall into 3 kinds of situations below:

    #1: If array[low] < array[mid], then left side is ordered normally.

    #2: If array[mid] < array[high], then right side is ordered normally.

    #3: array[mid] = array[low] = array[high], this might occur only when duplicates are allowed. In this condition, we have to search both sides because we don't know which side is ordered normally.

    Note: a little tricky thing is that when array has exactly 2 elements, the above logic may go wrong, so just consider this situation separately.

  • 相关阅读:
    rgbdslam 源代码的实现
    键值对排序并MD5加密
    字符编码
    排序算法
    Bridge Pattern (桥接模式)
    Visitor Pattern 和 doubledispatch
    栈、队列、链表
    如何使用visio
    架构师论文
    英语写作句子
  • 原文地址:https://www.cnblogs.com/Antech/p/3832935.html
Copyright © 2011-2022 走看看