返回介绍

solution / 0900-0999 / 0976.Largest Perimeter Triangle / README_EN

发布于 2024-06-17 01:03:32 字数 3817 浏览 0 评论 0 收藏 0

976. Largest Perimeter Triangle

中文文档

Description

Given an integer array nums, return _the largest perimeter of a triangle with a non-zero area, formed from three of these lengths_. If it is impossible to form any triangle of a non-zero area, return 0.

 

Example 1:

Input: nums = [2,1,2]
Output: 5
Explanation: You can form a triangle with three side lengths: 1, 2, and 2.

Example 2:

Input: nums = [1,2,1,10]
Output: 0
Explanation: 
You cannot use the side lengths 1, 1, and 2 to form a triangle.
You cannot use the side lengths 1, 1, and 10 to form a triangle.
You cannot use the side lengths 1, 2, and 10 to form a triangle.
As we cannot use any three side lengths to form a triangle of non-zero area, we return 0.

 

Constraints:

  • 3 <= nums.length <= 104
  • 1 <= nums[i] <= 106

Solutions

Solution 1

class Solution:
  def largestPerimeter(self, nums: List[int]) -> int:
    nums.sort()
    for i in range(len(nums) - 1, 1, -1):
      if (c := nums[i - 1] + nums[i - 2]) > nums[i]:
        return c + nums[i]
    return 0
class Solution {
  public int largestPerimeter(int[] nums) {
    Arrays.sort(nums);
    for (int i = nums.length - 1; i >= 2; --i) {
      int c = nums[i - 1] + nums[i - 2];
      if (c > nums[i]) {
        return c + nums[i];
      }
    }
    return 0;
  }
}
class Solution {
public:
  int largestPerimeter(vector<int>& nums) {
    sort(nums.begin(), nums.end());
    for (int i = nums.size() - 1; i >= 2; --i) {
      int c = nums[i - 1] + nums[i - 2];
      if (c > nums[i]) return c + nums[i];
    }
    return 0;
  }
};
func largestPerimeter(nums []int) int {
  sort.Ints(nums)
  for i := len(nums) - 1; i >= 2; i-- {
    c := nums[i-1] + nums[i-2]
    if c > nums[i] {
      return c + nums[i]
    }
  }
  return 0
}
function largestPerimeter(nums: number[]): number {
  const n = nums.length;
  nums.sort((a, b) => b - a);
  for (let i = 2; i < n; i++) {
    const [a, b, c] = [nums[i - 2], nums[i - 1], nums[i]];
    if (a < b + c) {
      return a + b + c;
    }
  }
  return 0;
}
impl Solution {
  pub fn largest_perimeter(mut nums: Vec<i32>) -> i32 {
    let n = nums.len();
    nums.sort_unstable_by(|a, b| b.cmp(&a));
    for i in 2..n {
      let (a, b, c) = (nums[i - 2], nums[i - 1], nums[i]);
      if a < b + c {
        return a + b + c;
      }
    }
    0
  }
}
int cmp(const void* a, const void* b) {
  return *(int*) b - *(int*) a;
}

int largestPerimeter(int* nums, int numsSize) {
  qsort(nums, numsSize, sizeof(int), cmp);
  for (int i = 2; i < numsSize; i++) {
    if (nums[i - 2] < nums[i - 1] + nums[i]) {
      return nums[i - 2] + nums[i - 1] + nums[i];
    }
  }
  return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文