在旋轉後有序數組中查找指定數(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.

給定一個從某個位置做了旋轉後的有序數組,(比如0 1 2 4 5 6 7,旋轉後變成4 5 6 7 0 1 2)。給定一個值,在這個數組裏面做搜索,找到就返回對應的位置,否則返回-1。

可以假定沒有重複數據存在。

分析

正常情況下,我們拿到的是一個有序數組,然後用二分查找就可以了,時間複雜度可以達到O(logn)。但是這個題目說的是有序數組還做了旋轉,那能不能還用二分查找呢?答案是肯定的,我們可以看到旋轉後的二維數組,從某個位置往左和往右都是有序的。

可以簡單的先判斷搜到中間節點和起始節點及終止節點的關係,之後再進行搜索。

可以分別採用遞歸調用和迭代循環進行解決。

代碼

/**************************************
* 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.
**************************************/
#include <iostream>
#include <vector>
using namespace std;

class Solution {
private:
	int search2Aux(const vector<int> &nums, int target, int first, int last) {
		if (first <= last) {
			int middle = (first + last) / 2;
			if (nums[middle] == target) {
				return middle;
			} else if (nums[first] <= nums[middle]) {
				if (nums[first] <= target && target < nums[middle]) {
					return search2Aux(nums, target, first, middle - 1);
				} else {
					return search2Aux(nums, target, middle + 1, last);
				}
			} else {
				if (nums[middle] < target && target <= nums[last]) {
					return search2Aux(nums, target, middle + 1, last);
				} else {
					return search2Aux(nums, target, first, middle - 1);
				}
			}
		} else {
			return -1;
		}
	}
public:
	/* Time: O(logn), Space: O(1) */
	int search1(const vector<int> &nums, int target) {
		int first = 0;
		int last = nums.size() - 1;
		while (first <= last) {
			int middle = (first + last) / 2;
			
			if (nums[middle] == target) {
				return middle;
			} else if (nums[first] <= nums[middle]) {
				if (nums[first] <= target && target < nums[middle]) {
					last = middle - 1;
				} else {
					first = middle + 1;
				}
			} else {
				if (nums[middle] < target && target <= nums[last]) {
					first = middle + 1;
				} else {
					last = middle - 1;
				}
			}
		}
		
		return -1;
	}
	
	/* Time: O(lgn), Space: O(1) */
	int search2(const vector<int> &nums, int target) {
		return search2Aux(nums, target, 0, nums.size() - 1);
	}
};

int main(void) {
	Solution* s = new Solution();
	vector<int> nums;
	nums.push_back(7);
	nums.push_back(9);
	nums.push_back(10);
	nums.push_back(0);
	nums.push_back(1);
	nums.push_back(2);
	nums.push_back(3);
	nums.push_back(5);
	
	cout << "Solution 1: " << s->search1(nums, 6) << endl;
	cout << "Solution 1: " << s->search1(nums, 4) << endl;
	cout << "Solution 1: " << s->search1(nums, 8) << endl;
	cout << "Solution 1: " << s->search1(nums, 9) << endl;
	cout << "Solution 1: " << s->search1(nums, 3) << endl;
	
	cout << "Solution 2: " << s->search2(nums, 6) << endl;
	cout << "Solution 2: " << s->search2(nums, 4) << endl;
	cout << "Solution 2: " << s->search2(nums, 8) << endl;
	cout << "Solution 2: " << s->search2(nums, 9) << endl;
	cout << "Solution 2: " << s->search2(nums, 3) << endl;
	
	delete s;
	return 0;
}

 

發佈了84 篇原創文章 · 獲贊 19 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章