【剣指Offer学習】【面接問題38:数字が並べ替えられた数】

846 ワード

https://www.nowcoder.com/questionTerminal/70610bf967994b22bb1c26f9ae901fa2?orderByHotValue=1&page=8&onlyReference=false
  // , , k , k-0.5 k+0.5 // , 。
 

int binSearch(int a[],int start,int end,double num) {
    while (start <= end) {
        int half = start + (end -start)/2;
        if (a[half] > num) {
            end = half - 1;
        }
        else if (a[half] < num) {
            start = half + 1;
        }
    }
    return start;
}

int timesOfShown(int a[], int len, int num) {
    if (a == NULL || len == 0) {
        return 0;
    }
    return binSearch(a,0,len-1,num+0.5) - binSearch(a,0,len-1,num-0.5);
}

    int times[] = {1, 2, 2, 2, 2, 2, 2, 8, 9, 10};
    int timesOf = timesOfShown(times,10,2);