Search Insert Position

来源:互联网 发布:九仙图坐骑进阶数据 编辑:程序博客网 时间:2024/04/29 18:02

Reimplement C++ STL lower_bound

class Solution {public:    int searchInsert(int A[], int n, int target) {        return lower_bound(A, 0, n-1, target);    }        int lower_bound(int A[], int first, int last, int target)    {        while(first <= last)        {            int mid = first + (last-first)/2;            if(A[mid] < target)                first = mid + 1;            else if(A[mid] >= target)                last = mid - 1;        }        return last+1;    }};

Reference: Leetcode "Search for a Range"

0 0
原创粉丝点击