类 ThreeSumFast
- java.lang.Object
-
- edu.princeton.cs.algs4.ThreeSumFast
-
public class ThreeSumFast extends java.lang.Object
TheThreeSumFast
class provides static methods for counting and printing the number of triples in an array of distinct integers that sum to 0 (ignoring integer overflow).This implementation uses sorting and binary search and takes time proportional to n^2 log n, where n is the number of integers.
For additional documentation, see Section 1.4 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
-
-
方法概要
修饰符和类型 方法 说明 static int
count(int[] a)
Returns the number of triples (i, j, k) withi < j < k
such thata[i] + a[j] + a[k] == 0
.static void
main(java.lang.String[] args)
Reads in a sequence of distinct integers from a file, specified as a command-line argument; counts the number of triples sum to exactly zero; prints out the time to perform the computation.static void
printAll(int[] a)
Prints to standard output the (i, j, k) withi < j < k
such thata[i] + a[j] + a[k] == 0
.
-
-
-
方法详细资料
-
printAll
public static void printAll(int[] a)
Prints to standard output the (i, j, k) withi < j < k
such thata[i] + a[j] + a[k] == 0
.- 参数:
a
- the array of integers- 抛出:
java.lang.IllegalArgumentException
- if the array contains duplicate integers
-
count
public static int count(int[] a)
Returns the number of triples (i, j, k) withi < j < k
such thata[i] + a[j] + a[k] == 0
.- 参数:
a
- the array of integers- 返回:
- the number of triples (i, j, k) with
i < j < k
such thata[i] + a[j] + a[k] == 0
-
main
public static void main(java.lang.String[] args)
Reads in a sequence of distinct integers from a file, specified as a command-line argument; counts the number of triples sum to exactly zero; prints out the time to perform the computation.- 参数:
args
- the command-line arguments
-
-