【leetcode】976. Largest Perimeter Triangle
题目如下:
Given an array
Aof 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: 5Example 2:
Input: [1,2,1]
Output: 0Example 3:
Input: [3,2,3,4]
Output: 10Example 4:
Input: [3,6,2,3]
Output: 8Note:
3 <= A.length <= 100001 <= A[i] <= 10^6
解题思路:三角形的边长规则有两个,一是任意两边之和大于第三边,这里其实只要判断两个较短的边的边长和是否大于最长边即可;二是任意两边之差小于第三边。
代码如下:
class Solution(object):
def largestPerimeter(self, A):
"""
:type A: List[int]
:rtype: int
"""
res = 0
A.sort(reverse=True)
for i in range(len(A)-2):
if A[i] - A[i+1] < A[i+2] and A[i] < A[i+1] + A[i+2]:
res = A[i] + A[i+1] + A[i+2]
break
return res
【leetcode】976. Largest Perimeter Triangle的更多相关文章
- 【LeetCode】976. Largest Perimeter Triangle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...
- 【Leetcode_easy】976. Largest Perimeter Triangle
problem 976. Largest Perimeter Triangle solution: class Solution { public: int largestPerimeter(vect ...
- 「Leetcode」976. Largest Perimeter Triangle(C++)
分析 好久不刷题真的思维僵化,要考虑到这样一个结论:如果递增的三个数\(x_i,x_{i+1},x_{i+2}\)不符合题意,那么最大的两边之差一定大于等于第一条边,那么任何比第一条边小的都不能成立. ...
- 【LeetCode】764. Largest Plus Sign 解题报告(Python)
[LeetCode]764. Largest Plus Sign 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
- 【LeetCode】813. Largest Sum of Averages 解题报告(Python)
[LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- LeetCode 976 Largest Perimeter Triangle 解题报告
题目要求 Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero ...
- 119th LeetCode Weekly Contest Largest Perimeter Triangle
Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero area, ...
- 976. Largest Perimeter Triangle
Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero area, ...
随机推荐
- npm和gem
https://blog.csdn.net/u011099640/article/details/53083845
- ueditor 图片粘贴上传,实现图文粘贴,图片自动上传
如何做到 ueditor批量上传word图片? 1.前端引用代码 <!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN& ...
- [CSP-S模拟测试]:marshland(最大费用可行流)
题目描述 前方有一片沼泽地.方便地,我们用一个$n\times n$的网格图来描述它,每一个格子代表着沼泽地的一小片区域.其中$(1,1)$代表网格图的左上角,$(n,n)$代表网格图的右下角.若用$ ...
- outlook使用inline style回复邮件
Reply with inline comments within the original message text When you reply to email messages in Outl ...
- Linux sudo 详解
简单的说,sudo 是一种权限管理机制,管理员可以授权于一些普通用户去执行一些 root 执行的操作,而不需要知道 root 的密码.严谨些说,sudo 允许一个已授权用户以超级用户或者其它用户的角色 ...
- 数据结构C语言实现
顺序表实现 typedef int Position; typedef struct LNode *List; struct LNode { ElementType Data[MAXSIZE]; Po ...
- 转 linux 服务器内存占用统计
linux 服务器内存占用统计 原文: https://www.cnblogs.com/eaglediao/p/6641811.html 当前内存占用率的计算,是根据top命令显示的Mem.used ...
- 转 LoadRunner错误处理函数
在脚本的Run-time Settings中,可以设置在脚本运行过程中发生错误的处理方式.进入到Run-time Settings中,切换到Miscellaneous标签页,可以看到Error Han ...
- 组件化框架设计之Java SPI机制(三)
阿里P7移动互联网架构师进阶视频(每日更新中)免费学习请点击:https://space.bilibili.com/474380680 本篇文章将从深入理解java SPI机制来介绍组件化框架设计: ...
- 插件化框架解读之so 文件加载机制(四)
阿里P7移动互联网架构师进阶视频(每日更新中)免费学习请点击:https://space.bilibili.com/474380680 提问 本文的结论是跟着 System.loadlibrary() ...