003 不重复打印排序数组中相加和为给定值的所有二元组
Table of Contents

ac

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class RsData{
    public int a;
    public int b;

    public RsData(int a, int b) {
        this.a = a;
        this.b = b;
    }
}
public class Main {

    public static Scanner sc = new Scanner(System.in);

    public static List<RsData> twoAns(int[] arr, int left, int right, int k){
        List<RsData> pairList = new ArrayList<>((right - left) / 2);
        int i = left;
        int j = right;
        while(i < j){
            int a = arr[i];
            int b = arr[j];
            if(a + b == k){
                pairList.add(new RsData(a, b));
                j--;
                while (i < j && arr[j] == b){
                    j --;
                }
                i++;
                while (i < j && arr[i+1] == a){
                    i++;
                }
            }else if(a + b < k){
                i ++;
            }else {
                j --;
            }
        }
        return pairList;
    }

    public static void dealTwo(){
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++){
            arr[i] = sc.nextInt();
        }
        List<RsData> pairList = twoAns(arr,0,n-1,k);
        for(RsData rsData: pairList){
            Integer a = rsData.a;
            Integer b = rsData.b;
            System.out.println(String.format("%d %d", a, b));
        }
    }

    public static void main(String[] args) {
        dealTwo();
    }

}