-
-
Save gaoyike/26f738493ad5b72a4a77 to your computer and use it in GitHub Desktop.
Eventually
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 | |
* */ | |
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 - 1; i++) { | |
for (int j = i + 1; j < A.length; j++) { | |
int end = A.length-1; | |
while (j < end) { | |
if (A[i] + A[j] > A[end] && A[i] - A[j] < A[end] && A[i] - A[j] < A[end]) { | |
ArrayList<Integer> tmp = new ArrayList<Integer>(); | |
tmp.add(A[i]); | |
tmp.add(A[j]); | |
tmp.add(A[end]); | |
result.add(tmp); | |
} | |
end--; | |
} | |
} | |
} | |
return result; | |
} | |
public ArrayList<ArrayList<Integer>> valid3(int[] A) { | |
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); | |
Arrays.sort(A); | |
for (int i = 0; i < A.length-2; i++) { | |
for (int j = i+1; j < A.length-1; j++) { | |
for (int k = j+1; k < A.length; k++) { | |
if(A[i] + A[j] > A[k] && A[i] - A[j] < A[k] && A[i] - A[j] < A[k]) { | |
ArrayList<Integer> tmp = new ArrayList<Integer>(); | |
tmp.add(A[i]); | |
tmp.add(A[j]); | |
tmp.add(A[k]); | |
result.add(tmp); | |
} | |
} | |
} | |
} | |
return result; | |
} | |
public static void main(String[] args) { | |
VaildTriangle vaildTriangle = new VaildTriangle(); | |
int[] t = new int[]{2, 4, 5, 8, 12, 15,16,24,21,25,31}; | |
ArrayList<ArrayList<Integer>> result2 = vaildTriangle.valid2(t); | |
ArrayList<ArrayList<Integer>> result3 = vaildTriangle.valid3(t); | |
System.out.println("v2: "+result2); | |
System.out.println("v3: "+result3); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment