首先谢谢各位的热心，没想到很多的高人，提供了不少的思路

unhappy_popular (unhappy_popular)

http://codercareer.blogspot.ca/2014/03/no-53-longest-arithmetic-sequence.html

import java.io.Console;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ASeries {

public int longest(int[] values) {

// sort the array
Arrays.sort(values);

Map<Integer, List<Pair>> hashTable = buildHashTable(values);

return analyze(hashTable, values.length);
}

/*

for an array {3, 8, 4, 5, 6, 2, 2}, group the pars like this
differenece 0: (2,2)
difference 1: (2,3), (3,4), (4,5),(5,6)

so we need a hash table to store the information, the key is the difference.
the value is the indexes of the pairs(not values)
*/
private Map<Integer, List<Pair>> buildHashTable(int[] values) {

Map<Integer, List<Pair>> hashTable = new HashMap<Integer, List<Pair>>();

for (int i = 0; i < values.length; i++) {
for (int j = i + 1; j < values.length; j++) {

Pair pair = new Pair();
pair.setFirst(i);
pair.setSecond(j);

int diff = values[j] - values[i];
if (hashTable.containsKey(diff)) {
} else {
List<Pair> newValue = new ArrayList<Pair>();
hashTable.put(diff, newValue);
}
}
}

return hashTable;

}

/*
in this method, we need to get the length of the pairs with each difference.

A list of pairs with difference k is got given a key k in the hash table.
If an element A[i] is mth element is an arithmetic sequence with a common difference k, and there is a pair (A[i], A[j]) (j>i) in the list of pairs,
the element A[j] should be the m+1th elemement in the arithmetic sequence.

*/
private int analyze(Map<Integer, List<Pair>> hashTable, int lengthOfNumbers) {

int maxLength = 0;
for (int key : hashTable.keySet()) {
int[] lengths = new int[lengthOfNumbers];

for (int i = 0; i < lengthOfNumbers; i++) {
lengths[i] = 1;
}

for (Pair pair : hashTable.get(key)) {
lengths[pair.getSecond()] = lengths[pair.getFirst()] + 1;
}

for (int length : lengths) {
if (length > maxLength) {
maxLength = length;
}
}
}

return maxLength;
}

private static void test(String testName, int[] numbers, int expected) {

ASeries as = new ASeries();

if (as.longest(numbers) == expected) {
System.out.println(String.format("%s passed.", testName));
} else {
System.out.println(String.format("%s failed.", testName));
}
}

private static void test1() {
int[] numbers = {3, 8, 4, 5, 6, 2, 2};
int expected = 5;
test("test1", numbers, expected);
}

private static void test2() {
int[] numbers = {-1, -5, 1, 3};
int expected = 3;
test("test2", numbers, expected);
}

private static void test3() {
int[] numbers = {-10, -20, -10, -10};
int expected = 3;
test("test3", numbers, expected);
}

public static void main(String[] args) {
test1();
test2();
test3();
}

}

class Pair {
private int first;
private int second;

public int getFirst() {
return first;
}

public void setFirst(int first) {
this.first = first;
}

public int getSecond() {
return second;
}

public void setSecond(int second) {
this.second = second;
}
}

(#9937838@0)
2016-2-10 -05:00