1. 程式人生 > >統計一個數字在排序陣列中出現的次數。(牛客劍指offer)

統計一個數字在排序陣列中出現的次數。(牛客劍指offer)

題目描述:

統計一個數字在排序陣列中出現的次數

class Solution {
public:
    int GetNumberOfK(vector<int> data ,int k) {
    }
}; 

Solution :來自高贊通過查詢k+0.5和 k-0.5 確定k左右兩數的位置,求得k的出現的次數(float 好巧秒!!以後做題多用腦子了)

Solution2: 不管大小序(牛客預設的是從小到大),多個while前的判斷和abs()

Solution3: 沒封裝在類的那個,用的依然是高讚的思路,但是採取了兩個search取別查詢k左右兩數的不同

這就是樓主的巧妙之處 ,通過浮點數,避免了重複元素導致前後兩數的不一致性 ,尋找前一個數應該是最後<k的整數,而尋找後面一個則應該尋找第一個>k的整數

最後註釋裡的Solution 暴力233

 

#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
class Solution {
public:
    int GetNumberOfK(vector<int> data ,int k) {
        return biSearch(data, k+0.5) - biSearch(data, k-0.5) ;
    }
private:
    int biSearch(const vector<int> & data, double num){
        int s = 0, e = data.size()-1;
        while(s <= e){
            int mid = (e - s)/2 + s;
            if(data[mid] < num)
                s = mid + 1;
            else if(data[mid] > num)
                e = mid - 1;
        }
        return s;
    }
};
class Solution2 {
public:
    int GetNumberOfK(vector<int> data ,int k) {
           return abs(biSearch(data, k+0.5) - biSearch(data, k-0.5)) ;
    }
private:
    int biSearch(const vector<int> & data, double num){
        int s = 0, e = data.size()-1;
        if(data[s]<=data[e]){
            while(s<=e){
                int mid=(e-s)/2+s;
                if(data[mid]<num)
                    s=mid+1;
                else
                if(data[mid]>num)
                    e=mid-1;
            }
            return s;
        }
        else{
            while(s<=e){
                int mid=(e-s)/2+s;
                if(data[mid]<num)
                    e=mid-1;
                else
                if(data[mid]>num)
                    s=mid+1;
            }
            return s;
        }
    }

};

int biSearch1(const vector<int> &data, int num){
        int s = 0, e = data.size()-1;
        while(s <= e){
            int mid = (e - s)/2 + s;
            if(data[mid]<=num)
                s = mid + 1;
            else if(data[mid]>num)
                e = mid - 1;
        }
        return s;
}
int biSearch2(const vector<int> &data, int num){
        int s = 0, e = data.size()-1;
        while(s <= e){
            int mid = (e - s)/2 + s;
            if(data[mid]<num)
                s = mid + 1;
            else if(data[mid]>=num)
                e = mid - 1;
        }
        return s;
}

int GetNumberOfK(vector<int> &data ,int k) {
        return biSearch2(data, k+1) - biSearch1(data, k-1) ;
}


int main()
{
    vector<int> a={1,2,2,2,2,3,3,3,4,4,4,7};
// ******************* Test 一 *************************8
    Solution sl;
    int ans1=sl.GetNumberOfK(a,7);
    cout<<"Test by find float k+0.5 and k-0.5:";
    cout<<ans1<<endl;
// ******************** Test 二 ********************
    cout<<"Test by find int k+1 and k-1:";
    int ans=GetNumberOfK(a,1);
    cout<<ans<<endl;
    cout << "Hello world!" << endl;
// ******************* Test 三 ********************
    vector<int> b={9,8,7,6,6,5,5,4,4,3,3,2,2,1,1};
    Solution2 sl2;
    int ans2=sl2.GetNumberOfK(b,9);
    cout<<"Test by find float k+0.5 and k-0.5(不管大小序):";
    cout<<ans2<<endl;

    return 0;
}
/*腦回路簡單了點 以後刷題得多想想了
class Solution {
public:
    int GetNumberOfK(vector<int> data ,int k) {
        /*int ans=0;
        for(int i=0;i<data.size();){
            if(data[i]!=k){
                i++;
                if(ans!=0)return ans;
                continue;
            }
            else{
                ans++;
                i++;
            }
        }
        return ans;
    }*/
        /*int start=0,end=data.size()-1;
        while(start<end){
            mid=(end-start)/2+start;
            if(data[mid]!=k);
        }*/