在旋转后有序数组中查找指定数(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 篇原创文章 · 获赞 18 · 访问量 9万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 创作都市 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览