# 思路一

``````    /**
* 方案一：先排序后筛选 [LeetCode：22ms]
*/
class Solution1 {
public int largestPerimeter(int[] A) {
Arrays.sort(A);
for (int i = A.length - 1; i >= 2; i--) {
if (A[i] < A[i - 1] + A[i - 2])
return A[i] + A[i - 1] + A[i - 2];
}
return 0;
}
}

# 思路二

``````
/**
* 方案二：选择排序，提前筛选 [LeetCode:14ms]
*/
class Solution2 {
public int largestPerimeter(int[] A) {
for (int i = 0; i < A.length; i++) {
int minP = i;
for (int j = i + 1; j < A.length; j++) {
if (A[j] > A[minP]) {
minP = j;
}
}
int temp = A[i];
A[i] = A[minP];
A[minP] = temp;
if (i >= 2 && A[i - 2] < A[i - 1] + A[i])
return A[i] + A[i - 1] + A[i - 2];
}
return 0;
}
}

/**
* 方案三：大顶堆，提前筛选 [LeetCode:8ms]
*/
class Solution3 {
/**
* 大顶堆
*
* @param A
*/
public int largestPerimeter(int[] A) {
buildHeap(A);
for (int i = A.length - 1; i >= 0; i--) {
swap(A, 0, i);
if (i < A.length - 2 && A[i + 2] < A[i + 1] + A[i]) {
return A[i + 2] + A[i + 1] + A[i];
}
}
return 0;
}

/**
* 调整
*
* @param nums
* @param length
* @param x
*/
public void adjust(int[] nums, int length, int x) {
while (x <= length / 2 - 1) {
int left = nums[2 * x + 1];
int right = left - 1;
if (2 * x + 2 < length) {
right = nums[2 * x + 2];
}
int max = Math.max(left, right);
if (nums[x] >= max)
break;
if (max == left) {
int temp = nums[x];
nums[x] = left;
nums[2 * x + 1] = temp;
x = 2 * x + 1;
} else {
int temp = nums[x];
nums[x] = right;
nums[2 * x + 2] = temp;
x = 2 * x + 2;
}
}
}

/**
* 交换两个数
*
* @param nums
* @param i
* @param j
*/
public void swap(int[] nums, int i, int j) {
if (i == j)
return;
nums[i] = nums[i] ^ nums[j];
nums[j] = nums[i] ^ nums[j];
nums[i] = nums[i] ^ nums[j];
}

/**
* 建堆
*
* @param nums
*/
public void buildHeap(int[] nums) {
int length = nums.length;
for (int i = length / 2 - 1; i >= 0; i--) {