c++ Binary Search


  • 0

    class Solution {
    public:
    int searchInsert(vector<int>& nums, int target) {
    int h=0,c=0,e=0,c0=123124123;;//head,end,cursor
    c=(e+h)/2;
    for( e=nums.size()-1;h<e&&c0!=c;c=(e+h)/2){
    // cout<<target<<","<<nums[e]<<endl;
    if(target<=nums[h])return h;
    if(target>nums[e])return e+1;
    if(target==nums[e])return e;
    if(target>=nums[c])h=c;
    if(target<nums[c])e=c;
    c0=c;
    }
    if(target>nums[e])return e+1;
    if(target==nums[h])return h;
    return e;
    }
    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.