classSolution{public:/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums int整型vector
* @param target int整型
* @return int整型
*/intsearch(vector<int>& nums,int target){// write code hereint l =0;int r = nums.size()-1;while(l<=r){int m =(l + r)/2;if(nums[m]== target)return m;if(nums[m]< target)
l = m+1;else
r = m-1;}return-1;}};
18 二维数组中的查找
classSolution{public:boolFind(int target, vector<vector<int>> array){if(array.size()==0)returnfalse;int m = array.size();int n = array[0].size();int x = m-1;int y =0;while(x>=0&& y <=n-1){if(array[x][y]== target)returntrue;if(array[x][y]< target)
y++;else
x--;}returnfalse;}};
19 寻找峰值
classSolution{public:/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums int整型vector
* @return int整型
*/intfindPeakElement(vector<int>& nums){// write code hereint l =0;int r = nums.size()-1;while(l < r){int m =(l+r)/2;if(nums[m]> nums[m+1]) r = m;else l = m +1;}return r;}};
20 数组中的逆序对
classSolution{public:int mod =1000000007;int* mydata;intawm(vector<int>&data,int l,int r){if(l >= r)return0;int m =(l + r)/2;int ans =awm(data, l, m)+awm(data, m +1, r);
ans %= mod;int p1 = l;int p2 = m +1;for(int i = l; i <= r; i++){if(p1 == m +1){
mydata[i]= data[p2++];}elseif(p2 == r +1){
mydata[i]= data[p1++];}else{if(data[p1]> data[p2]){
ans += m - p1 +1;
ans %= mod;
mydata[i]= data[p2++];}else{
mydata[i]= data[p1++];}}}for(int i = l; i <= r ; i++){
data[i]= mydata[i];}return ans % mod;}intInversePairs(vector<int> data){int n = data.size();
mydata =newint[n+10];returnawm(data,0, n -1);}};
21 旋转数组的最小数字
classSolution{public:intminNumberInRotateArray(vector<int> rotateArray){int l =0;int r = rotateArray.size()-1;while(l<r){int m =(l + r)/2;if(rotateArray[m]> rotateArray[r]){
l = m+1;}elseif(rotateArray[m]== rotateArray[r]){
r--;}else{
r = m;}}return rotateArray[r];}};