给定一个长度为 n 的非降序数组和一个非负数整数 k ,要求统计 k 在数组中出现的次数
数据范围:,数组中每个元素的值满足
要求:空间复杂度 ,时间复杂度
要求:空间复杂度 ,时间复杂度
//1是右边界,0是找左边界 int search_boundary(int direction,int* data, int dataLen, int k ) { int l = 0,r=dataLen-1; while(l<=r) { int mid = (l+r)/2; if(direction==1) { if(data[mid]<=k) l=mid+1; else r=mid-1; } else { if(data[mid]>=k) r=mid-1; else l=mid+1; } } if(direction==1 && data[r] == k) return r+1; //多加一个好算总数 else if(direction==0 && data[l] == k) return l; return 0; } int GetNumberOfK(int* data, int dataLen, int k ) { // write code here if(data == NULL || dataLen == 0) return 0; return search_boundary(1,data,dataLen,k)-search_boundary(0,data,dataLen,k); }
时间复杂度O(n),极限情况下遍历完整的数组
int GetNumberOfK(int* data, int dataLen, int k ) { // write code here if(dataLen==0 || k<data[0] || k>data[dataLen-1]) return 0; int left=0,right=dataLen-1; while(left <= right && (data[left] != k || data[right] != k)) { // printf("left:%d,right:%d\n", left, right); int mid = left+(right-left)/2; if(data[mid]<k) left=mid+1; else if(data[mid]>k) right=mid-1; else if(data[mid]==k){ if(data[left]<k) left++; if(data[right]>k) right--; } } return right-left+1; }
void Dichotomy_check(int *a,int*count,int key,int left,int right) { if(left>right) return; int mid=(left+right)/2; if(a[mid]>key)//那么一定在左区间,更新区间 { right=mid-1; Dichotomy_check(a, count, key, left, right); } else if(a[mid]<key)//一定在右区间,更新区间 { left=mid+1; Dichotomy_check(a, count, key, left, right); } else//左右区间都有可能 { (*count)++; Dichotomy_check(a, count, key, left, mid-1); Dichotomy_check(a, count, key, mid+1, right); } } int GetNumberOfK(int* data, int len, int k ) { // write code her int*count=(int*)malloc(4);//用来记录符合条件的k的次数 *count=0; int left=0; int right=len-1; Dichotomy_check(data, count, k, left, right);//采用分治的思想,分别对左右区间进行查找 int tmp_count=*count; free(count); count=NULL; return tmp_count; }
int GetNumberOfK(int* data, int dataLen, int k ) { // write code here int left=0; int count=0; int right=dataLen-1; while(left<=right) { int mid=(right+left)/2; if(data[mid]<k) left=mid+1; else if(data[mid]>k) right=mid-1; else if(data[mid]==k) { int start=mid+1; count=0; //左边 while(mid>=0&&data[mid]==k) { count++; mid--; } //右边 while(start<dataLen&&data[start]==k) { count++; start++; } break; } } return count; }
int GetNumberOfK(int* data, int dataLen, int k ) { int left = 0; int right = dataLen - 1; int count = 0; while(left <= right) { int mid = (left + right) / 2; if (data[mid] > k) right = mid - 1; else if (data[mid] < k) left = mid + 1; else { count++; //记录找到的位置 int tmp = mid; //从该位置向左找,直到找到不同数 while(--tmp >= 0 && data[tmp] == k) { count++; } tmp = mid; //从该位置向右找,直到找到不同数 while (++tmp < dataLen && data[tmp] == k) { count++; } break; } } return count; }
/** * * @param data int整型一维数组 * @param dataLen int data数组长度 * @param k int整型 * @return int整型 * * C语言声明定义全局变量请加上static,防止重复定义 */ int GetNumberOfK(int* data, int dataLen, int k ) {// write code here int n = 0; for (int i = 0; i < dataLen; i++) { if (*(data + i) == k) { n++; } } return n; }
int GetNumberOfK(int* data, int dataLen, int k ) { int l=0,r=dataLen-1,mid; while(l<=r){ mid=(l+r)/2; if(data[mid]>k) r=mid-1; else if(data[mid]<k) l=mid+1; else{ l=mid-1; r=mid+1; while(l>=0&&data[l]==k) l--; while(r<dataLen&&data[r]==k) r++; return r-l-1; } } return 0; }
int find(int*data,int len,int k,int flag) { int left=0,right=len-1,mid; while(left<=right){ mid=left+(right-left)/2; if(data[mid]>k) right=mid-1; else if(data[mid]<k) left=mid+1; else{ if(flag==0){//flag==0,找最左边数字, if(mid==left ||data[mid-1]!=k) return mid; else right=mid-1;//把中心向左推 }else { if(mid==right||data[mid+1]!=k) return mid; else left=mid+1; } } } return -1; } int GetNumberOfK(int* data, int dataLen, int k ) { // write code here if(dataLen==0) return 0; int left=find(data,dataLen,k,0); int right=find(data,dataLen,k,1); if(left==-1&&right==-1) return 0;//没找到 return right-left+1; }
int GetNumberOfK(int* data, int dataLen, int k ) { // write code here int i = 0; int j = 0; for(i=0;i < dataLen;i++) { if(*(data+i)==k) j++; } return j; }
int GetNumberOfK(int* data, int dataLen, int k ) { int left = 0, right = dataLen-1; int count = 0; while(left <= right) { if(data[left] == k) { left++, count++; } else if(data[right] == k) { right--, count++; } else { left++, right--; } } return count; }