intexist(int arr[],int length,int num){if(arr ==NULL|| length ==0){return0;}int L =0;int R = length -1;int M =0;while(L <= R){M =(L + R)/2;if(arr[M]== num){return1;}elseif(arr[M]< num){L = M +1;}else{R = M -1;}}return0;}
在有序数组中找>=num的最左位置
Java实现
publicstaticintfindLeftIndex(int[] arr,int num){intL=0;intR= arr.length -1;intM=0;intIndex=-1;while(L<=R){M=(L+R)/2;// 数组长度很长的时候有溢出风险// M = L + (R - L) / 2;// M = L + ((R - L) >> 1);if(arr[M]>= num){Index=M;R=M-1;}else{L=M+1;}}returnIndex;}
C语言实现
intfindLeftIndex(int arr[],int length,int num){int R = length -1;int L =0;int M =0;int Index =-1;while(L <= R){M =(R + L)/2;if(arr[M]>= num){Index = M;R = M -1;}else{L = M +1;}}return Index;}
intfindRightIndex(int arr[],int length,int num){int R = length -1;int L =0;int M =0;int Index =-1;while(L <= R){M =(R + L)/2;if(arr[M]<= num){Index = M;L = M +1;}else{R = M -1;}}return Index;}
检索销量表中销量最好的商品id和销量,下列SQL语句正确的是()
A. SELECT 商品id,销量 FROM 销量表 WHERE 销量MAX(销量)
B. SELECT 商品id,MAX(销量) FROM 销量表 GROUP BY 销量
C. SELECT 商品id,MAX(销量) FROM 销量表 GROUP BY 商品id
…