二分查找
📌 在排序数组中查找数字
· ☕ 1 分钟
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 int search(int* nums, int numsSize, int target){ int low = 0, high = numsSize - 1, mid, count = 0; while(low<=high){ mid =(low + high) >> 1; if(nums[mid] == target){ count += 1; for(int i=mid-1; i>=low; i--){ if(nums[i] == target) count += 1; } for(int i=mid+1; i<=high; i++){ if(nums[i] == target) count += 1; } break; }else

📌 二分查找
· ☕ 1 分钟
1 2 3 4 5 6 7 8 9 10 11 12 13 int search(int* nums, int numsSize, int target){ int left = 0, right = numsSize-1, mid; while(left<=right){ mid = (left + right)/2; if(target < nums[mid]) right = mid - 1; else if(target > nums[mid]) left = mid + 1; else return mid; } return -1; }