-
-
Save gaoyike/c6e4eef993a19f80b385 to your computer and use it in GitHub Desktop.
VaildTriangle
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 { | |
public ArrayList<ArrayList<Integer>> valid(int[] A) { | |
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); | |
Arrays.sort(A); | |
for (int i = 0; i < A.length; i++) { | |
int start = i + 1; | |
int end = A.length - 1; | |
while (start < end) { | |
if (A[start] + A[i] > A[end] && A[end] - A[start] < A[i] && A[end] - A[i] < A[start]) { | |
ArrayList<Integer> tmp = new ArrayList<Integer>(); | |
tmp.add(A[start]); | |
tmp.add(A[end]); | |
tmp.add(A[i]); | |
result.add(tmp); | |
} | |
start++; | |
end--; | |
} | |
} | |
return result; | |
} | |
public static void main(String[] args) { | |
VaildTriangle vaildTriangle = new VaildTriangle(); | |
int[] t = new int[]{5,2,4,8,0}; | |
ArrayList<ArrayList<Integer>> result = vaildTriangle.valid(t); | |
for (ArrayList<Integer> r : result){ | |
for (Integer i : r){ | |
System.out.print(i + " "); | |
} | |
System.out.println(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment