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(33). Search in Rotated Sorted Array

problem Suppose an array sorted in ascending order is rotated at some pivot unknown to you befo...

LeetCode OJ 33 Search in Rotated Sorted Array

难度:hard 由此可见leetcode的hard难度比较于acm是偏简单一些的。 Suppose a sorted array is rotated at some pivot unkn...

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

leetcode解题之33&81. Search in Rotated Sorted Array java版(在旋转的数字中查找指定值)

33. Search in Rotated Sorted Array ,81. Search in Rotated Sorted Array II 。 leetcode解题之33&81. Search...

[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 mig...

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

leetcode 33. Search in Rotated Sorted Array

题目大意:一个排序数组绕某个数选择了,在这个旋转后的数组中找指定的元素target 找到就返回在数组中的序号,否则返回-1 解题思路: 1、当然是可以直接搜的(复杂度O(n)),也可以过.但是对于有一...

Leetcode 33. Search in Rotated Sorted Array

题目如下: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand...

Leetcode 33 - Search in Rotated Sorted Array(二分)

题意给一个有序数组,然后从数组的某一个位置,将前后两半交换。现在给一个数k,求k在数组中出现的位置,如果没有出现,返回-1。思路算法1画个图,其实就两种情况:前半段长还是后半段长,然后分若干种情况去讨...
  • Lzedo
  • Lzedo
  • 2017-02-13 16:44
  • 88

【C++】【LeetCode】33. Search in Rotated Sorted Array

题目Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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