publicint[] searchRange(int[] nums, int target) { int left = searchLeft(nums, target); int right = searchRight(nums, target); returnnewint[]{left, right}; } //在nums中寻找target的右边界 publicintsearchRight(int[] nums, int target){ int left = 0; int right = nums.length - 1; int res = -1; while(left <= right){ int mid = left + (right - left) / 2; if(nums[mid] > target) right = mid - 1; elseif(nums[mid] < target) left = mid + 1; else{ left = mid + 1; res = mid; } } return res; } //在nums中寻找target的左边界 publicintsearchLeft(int[] nums, int target){ int left = 0; int right = nums.length - 1; int res = -1; while(left <= right){ int mid = left + (right - left) / 2; if(nums[mid] > target) right = mid - 1; elseif(nums[mid] < target) left = mid + 1; else{ right = mid - 1; res = mid; } } return res; }