c++二分查找

    xiaoxiao2024-12-22  4

    int searchInsert(vector<int>& nums, int target) { /*从小到大排序,进行二分查找*/ int head = 0, tail = nums.size() - 1; while (tail >= head) { if (target < nums[(tail + head) / 2]) tail = (tail + head) / 2 - 1; else if (target > nums[(tail + head) / 2]) head = (tail + head) / 2 + 1; else return (tail + head) / 2; } return -1; }

    c++进行二分查找代码

    最新回复(0)