classSolution { public: intgetNumberOfK(vector<int>& nums , int k){ if (nums.empty()) return0; int l = 0, r = nums.size() - 1; // 左边界 while (l < r) { int mid = l + r >> 1; if (nums[mid] < k) { l = mid + 1; } else { r = mid; } } if (nums[r] != k) return0; int left = r;
// 右边界 l = 0, r = nums.size() - 1; while (l < r) { int mid = l + r + 1 >> 1; if (nums[mid] > k) { r = mid - 1; } else { l = mid; } }