A text and understand the array

Method a: the double pointer method, first sort the array

a=[12,6,8,1,4,3]
def sum2(a,target):
    res=[]
    a=sorted(a)
    l,r=0,len(a)-1
    while l<r:
        tmp=[0,0]
        if a[l]+a[r]==target:
            tmp[0]=a[l]
            tmp[1]=a[r]
            res.append(tmp)
            l+=1
            r-=1
        elif a[l]+a[r]>target:
            r-=1
        else:
            l+=1
    return res
print(sum2(a,9))

Output: [[1, 8], [3, 6]]

Method Two: For the first method, the main time is spent on making the sort, we can use the hash to avoid the sort.

def sum2(a,target):
    dic={}
    res=[]
    for i in range(len(a)):
        tmp=[0,0]
        m=a[i]
        if target-m in dic:
            tmp[0]=m
            tmp[1]=target-m
            res.append(tmp)
        dic[m]=i
    return res
print(sum2(a,10))

Output: [[1, 8], [3, 6]]

Method three: eliminating established hash table

def sum2(a,target):
    res=[]
    for i in range(len(a)):
        tmp=[0,0]
        if  target-a[i] in a[i+1:]:
            tmp[0]=a[i]
            tmp[1]=target-a[i]
            res.append(tmp)
    return res
print(sum2(a,9))

Output: [[6,3], [8, 1]]

Extension: The method and can be extended to three, four and the number of the three numbers, and the like;

def sum3(a,target):
    res=[]
    for i in range(len(a)):
        for j in range(i+1,len(a)):
            tmp=[0,0,0]
            if target-a[i]-a[j] in a[j+1:]:
                tmp[0]=a[i]
                tmp[1]=a[j]
                tmp[2]=target-a[i]-a[j]
                res.append(tmp)
    return res
print(sum3(a,13))

Output: [[6, 4, 3], [8, 1, 4]]

def sum4(a,target):
    res=[]
    for i in range(len(a)):
        for j in range(i+1,len(a)):
            for k in range(j+1,len(a)):
                tmp=[0,0,0,0]
                if target-a[i]-a[j]-a[k] in a[k+1:]:
                    tmp[0]=a[i]
                    tmp[1]=a[j]
                    tmp[2]=a[k]
                    tmp[3]=target-a[i]-a[j]-a[k]
                    res.append(tmp)
    return res
print(sum4(a,24))

Output: [[12, 8, 1, 3]]

Extension: and the array is n, but not the number, but also can not be repeated

a=[12,6,8,1,4,3]
res=[]
def nor_sum(a,target,pos,end,tmp):
    global res
    if target<0:
        return
    if target==0:
        res.append(tmp[:])
    for i in range(pos,end):
        tmp.append(a[i])
        nor_sum(a,target-a[i],i+1,end,tmp)
        tmp.pop()
nor_sum(a,12,0,len(a),[])
print(res)

Output: [[12], [8, 1, 3], [8, 4]]

:( The main difference can be repeated as red marked)

a=[12,6,8,4,3]
res=[]
def nor_sum(a,target,pos,end,tmp):
    global res
    if target<0:
        return
    if target==0:
        res.append(tmp[:])
    for i in range(pos,end):
        tmp.append(a[i])
        nor_sum(a,target-a[i],i,end,tmp)
        tmp.pop()
nor_sum(a,12,0,len(a),[])
print(res)

Output: [[12], [6, 6], [6, 3, 3], [8, 4], [4, 4, 4], [3, 3, 3, 3]]

 

Guess you like

Origin www.cnblogs.com/xiximayou/p/11622102.html