LeetCode Search in Rotated Sorted Array 在旋转了的数组中查找

原创 2013年12月02日 08:24:08

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.

我突然想用英文解说下,展示一下我英文解说这么专业主题的功力O(∩_∩)O~,都搞双语的话好像又太费时间了。

This is a classic interview question. It's solution is similar with normal binary search. The only difference is that we need to add more conditional sentences.

和普通的二分法差不多

The key points is when we divide the array into two half, we need to compare A[mid] with one of the end element of the array, so that we can know which half of the array is sorted, and which half is rotated, and divided them again subsequently.

关键是会增加条件判断

The difference I do here is that I add one normal binary search here as  a helper function. Actually you don't need the binary search function, just one function will be alright. But that's fun to add them together to check the difference between them.

增加普通的binary search对比一下

 

class Solution {
public:
	int search(int A[], int n, int target) 
	{
		return unordBiSearch(A, 0, n-1, target);
	}

	int unordBiSearch(int A[], int low, int up, int tar)
	{
		if (low > up) return -1;
		
		int mid = (low+up)>>1;
		if (A[mid] == tar)
			return mid;
		if (A[mid]>A[up])
		{
			if (A[low] <= tar && A[mid] > tar)
				return biSearch(A, low, mid-1, tar);
			else return unordBiSearch(A, mid+1, up, tar);
		}
		if (A[mid]<A[up])
		{
			if (A[mid] < tar && A[up] >= tar)
				return biSearch(A, mid+1, up, tar);
			else return unordBiSearch(A, low, mid-1, tar);
		}
		return -1;
	}

	int biSearch(int A[], int low, int up, int key)
	{
		if(low>up) return -1;  
		int mid = (low+up)>>1;  
		if (key < A[mid])  
			return biSearch(A, low, mid-1, key);  
		else if (A[mid] < key)  
			return biSearch(A, mid+1, up, key);  
		return mid; 
	}
};


 

 

//2014-1-26 update
	int search(int A[], int n, int target) 
	{
		return biSearch(A, 0, n-1, target);
	}

	int biSearch(int A[], int low, int up, int tar)
	{
		if (low > up) return -1;
		int mid = low + ((up-low)>>1);
		if (tar == A[mid]) return mid;

		if (A[low] <= A[mid])
		{
			//容易错漏了A[low]<=tar的条件,容易遗漏等号
			if (A[low] <= tar && tar < A[mid]) return biSearch(A, low, mid-1, tar);
			else return biSearch(A, mid+1, up, tar);
		}
		else
		{
			//容易错漏
			if (A[mid] < tar && tar <= A[up]) return biSearch(A, mid+1, up, tar);
			else return biSearch(A, low, mid-1, tar);
		}
		return -1;
	}

 

 

版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者允许不得转载。

相关文章推荐

LeetCode | 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 m...

leetcode 81. Search in Rotated Sorted Array II-旋转数组|二分查找

原题链接:81. Search in Rotated Sorted Array II 【思路-Java、Python】 二分查找实现 本题采用二分法实现,但是比较挠头的是边界问题,而且元素有重复,...

leetCode 33.Search in Rotated Sorted Array(排序旋转数组的查找) 解题思路和方法

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

【LeetCode刷题】旋转数组的查找 Search in Rotated Sorted Array

原文:http://www.dy1280.com/thread-685-1-1.html     描述 Suppose a sorted array is rotated at some pi...
  • share_4
  • share_4
  • 2014年10月06日 13:30
  • 305

【LeetCode-面试算法经典-Java实现】【033-Search in Rotated Sorted Array(在旋转数组中搜索)】

【033-Search in Rotated Sorted Array(在旋转数组中搜索)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Suppose a sorted...

leetcode之Search in Rotated Sorted Array,剑指offer之旋转数组的最小数字

输入一个递增数组的旋转,

【LeetCode】Search in Rotated Sorted Array旋转数组问题总结

// Date : 2016.09.06 // Author : yqtao // https://github.com/yqtaowhu/**************************...

【LeetCode-面试算法经典-Java实现】【081-Search in Rotated Sorted Array II(搜索旋转的排序数组)】

【081-Search in Rotated Sorted Array II(搜索旋转的排序数组)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Follow up fo...

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刷题记—— 81. Search in Rotated Sorted Array II(在翻转数组中查找2)

一、题目叙述: Follow up for "Search in Rotated Sorted A...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode Search in Rotated Sorted Array 在旋转了的数组中查找
举报原因:
原因补充:

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