/* 题目: 寻找递增数组0~n-1中缺失的数字。 */ /* 思路: 变形二分法。 */ #include<iostream> #include<cstring> #include<vector> #include<algorithm> #include<map> using namespace std; int getMissingNumber(vector<int> &A, int n){ int start = 0; int end = n - 1; int mid = (start + end) / 2; while(start < end){ if(mid == A[mid]){ start = mid + 1; }else if(mid < A[mid]){ end = mid - 1; }else{ return -1; } mid = (start + end) / 2; //cout<<start<<" "<<end<<endl; } return start; } int main(){ vector<int> a = {1,2,3,4,5,6,7}; cout<<getMissingNumber(a,8); }
原文地址:https://www.cnblogs.com/buaaZhhx/p/12088493.html
时间: 2024-10-08 07:11:56