美文网首页
167-two-sum-ii-input-array-is-so

167-two-sum-ii-input-array-is-so

作者: Twopothead | 来源:发表于2019-07-21 18:32 被阅读0次
    int binarysearch(int* nums, int numsSize, int target){
        int lo = 0,hi = numsSize-1;
        int mid = 0;
        while (lo<=hi)
        {
            mid = (lo+hi)/2;
            if(target == nums[mid])
                return mid;
            if(target < nums[mid])
                hi = mid -1;
            else
                lo = mid +1;
        } 
        return -1;
    }
    
    /**
     * Note: The returned array must be malloced, assume caller calls free().
     */
    int* twoSum(int* numbers, int numbersSize, int target, int* returnSize){
        *returnSize = 2;
        int index = 0;
        int *r = (int *)malloc((*returnSize)*sizeof(int));
        for(int i=0;i<numbersSize;i++){
            index = binarysearch(numbers,numbersSize,target-numbers[i]);
            if(i>index)
                index = -1;
            if(i==index){
                int tmp = numbers[i];
                int before_target = target-numbers[i];
                numbers[i] = target-numbers[i]-1;
                index = binarysearch(numbers,numbersSize,before_target);
                numbers[i] = tmp;
                if(i>index)
                    index = -1;
            }
            if(index != -1){
                r[0] = i+1;
                r[1] = index+1;
                return r;
            }
        }
        return r;
    }
    

    相关文章

      网友评论

          本文标题:167-two-sum-ii-input-array-is-so

          本文链接:https://www.haomeiwen.com/subject/tksvlctx.html