求两数组中满足给定和的数对
给定两个有序整型数组a和b,各有n个元素,求两个数组中满足给定和的数对, 即对a中元素i和b中元素j,满足i + j = s(s已知)
public class PairSummation { public static void execute(int[] a, int[] b, int s) { assert (a.length == b.length); int i = 0; int j = b.length - 1; int repeat = 0; while (i <= a.length - 1) { while (j >= 0) { int tmp = a[i] + b[j]; if (tmp > s) { j--; } else if (tmp == s) { System.out.println("a[" + i + "]:" + a[i] + " b[" + j + "]:" + b[j]); j--; repeat++; } else { j += repeat; repeat = 0; break; } } i++; } } public static void main(String[] args) { int[] a = { 1, 1, 2, 2, 3, 4, 5 }; int[] b = { 6, 7, 8, 9, 10, 10, 11 }; PairSummation.execute(a, b, 11); } }