题目:

  判断数字x在有序数组出现的次数

  

 1 class Solution {
 2 public:
 3     int GetNumberOfK(vector<int> data ,int k) {
 4         if(!data.size()) return 0;
 5         cout << upper_bound(data, k) << endl;
 6         return upper_bound(data, k) - lower_bound(data, k);
 7     }
 8     int lower_bound(vector<int> data ,int k) {
 9         int l=0, r=data.size();
10         while(l<r){
11             int mid = (l+r)/2;
12             if(data[mid]<k)
13                 l = mid +1;
14             else
15                 r = mid;
16         }
17         return l;
18     }
19    int upper_bound(vector<int> data ,int k) {
20        int l=0, r=data.size();
21         while(l<r){
22             int mid = (l+r)/2;
23             if(data[mid]<=k)
24                 l = mid + 1;
25             else
26                 r = mid;
27         }
28         return l;
29    }
30 };