Skip to content

Instantly share code, notes, and snippets.

@EduardoSaverin
Forked from st0le/ThreeSum.java
Last active August 29, 2015 14:23
Show Gist options
  • Save EduardoSaverin/ccaffe956c25b6b91576 to your computer and use it in GitHub Desktop.
Save EduardoSaverin/ccaffe956c25b6b91576 to your computer and use it in GitHub Desktop.
// Complexity - O(n^2), Space Complexity - O(n^2)
private int[] findTriple_3(int[] A) {
Map<Integer, int[]> map = new HashMap<Integer, int[]>();
for (int i = 0, l = A.length; i < l; i++) {
map.clear();
for (int j = i + 1; j < l; j++) {
if (map.containsKey(A[j])) {
int[] pair = map.get(A[j]);
return new int[]{pair[0], pair[1], A[j]};
} else
map.put(-A[i] - A[j], new int[]{A[i], A[j]});
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment