资源简介
代码片段和文件信息
/******************************************************************************
* Compilation: javac ThreeSum.java
* Execution: java ThreeSum input.txt
* Dependencies: In.java StdOut.java Stopwatch.java
* Data files: https://algs4.cs.princeton.edu/14analysis/1Kints.txt
* https://algs4.cs.princeton.edu/14analysis/2Kints.txt
* https://algs4.cs.princeton.edu/14analysis/4Kints.txt
* https://algs4.cs.princeton.edu/14analysis/8Kints.txt
* https://algs4.cs.princeton.edu/14analysis/16Kints.txt
* https://algs4.cs.princeton.edu/14analysis/32Kints.txt
* https://algs4.cs.princeton.edu/14analysis/1Mints.txt
*
* A program with cubic running time. Reads n integers
* and counts the number of triples that sum to exactly 0
* (ignoring integer overflow).
*
* % java ThreeSum 1Kints.txt
* 70
*
* % java ThreeSum 2Kints.txt
* 528
*
* % java ThreeSum 4Kints.txt
* 4039
*
******************************************************************************/
package edu.princeton.cs.algs4;
/**
* The {@code ThreeSum} class provides static methods for counting
* and printing the number of triples in an array of integers that sum to 0
* (ignoring integer overflow).
*
* This implementation uses a triply nested loop and takes proportional to n^3
* where n is the number of integers.
*
* For additional documentation see Section 1.4 of
* Algorithms 4th Edition by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class ThreeSum {
// Do not instantiate.
private ThreeSum() { }
/**
* Prints to standard output the (i j k) with {@code i < j < k}
* such that {@code a[i] + a[j] + a[k] == 0}.
*
* @param a the array of integers
*/
public static void printAll(int[] a) {
int n = a.length;
for (int i = 0; i < n; i++) {
for (int j = i+1; j < n; j++) {
for (int k = j+1; k < n; k++) {
if (a[i] + a[j] + a[k] == 0) {
StdOut.println(a[i] + “ “ + a[j] + “ “ + a[k]);
}
}
}
}
}
/**
* Returns the numb
- 上一篇:班级同学录 Java程序设计
- 下一篇:算法4谢路云源码之ThreeSumFast
评论
共有 条评论