题解 | #数字在升序数组中出现的次数#
数字在升序数组中出现的次数
https://www.nowcoder.com/practice/70610bf967994b22bb1c26f9ae901fa2
class Solution { public: int GetNumberOfK(vector<int> data ,int k) { int t=0; if(data.size()==0){ return 0; } if(k>data[data.size()-1]){ return 0; } while (data[t]<k) { t++; } if(data[data.size()-1]==k){ return data.size()-t; } int ans=0; while(data[t]==k){ ans++; t++; } return ans; } };