public class Solution {
/*
* @param nums: An integer array sorted in ascending order
* @param target: An integer
* @return: An integer
*/
public int findPosition(int[] nums, int target) {
// write your code here
if (nums.length = 0) {
return -1;
}
int start = 0;
int end = num.length - 1;
int mid;
while (start + 1 < end) {
mid = start + (end - start) / 2;
if (nums[mid] == target) {
end = mid;
} else if (nums[mid] < target) {
start = mid;
} else if (nums[mid] > target) {
end = mid;
}
}
if (nums[start] == target) {
return start;
}
if (nums[end] == target) {
return end;
}
}
return -1;
}
答案 0 :(得分:2)
您的代码似乎有错误,如
if (nums.length == 0) {
return -1;
}
应该有benn
int end = num.length - 1;
另外
int end = nums.length - 1;
应该是
public int findPosition(int[] nums, int target) {
// write your code here
if (nums.length == 0) {
return -1;
}
int start = 0;
int end = nums.length - 1;
int mid;
while (start + 1 < end) {
mid = start + (end - start) / 2;
if (nums[mid] == target) {
end = mid;
} else if (nums[mid] < target) {
start = mid;
} else if (nums[mid] > target) {
end = mid;
}
}
if (nums[start] == target) {
return start;
}
if (nums[end] == target) {
return end;
}
return -1;
}
您的错误代码看起来就像。
{{1}}