976. Largest Perimeter Triangle(Leetcode每日一題-2020.11.29)
Problem
Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero area, formed from 3 of these lengths.
If it is impossible to form any triangle of non-zero area, return 0.
Note:
- 3 <= A.length <= 10000
- 1 <= A[i] <= 10^6
Example1
Input: [2,1,2]
Output: 5
Example2
Input: [1,2,1]
Output: 0
Example3
Input: [3,2,3,4]
Output: 10
Example4
Input: [3,6,2,3]
Output: 8
Solution
class Solution {
public:
int largestPerimeter(vector<int>& A) {
sort(A.begin(),A.end());
for(int i = A.size()-1;i>=2;--i)
{
if(A[i-2] + A[i-1] > A[i])
return A[i] + A[i-1] + A[i-2];
}
return 0;
}
};
相關文章
- leetcode每日一題LeetCode每日一題
- Leetcode每日一題(1)LeetCode每日一題
- LeetCode:Largest NumberLeetCode
- Leetcode TriangleLeetCode
- LeetCode每日一題:sort colorsLeetCode每日一題
- LeetCode-Largest NumberLeetCode
- Leetcode-TriangleLeetCode
- Triangle leetcode javaLeetCodeJava
- LeetCode 每日一題「判定字元是否唯一」LeetCode每日一題字元
- LeetCode-Largest BST SubtreeLeetCode
- Leetcode Pascal's TriangleLeetCode
- LeetCode每日一題:Nim遊戲(No.292)LeetCode每日一題遊戲
- LeetCode每日一題: 找不同(No.389)LeetCode每日一題
- LeetCode每日一題: 移除元素(No.27)LeetCode每日一題
- LeetCode Kth Largest Element in an ArrayLeetCode
- Leetcode - Largest Divisible SubsetLeetCode
- LeetCode-Largest Divisble SubsetLeetCode
- LeetCode 120 TriangleLeetCode
- leetcode Pascal's Triangle IILeetCode
- Leetcode-Pascal's TriangleLeetCode
- LeetCode每日一題: 移動零(No.283)LeetCode每日一題
- LeetCode每日一題:自除數(No.728)LeetCode每日一題
- LeetCode每日一題:迴文數(No.9)LeetCode每日一題
- LeetCode每日一題:兩數之和(No.1)LeetCode每日一題
- LeetCode每日一題:爬樓梯(No.70)LeetCode每日一題
- LeetCode每日一題: 排列硬幣(No.441)LeetCode每日一題
- LeetCode每日一題: 各位相加(No.258)LeetCode每日一題
- LeetCode每日一題:longest palindromic substringLeetCode每日一題
- LeetCode 2024/6 每日一題 合集LeetCode每日一題
- LeetCode解題報告 120. Triangle [medium]LeetCode
- Leetcode每日一題:面試題16.19.水域大小LeetCode每日一題面試題
- LeetCode每日一題:整數反轉(No.7)LeetCode每日一題
- LeetCode每日一題:求眾數(No.169)LeetCode每日一題
- LeetCode每日一題: 轉置矩陣(No.867)LeetCode每日一題矩陣
- LeetCode每日一題:最長公共字首(No.14)LeetCode每日一題
- LeetCode每日一題: 搜尋插入位置(No.35)LeetCode每日一題
- 【js】Leetcode每日一題-葉子相似的樹JSLeetCode每日一題
- 【LeetCode】每日一題164. 最大間距LeetCode每日一題