-
-
Save gaoyike/40df1e0ec5a6c95e0014 to your computer and use it in GitHub Desktop.
Correct Answer;
Find all triangle in array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Arrays; | |
/** | |
* Created by Readman on 8/1/14. | |
*/ | |
public class VaildTriangle { | |
/* | |
>.< Eventually | |
* */ | |
int answer = 0; | |
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) { | |
int k = i; | |
for (int j = i; j < A.length; ++j) { | |
while (k < A.length && A[i] + A[j] > A[k]) | |
++k; | |
for (int l = j; l < k; l++) { | |
ArrayList<Integer> tmp = new ArrayList<Integer>(); | |
tmp.add(A[i]); | |
tmp.add(A[j]); | |
tmp.add(A[l]); | |
result.add(tmp); | |
} | |
} | |
} | |
return result; | |
} | |
public static void main(String[] args) { | |
VaildTriangle vaildTriangle = new VaildTriangle(); | |
int[] t = new int[]{1,2,3}; | |
ArrayList<ArrayList<Integer>> result2 = vaildTriangle.valid2(t); | |
System.out.println(result2.size()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment