算法--Two sum之排序数字二分查找实现

python

def findTwoSumSorted(inputList, sumOfTwo, start, end):
    """ find two number in the inputList, which the sum is sumOfTwo, the inputList is sorted inputList: a list of number sumOfTwo: a number return: list of two index """
    result = [-1, -1]

    while (start + 1 < end):
        mid = int((start + end)/2)
        print("start is:", start, "end is:", end, "mid is:", mid)

        if (inputList[start] + inputList[end]) > sumOfTwo:
            if (inputList[start] + inputList[mid] > sumOfTwo):
                end = mid - 1
            elif (inputList[start] + inputList[mid] < sumOfTwo):
                end = end -1
            else:
                result[0] = start
                result[1] = mid
                print("find two: ",result[0], result[1])
 return result
        elif (inputList[start] + inputList[end]) < sumOfTwo:   
            if (inputList[mid] + inputList[end] < sumOfTwo):
                start = mid + 1
            elif (inputList[mid] + inputList[end] > sumOfTwo):
                start = start + 1
            else:
                result[0] = mid
                result[1] = end
                print("find two: ",result[0], result[1])
 return result
        else:
            result[0] = start
            result[1] = end
            print("find two: ",result[0], result[1])
 return result   

    if (start < end) and (inputList[start] + inputList[end] == sumOfTwo): 
        print("start is:", start, "end is:", end)
        result[0] = start
        result[1] = end
        print("find two: ",result[0], result[1])
    else:
        print("COULD NOT find two with sum of", sumOfTwo)

if __name__ == "__main__":
   # rst = findTwoSum([1,5,3,7,2,9,6,11], 18)
    inputList = [1,2,5,7,8,9,12,13,16,18,22,25]
    two = findTwoSumSorted(inputList, 12, 0, len(inputList)-1)

C语言

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//用二分查找解决在排好序的一列数中找到两个之和为某一给定值得问题
int *sumTwo(int *numList, int len, int target)
{
    int start,mid,end;
    int * index = (int *)malloc(2*sizeof(int));  //存放输出结果
    start=0;
    end=len-1;

    if(numList == NULL)
    {
        printf("number list is null\n");
        return NULL;
    }   

    while(start+1<end)
    {
        mid=start + (end-start)/2;
        if(numList[start] + numList[end]<target)
        {
            if(numList[mid]+numList[end]<target)
            {
                start=mid;
            }   
            else if(numList[mid]+numList[end]>target)
            {
                start++;
            }   
            else
            {
                index[0]=mid;
                index[1]=end;
                printf("index:%d,%d\n",index[0],index[1]);
                return index;
            }   

        }
        else if(numList[start] + numList[end]>target)
        {
            if(numList[start]+numList[mid]>target)
            {
                end=mid;
            }   
            else if(numList[start]+numList[mid]<target)
            {
                end--;
            }   
            else
            {
                index[0]=start;
                index[1]=mid;
                printf("index:%d,%d\n",index[0],index[1]);
                return index;
            }               
        }
        else
        {
            index[0]=start;
            index[1]=end;
            printf("index:%d,%d\n",index[0],index[1]);
            return index;
        }   
    }   
    printf("fail to find two sum is equal to target\n");
    return NULL;
}

int main () {
    clock_t clockStart,clockEnd; //用于计算程序运行时间
    double cost;
    clockStart=clock();

    printf("hello https://tool.lu/\n");
    int nums[]={2,3,5,6,7,9,13,17,19,33};
    int sum=16;
    int lenInput=sizeof(nums)/sizeof(nums[0]);
    printf("lenInput:%d\n",lenInput);
    int *twoIndex=sumTwo(nums,lenInput,sum);

    if(twoIndex!=NULL)
    {
        free(twoIndex);
        twoIndex=NULL;
    }   

    clockEnd=clock();
    cost=(double)(clockEnd-clockStart)/CLOCKS_PER_SEC;
    printf("clockStart:%ld,clockEnd:%ld,cost %ld seconds\n",clockStart,clockEnd,cost);

    return 0;
}
    原文作者:查找算法
    原文地址: https://blog.csdn.net/weixin_41043240/article/details/79440040
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞