📜  两个指针技术的Java程序

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

两个指针技术的Java程序

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

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

Java
// Naive solution to find if there is a
// pair in A[0..N-1] with given sum.
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        int arr[] = { 3, 5, 9, 2, 8, 10, 11 };
        int val = 17;
  
        System.out.println(isPairSum(arr, arr.length, val));
    }
      
    private static int isPairSum(int A[], int N, int X)
    {
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++) 
            {
                // 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;
    }
}


Java
import java.io.*;
  
class GFG 
{
     // Two pointer technique based solution to find
    // if there is a pair in A[0..N-1] with a given sum.
    public static int isPairSum(int A[], int N, int X)
    {
        // represents first pointer
        int i = 0;
  
        // represents second pointer
        int j = N - 1;
  
        while (i < j) {
  
            // If we find a pair
            if (A[i] + A[j] == X)
                return 1;
  
            // If sum of elements at current
            // pointers is less, we move towards
            // higher values by doing i++
            else if (A[i] + A[j] < X)
                i++;
  
            // If sum of elements at current
            // pointers is more, we move towards
            // lower values by doing j--
            else
                j--;
        }
        return 0;
    }
    
    // Driver code
    public static void main(String[] args)
    {
        // array declaration
        int arr[] = { 3, 5, 9, 2, 8, 10, 11 };
          
        // value to search
        int val = 17;
        
        // size of the array
        int arrSize = arr.length;
        
        // Function call
        System.out.println(isPairSum(arr, arrSize, val));
    }
}


输出
1

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

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

Java

import java.io.*;
  
class GFG 
{
     // Two pointer technique based solution to find
    // if there is a pair in A[0..N-1] with a given sum.
    public static int isPairSum(int A[], int N, int X)
    {
        // represents first pointer
        int i = 0;
  
        // represents second pointer
        int j = N - 1;
  
        while (i < j) {
  
            // If we find a pair
            if (A[i] + A[j] == X)
                return 1;
  
            // If sum of elements at current
            // pointers is less, we move towards
            // higher values by doing i++
            else if (A[i] + A[j] < X)
                i++;
  
            // If sum of elements at current
            // pointers is more, we move towards
            // lower values by doing j--
            else
                j--;
        }
        return 0;
    }
    
    // Driver code
    public static void main(String[] args)
    {
        // array declaration
        int arr[] = { 3, 5, 9, 2, 8, 10, 11 };
          
        // value to search
        int val = 17;
        
        // size of the array
        int arrSize = arr.length;
        
        // Function call
        System.out.println(isPairSum(arr, arrSize, val));
    }
}
输出
1

插图 :

时间复杂度: O(n)

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

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

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

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