剑指offer53题目二

#include<iostream>
using namespace std;

class Solution {
public:
	int Find(int* p,int n) {
		int i = 0, j = n - 2,mid;
		while (i<=j){
			mid = (j - i) / 2 + i;
			if (p[mid] > mid) {
				j = mid - 1;
			}
			else if(p[mid] < mid){
				i = mid + 1;
			}
			else {
				if (mid<n-1&&p[mid+1]!=mid+1) {
					break;
				}
				else i = mid + 1;
			}
		}
		return mid + 1;
	}
};

int main() {
	Solution s;
	int p[6] = {0,1,2,3,5,6};
	cout<<s.Find(p,6);
	return 0;
}

阅读更多
个人分类: 剑指offer
想对作者说点什么? 我来说一句

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

关闭
关闭
关闭