public static int findSameMin(int[] A, int[] B) { Arrays.sort(A); Arrays.sort(B); int indexA = 0; int indexB = 0; while (indexA < A.length && indexB < B.length) { int headOfA = A[indexA]; int headOfB = B[indexB]; if (headOfA == headOfB) { return headOfA; } else if (headOfA < headOfB) { indexA++; } else { indexB++; } } return -1; }
private static List<List<Integer>> threeSum(int[] num) { List resultList = new ArrayList<ArrayList>(); Arrays.sort(num); int length = num.length; for (int i = 0; i < length - 2; ) { int j = i + 1, k = length - 1; while (j < k) { int sum = num[i] + num[j] + num[k]; if (sum == 0) { List<Integer> list = new ArrayList<Integer>(); list.add(num[i]); list.add(num[j]); list.add(num[k]); resultList.add(list); do { j++; } while (num[j] == num[j - 1]); do { k--; } while (num[k] == num[k + 1]); } else if (sum > 0) { do { k--; } while (num[k] == num[k + 1]); } else { do { j++; } while (num[j] == num[j - 1]); } } do { i++; } while (num[i] == num[i - 1]); } return resultList; }