LeetCode 33, Search in Rotated Sorted Array-----Binary Search

原创 2015年07月08日 22:37:03

LeetCode 33, Search 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).

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.

这个题目,数组是部分排序的。如果是全排序,binary Search 可以在O(lgN) 下查找的 target, 那这里其实还是 binary search,和标准的二分法只是判定条件的区别。一般二分法通过比较mid 和 target就能不断调整,直至找到target, 这里由于整个数组被折叠,从而由两个递增有序数组组成,因此判定条件更复杂。

一种算法就是通过更改一般二分法的判定条件,直接找到target:

public class Solution {
    public int search(int[] nums, int target) {
        int len=nums.length;
        int h=0, t=len-1,mid=-1;
        while(mid!=(h+t)/2){
        	mid=(h+t)/2;
        	if(nums[mid]>=nums[h]){
        		if(target>=nums[h]&&target<=nums[mid])  t=mid;
        		else h=mid;       		
        	}else{
        		if(target<=nums[t] && target>nums[mid]) h=mid;
        		else t=mid;
        	}        
        }
        if(nums[t]==target) return t;
        return -1;
    }
}

这里是通过循环实现了这个算法,写成递归当然也是可以的。

其实一开始,我先想到的算法比较复杂,并没有这个好,时间复杂度为2*lgN。思路是,先找到有序数组反转的折点,然后对两部分分别利用二分查找。寻找折叠点,其实用的还是binary search 的思想, 对两个有序子序列进行binary search可以直接调用Arrays.binarySearch() 来完成。代码如下:

import java.util.*;
public class Solution {	
	public int search(int[] nums, int target) {
		int len=nums.length;
		if(len==0) return -1;
		int h=0,t=len-1,mid=-1;
		while(mid!=(h+t)/2){
			mid=(h+t)/2;
			if(nums[mid]<nums[h]) t=mid;
			else h=mid;
		}
		int index=Arrays.binarySearch(nums,0,t,target);
		if(index>=0) return index;
		index=Arrays.binarySearch(nums,t,len,target);
		if(index>=0) return index; 
		return -1;
	}
}
 

这个Solution 比上面的慢一点,而且代码量也没上面少,因此并不是很好。
版权声明:本文为博主原创文章,请随便转载

LeetCode Algorithms 33. Search in Rotated Sorted Array

题目难度: Medium 原题描述: Suppose an array sorted in ascending order is rotated at some pivot unk...

leetcode-33-Search in Rotated Sorted Array

#include #include using namespace std; class Solution { public: /* 旋转数组只能有三种情况: 1. 左...

Leetcode33 Search in Rotated Sorted Array

Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you befo...

【leetcode】No.33 Search 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 mig...

LeetCode 33 Search in Rotated Sorted Array 题解

题意简述:给定一个按升序排序的数组,这个数组进行了数次旋转(例如数组[0,1,2,4,5,6,7]在2和4之间执行一次旋转就会变成[4,5,6,7,0,1,2]。需要搜索某个数是否在这个数组里面。数组...

leetcode 33. Search in Rotated Sorted Array

1.题目 Suppose a sorted array is rotated at some ...

[Leetcode] 33. Search 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...

LeetCode 33. Search in Rotated Sorted Array

问题描述: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 ...

Leetcode 33. Search in Rotated Sorted Array

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e.,...

【leetcode】33. Search in Rotated Sorted Array

Difficulty:hard Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i....
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode 33, Search in Rotated Sorted Array-----Binary Search
举报原因:
原因补充:

(最多只允许输入30个字)