Search Insert Position &&Search in Rotated Sorted Array &&Search in Rotated Sorted Array II

来源:互联网 发布:拳皇2002um画质优化 编辑:程序博客网 时间:2024/05/17 20:28

Search Insert Position

Description

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

You may assume NO duplicates in the array.

Example

[1,3,5,6], 5 → 2

[1,3,5,6], 2 → 1

[1,3,5,6], 7 → 4

[1,3,5,6], 0 → 0

这题比较简单。二分查找。每次取中间的值,如果相等,则返回index,不等,则判断target大于中间值还是小于中间值。小于则取左边一半,大于则取右边一半。算法复杂度是O(logn),空间复杂度O(1)

 public int searchInsert(int[] A, int target) {        if(A == null || A.length == 0)          {              return 0;          }          int l = 0;          int r = A.length-1;          while(l<=r)          {              int mid = (l+r)/2;              if(A[mid]==target)                  return mid;              if(A[mid]<target)                  l = mid+1;              else                  r = mid-1;          }          return l;    }

这样的写法有一个好处:当while循环结束,没有找到目标值,l会恰好停在比目标值大的index上,r会恰好停留在比目标值小的index上

参考网址:http://blog.csdn.net/linhuanmars/article/details/20278967

Search in Rotated Sorted Array

Description

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

Example

For [4, 5, 1, 2, 3] and target=1, return 2.
For [4, 5, 1, 2, 3] and target=0, return -1.

这一题是上面的变体。要注意理解 ‘a sorted array is rotated’旋转有序数组。以0 1 2 4 5 6 7 为例:这个数组是一个有序数组。但是它怎么旋转能变成4 5 6 7 0 1 2 ?方法是向右旋转四位。2向右旋转四位,到7的位置。而7已经在最右边,所以会从下标0开始向右旋转四位。即到下标3的位置。这种旋转是轴承是的单方向旋转,不是以一个数字为轴,轴的左右两边交换。
旋转结果如图:
这里写图片描述

思路:
也是利用二分法。取中间值,如果是目标值,返回index。如果不是,则中间值和最右边值比较。因为是有序的,所以如果中间值小于最右边值,则右边肯定是有序的。如结果图的前四行。红线右边都是有序的。如果是有序的而且目标值在中间值和最右边值之间,则minPos=middle+1;如果不在中间值和最右边值之间,则在左边,所以maxPos=middle-1;
如果中间值大于等于最右边值,如结果图第四行。则左边肯定有序。然后再判断目标值在左边还是在右边即可。

 public int search(int[] A, int target) {        int minPos=0,maxPos=A.length-1;             while(minPos<=maxPos){                  int middle=(maxPos+minPos)>>1;            if(A[middle]==target) return middle;            if(A[middle]<A[maxPos]){                if(target<=A[maxPos] && target>A[middle]){                    minPos=middle+1;                }else{                                  maxPos=middle-1;                }            }else{                if(target<A[middle] && target>=A[minPos]){                    maxPos=middle-1;                }else{                    minPos=middle+1;                }            }        }        return -1;    }

Search in Rotated Sorted Array II

Description

Follow up for Search in Rotated Sorted Array:
What if duplicates are allowed?
Would this affect the run-time complexity? How and why?
Write a function to determine if a given target is in the array.

Example
Given [1, 1, 0, 1, 1, 1] and target = 0, return true.
Given [1, 1, 1, 1, 1, 1] and target = 0, return false.

这一题是上面题目的拓展。即多了一种可重复值得情况。
以[9,9,9,9,9,9,5,6,7,8,9],target=8为例:当第一次取中间值得时候,中间值为9,和最左边和最后边的值相同。按照上面的算法,则进入左边,结果为false.所以为了准确的判断进入左边还是右边,maxPos–.即向左移动一位。如果还是相等再向左移动,直到和中间值不等的情况。

public boolean search(int[] A, int target) {        // write your code here        int minPos=0,maxPos=A.length-1;        while(minPos<=maxPos){            int middle=(maxPos+minPos)>>1;            if(A[middle]==target) return true;            if(A[middle]<A[maxPos]){                if(target<=A[maxPos] && target>A[middle]){                    minPos=middle+1;                }else{                                  maxPos=middle-1;                }            }else{                if(middle>A[maxPos]){                    if(target<A[middle] && target>=A[minPos]){                        maxPos=middle-1;                    }else{                        minPos=middle+1;                    }                }else{                    maxPos--;                }            }        }        return false;    }

这里说明一下:我这里和中间值比较的是最右边的值,所以是如果遇到相等个情况,maxPos–;如果和中间值比较的是最左边的值,同理,只不过是minPos++;

参考网址:http://blog.csdn.net/linhuanmars/article/details/20588511

还有其他类似题目:Find Minimum in Rotated Sorted Array

原创粉丝点击