📜  用于两个指针技术的 Python3 程序

📅  最后修改于: 2022-05-13 01:54:20.168000             🧑  作者: Mango

用于两个指针技术的 Python3 程序

两个指针确实是一种简单有效的技术,通常用于在排序数组中搜索对。
给定一个排序数组 A(按升序排序),有 N 个整数,找出是否存在任何一对元素(A[i],A[j]),使得它们的和等于 X。

让我们看看天真的解决方案

Python3
# Naive solution to find if there is a
# pair in A[0..N-1] with given sum.
def isPairSum(A, N, X):
  
    for i in range(N):
        for j in range(N):
  
            # as equal i and j means same element
            if(i == j):
                continue
  
            # pair exists
            if (A[i] + A[j] == X):
                return True
  
            # as the array is sorted
            if (A[i] + A[j] > X):
                break
              
    # No pair found with given sum
    return 0
  
# Driver code
arr = [3, 5, 9, 2, 8, 10, 11]
val = 17
  
print(isPairSum(arr, len(arr), val))
  
# This code is contributed by maheshwaripiyush9


Python3
# Two pointer technique based solution to find
# if there is a pair in A[0..N-1] with a given sum.
def isPairSum(A, N, X):
  
    # represents first pointer
    i = 0
  
    # represents second pointer
    j = N - 1
  
    while(i < j):
        
        # If we find a pair
        if (A[i] + A[j] == X):
            return True
  
        # If sum of elements at current
        # pointers is less, we move towards
        # higher values by doing i += 1
        elif(A[i] + A[j] < X):
            i += 1
  
        # If sum of elements at current
        # pointers is more, we move towards
        # lower values by doing j -= 1
        else:
            j -= 1
    return 0
  
# array declaration
arr = [3, 5, 9, 2, 8, 10, 11]
  
# value to search
val = 17
  
print(isPairSum(arr, len(arr), val))
  
# This code is contributed by maheshwaripiyush9.


输出
1

时间复杂度: O(n 2 )。

现在让我们看看两点技术是如何工作的。我们使用两个指针,一个代表数组的第一个元素,另一个代表数组的最后一个元素,然后我们将两个指针处的值相加。如果它们的和小于 X,那么我们将左指针向右移动,或者如果它们的和大于 X,那么我们将右指针向左移动,以便更接近和。我们不断移动指针,直到我们得到总和为 X。

Python3

# Two pointer technique based solution to find
# if there is a pair in A[0..N-1] with a given sum.
def isPairSum(A, N, X):
  
    # represents first pointer
    i = 0
  
    # represents second pointer
    j = N - 1
  
    while(i < j):
        
        # If we find a pair
        if (A[i] + A[j] == X):
            return True
  
        # If sum of elements at current
        # pointers is less, we move towards
        # higher values by doing i += 1
        elif(A[i] + A[j] < X):
            i += 1
  
        # If sum of elements at current
        # pointers is more, we move towards
        # lower values by doing j -= 1
        else:
            j -= 1
    return 0
  
# array declaration
arr = [3, 5, 9, 2, 8, 10, 11]
  
# value to search
val = 17
  
print(isPairSum(arr, len(arr), val))
  
# This code is contributed by maheshwaripiyush9.
输出
1

插图 :

时间复杂度: O(n)

这是如何运作的?
该算法基本上使用输入数组已排序的事实。我们开始对极值(最小和最大)求和,并有条件地移动两个指针。当 A[i] 和 A[j] 之和小于 X 时,我们移动左指针 i。我们不会遗漏任何一对,因为和已经小于 X。同样的逻辑适用于右指针 j。

基于两个指针技术的更多问题。

  • 从两个排序数组中找到最近的对
  • 在数组中找到总和最接近 x 的对
  • 找到所有零和的三元组
  • 找到一个总和为给定值的三元组
  • 找到一个三元组,使得两个之和等于第三个元素
  • 找到总和为给定值的四个元素

有关更多详细信息,请参阅有关两指针技术的完整文章!