def erfenfa(nums,target): # python
l,r = 0,len(nums)-1
while (l < r):
mid = (l + r) // 2
if target < nums[mid]:
r = mid - 1
elif target > nums[mid]:
l = mid + 1
else:
return mid
return -1
if __name__ == "__main__":
nums = [-1, 0, 3, 5, 9, 12]
target = 6
print(erfenfa(nums,target))
#include <iostream> //C++
using namespace std;
int erfenfa(int nums[], int target,int n) {
int l = 0;
int r = n - 1; //c++中,如果将数组作为参数传递,则在函数中是无法获得数组的大小的
while(l <= r){
int mid = (l + r) / 2;
if (nums[mid] < target) {
l = mid + 1;
}else if(nums[mid] > target) {
r = mid - 1;
}
else {
return mid;
}
}
return -1;
}
int main() {
int nums[6] = { -1, 0, 3, 5, 9, 12 };
int target = 9;
int n = sizeof(nums)/sizeof(nums[0]);
cout << erfenfa(nums,target,n) << endl;
}
def erfenfa(nums,target): #python
leftBoder = getLeftBorder(nums, target)
rightBoder = getRightBorder(nums, target)
# 情况一
if leftBoder == -2 or rightBoder == -2: return [-1, -1]
# 情况三
if rightBoder - leftBoder > 1: return [leftBoder + 1, rightBoder - 1]
# 情况二
return [-1, -1]
def getRightBorder(nums, target):
left, right = 0, len(nums) - 1
rightBoder = -2 # 记录一下rightBorder没有被赋值的情况
while left <= right:
middle = left + (right - left) // 2
if nums[middle] > target:
right = middle - 1
else: # 寻找右边界,nums[middle] == target的时候更新left
left = middle + 1
rightBoder = left
return rightBoder
def getLeftBorder(nums, target):
left, right = 0, len(nums) - 1
leftBoder = -2 # 记录一下leftBorder没有被赋值的情况
while left <= right:
middle = left + (right - left) // 2
if nums[middle] >= target: # 寻找左边界,nums[middle] == target的时候更新right
right = middle - 1
leftBoder = right
else:
left = middle + 1
return leftBoder
if __name__ == "__main__":
nums = [-1, 0, 3, 5, 9]
target = 5
print(erfenfa(nums, target))
#include <iostream> //C++
using namespace std;
int getRightBorder(int nums[], int target, int n);//函数声明
int getLeftBorder(int nums[], int target, int n);
int erfenfa(int nums[], int target,int n) {
int rightBorder = getRightBorder(nums, target, n);
int leftBorder = getLeftBorder(nums, target, n);
// 情况一
if (leftBorder == -2 || rightBorder == -2) return -1,-1;
// 情况三
if (rightBorder - leftBorder > 1) return leftBorder + 1, rightBorder - 1;
// 情况二
return -1, -1;
}
int getRightBorder(int nums[], int target,int n) {
int left = 0;
int right = n - 1;
int rightBorder = -2; // 记录一下rightBorder没有被赋值的情况
while (left <= right) { // 当left==right,区间[left, right]依然有效
int middle = left + ((right - left) / 2);// 防止溢出 等同于(left + right)/2
if (nums[middle] > target) {
right = middle - 1; // target 在左区间,所以[left, middle - 1]
}
else { // 当nums[middle] == target的时候,更新left,这样才能得到target的右边界
left = middle + 1;
rightBorder = left;
}
}
return rightBorder;
}
int getLeftBorder(int nums[], int target, int n) {
int left = 0;
int right = n - 1;
int leftBorder = -2; // 记录一下leftBorder没有被赋值的情况
while (left <= right) {
int middle = left + ((right - left) / 2);
if (nums[middle] >= target) { // 寻找左边界,就要在nums[middle] == target的时候更新right
right = middle - 1;
leftBorder = right;
}
else {
left = middle + 1;
}
}
return leftBorder;
}
int main() {
int nums[6] = { -1, 0, 3, 5, 9, 12 };
int target = 9;
int n = sizeof(nums)/sizeof(nums[0]);
cout << erfenfa(nums, target, n) << endl;
}
个人刷题记录整理,欢迎随时纠错和讨论!