[LinkedIn] Find all triangles in an array

From Here Changed it a little

public ArrayList<ArrayList<Integer>> valid2(int[] A) {
        ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
        Arrays.sort(A);
        for (int i = 0; i < A.length; ++i) {
            for (int j = i; j < A.length; ++j) {
                for (int l = j; l < A.length && A[i] + A[j] > A[l]; l++) {
                    ArrayList<Integer> tmp = new ArrayList<Integer>();
                    tmp.add(A[i]);
                    tmp.add(A[j]);
                    tmp.add(A[l]);
                    result.add(tmp);
                }
            }
        }
        return result;
    }
posted on 2015-03-31 15:26  Seth_L  阅读(109)  评论(0编辑  收藏  举报