976. 三角形的最大周长

版权声明:孔庆鑫 https://blog.csdn.net/kongqingxin12/article/details/86418122

976. 三角形的最大周长

class Solution {
public:
	int largestPerimeter(vector<int>& A) {
		sort(A.begin(), A.end());
		int _min = 0, _max = 0;
		for (int i = A.size() - 1; i > 1; --i) {
			int first = A[i], second = A[i - 1];
			int pos_three = i - 2;
			_min = abs(first - second), _max = first + second;
			if (A[pos_three] <= _min && pos_three == 0)
				return 0;
			else if (A[pos_three] > _min)
				return first + second + A[pos_three];
		}
	}
};

猜你喜欢

转载自blog.csdn.net/kongqingxin12/article/details/86418122