public int Search(int[] nums, int target) {
if (nums.Length == 0) return 0;
//5, 7, 7, 8, 8, 10
//根据两分法找到顺序数组中10的下标 减去 数组中7的下标
return helper(nums, target) - helper(nums, target - 1);
}
public int helper(int[] nums, int target) {
int l = 0, r = nums.Length-1;
while (l <= r) {
int m = (l + r) / 2;
if (nums[m] <= target) l = m + 1;
else r = m - 1;
}
return l;
}
相关文章
- 01-2353-1