【Leetcode_easy】976. Largest Perimeter Triangle

problem

976. Largest Perimeter Triangle

solution:

class Solution {
public:
    int largestPerimeter(vector<int>& A) {
        sort(A.begin(), A.end());//decrease.
        for(int i=A.size()-1; i>1; i--)
        {
            if(A[i]<A[i-1]+A[i-2]) return A[i]+A[i-1]+A[i-2];
        }
        return 0;
    }
};

参考

1. Leetcode_easy_976. Largest Perimeter Triangle;

原文地址:https://www.cnblogs.com/happyamyhope/p/11316927.html

时间: 2024-11-11 02:05:33

【Leetcode_easy】976. Largest Perimeter Triangle的相关文章

LeetCode 976. Largest Perimeter Triangle

976. Largest Perimeter Triangle(三角形的最大周长) 链接 https://leetcode-cn.com/problems/largest-perimeter-triangle 题目 给定由一些正数(代表长度)组成的数组 A,返回由其中三个长度组成的.面积不为零的三角形的最大周长. 如果不能形成任何面积不为零的三角形,返回?0. 示例 1: 输入:[2,1,2] 输出:5 示例 2: 输入:[1,2,1] 输出:0 示例 3: 输入:[3,2,3,4] 输出:10

LeetCode 976. Largest Perimeter Triangle (三角形的最大周长)

题目标签:Array 题目给了我们一个 边长的 array, 让我们找出 最大边长和的三角形,当然前提得是这三条边能组成三角形.如果array 里得边长组成不了三角形,返回0. 最直接的理解就是,找到三条最长的边,再判断是不是能够组成三角形,如果不行,继续去找更小得边. 所以维护三个max1,max2,max3,然后利用 “任意两边之和大于第三边” 来判断. 具体看code. Java Solution: Runtime beats 99.57% 完成日期:2/11/2019 关键点:“任意两边

「Leetcode」976. Largest Perimeter Triangle(C++)

分析 好久不刷题真的思维僵化,要考虑到这样一个结论:如果递增的三个数\(x_i,x_{i+1},x_{i+2}\)不符合题意,那么最大的两边之差一定大于等于第一条边,那么任何比第一条边小的都不能成立.这样一来,递增排序,然后线性找就可以了. 代码 class Solution { public: int largestPerimeter(vector<int>& A) { int ans=0; sort(A.begin(),A.end()); for(int i=A.size()-3;

【leetcode】118. Pascal&#39;s Triangle

@requires_authorization @author johnsondu @create_time 2015.7.23 19:54 @url [Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/) /************************ * @description: simple. * @time_complexity: O(n) * @space_complexity: O(n) ****

【LeetCode】119 - Pascal&#39;s Triangle II

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? Solution: 1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex){

【转】Life of a triangle - NVIDIA&#39;s logical pipeline

From:https://developer.nvidia.com/content/life-triangle-nvidias-logical-pipeline Since the release of the ground breaking Fermi architecture almost 5 years have gone by, it might be time to refresh the principle graphics architecture beneath it. Ferm

【Leetcode】Pascal&amp;#39;s Triangle II

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 思路:最简单的方法就是依照[Leetcode]Pascal's Triangle 的方式自顶向下依次求解,但会造成空间的浪费.若仅仅用一个vect

[Swift Weekly Contest 118]LeetCode976. 三角形的最大周长 | Largest Perimeter Triangle

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. Example 1: Input: [2,1,2] Output: 5 Example 2: I

【LeetCode】84. Largest Rectangle in Histogram——直方图最大面积

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each bar is 1, given height =[2,1,5,6,2,3]. The largest r