leetcode

package
v0.0.0-...-3200de1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 8, 2023 License: MIT Imports: 0 Imported by: 0

README

976. 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:

Input: [1,2,1]
Output: 0

Example 3:

Input: [3,2,3,4]
Output: 10

Example 4:

Input: [3,6,2,3]
Output: 8

Note:

  • 3 <= A.length <= 10000
  • 1 <= A[i] <= 10^6

题目大意

找到可以组成三角形三条边的长度,要求输出三条边之和最长的,即三角形周长最长。

解题思路

这道题也是排序题,先讲所有的长度进行排序,从大边开始往前找,找到第一个任意两边之和大于第三边(满足能构成三角形的条件)的下标,然后输出这 3 条边之和即可,如果没有找到输出 0 。

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL