我正在解决一个 leetcode 问题,即在排序数组中查找元素的第一次和最后一次出现。因为它是一个排序数组,所以我使用二分搜索。但即使我将 ryt 参数传递给一个方法,它也会显示错误,实际参数列表和形式参数列表的长度不同。
这是我的代码,
class Solution {
public int searchRange(int[] nums, int target, boolean findStart) {
int ans = -1;
int start = 0;
int end = nums.length - 1;
while (start <= end) {
int mid = start + (end - start) / 2;
if (target < nums[mid]) {
end = mid - 1;
} else if (target > nums[mid]) {
start = mid + 1;
} else {
ans = mid;
if (findStart) {
end = mid - 1;
} else {
start = mid + 1;
}
}
}
return ans;
}
public void main(String[] args) {
int[] nums = {5, 7, 7, 8, 8, 10};
int target = 8;
int[] result = search(nums,target);
System.out.println(result);
}
public int[] search(int[] nums, int target) {
int[] result = {-1, -1};
int start = searchRange(nums, target, true);
int end = searchRange(nums, target, false);
result[0] = start;
result[1] = end;
return result;
}
}
Compile Error
Line 7: error: method searchRange in class Solution cannot be applied to given types; [in __Driver__.java]
int[] ret = new Solution().searchRange(param_1, param_2);
^
required: int[],int,boolean
found: int[],int
reason: actual and formal argument lists differ in length