返回介绍

solution / 3000-3099 / 3024.Type of Triangle / README_EN

发布于 2024-06-17 01:02:57 字数 4958 浏览 0 评论 0 收藏 0

3024. Type of Triangle

中文文档

Description

You are given a 0-indexed integer array nums of size 3 which can form the sides of a triangle.

  • A triangle is called equilateral if it has all sides of equal length.
  • A triangle is called isosceles if it has exactly two sides of equal length.
  • A triangle is called scalene if all its sides are of different lengths.

Return _a string representing_ _the type of triangle that can be formed __or _"none"_ if it cannot form a triangle._

 

Example 1:

Input: nums = [3,3,3]
Output: "equilateral"
Explanation: Since all the sides are of equal length, therefore, it will form an equilateral triangle.

Example 2:

Input: nums = [3,4,5]
Output: "scalene"
Explanation: 
nums[0] + nums[1] = 3 + 4 = 7, which is greater than nums[2] = 5.
nums[0] + nums[2] = 3 + 5 = 8, which is greater than nums[1] = 4.
nums[1] + nums[2] = 4 + 5 = 9, which is greater than nums[0] = 3. 
Since the sum of the two sides is greater than the third side for all three cases, therefore, it can form a triangle.
As all the sides are of different lengths, it will form a scalene triangle.

 

Constraints:

  • nums.length == 3
  • 1 <= nums[i] <= 100

Solutions

Solution 1: Sorting + Case Discussion

First, we sort the array, and then we can classify and discuss according to the definition of a triangle.

  • If the sum of the smallest two numbers is less than or equal to the largest number, then it cannot form a triangle, return "none".
  • If the smallest number is equal to the largest number, then it is an equilateral triangle, return "equilateral".
  • If the smallest number is equal to the middle number or the middle number is equal to the largest number, then it is an isosceles triangle, return "isosceles".
  • Otherwise, return "scalene".

The time complexity is $O(1)$, and the space complexity is $O(1)$.

class Solution:
  def triangleType(self, nums: List[int]) -> str:
    nums.sort()
    if nums[0] + nums[1] <= nums[2]:
      return "none"
    if nums[0] == nums[2]:
      return "equilateral"
    if nums[0] == nums[1] or nums[1] == nums[2]:
      return "isosceles"
    return "scalene"
class Solution {
  public String triangleType(int[] nums) {
    Arrays.sort(nums);
    if (nums[0] + nums[1] <= nums[2]) {
      return "none";
    }
    if (nums[0] == nums[2]) {
      return "equilateral";
    }
    if (nums[0] == nums[1] || nums[1] == nums[2]) {
      return "isosceles";
    }
    return "scalene";
  }
}
class Solution {
public:
  string triangleType(vector<int>& nums) {
    sort(nums.begin(), nums.end());
    if (nums[0] + nums[1] <= nums[2]) {
      return "none";
    }
    if (nums[0] == nums[2]) {
      return "equilateral";
    }
    if (nums[0] == nums[1] || nums[1] == nums[2]) {
      return "isosceles";
    }
    return "scalene";
  }
};
func triangleType(nums []int) string {
  sort.Ints(nums)
  if nums[0]+nums[1] <= nums[2] {
    return "none"
  }
  if nums[0] == nums[2] {
    return "equilateral"
  }
  if nums[0] == nums[1] || nums[1] == nums[2] {
    return "isosceles"
  }
  return "scalene"
}
function triangleType(nums: number[]): string {
  nums.sort((a, b) => a - b);
  if (nums[0] + nums[1] <= nums[2]) {
    return 'none';
  }
  if (nums[0] === nums[2]) {
    return 'equilateral';
  }
  if (nums[0] === nums[1] || nums[1] === nums[2]) {
    return 'isosceles';
  }
  return 'scalene';
}
public class Solution {
  public string TriangleType(int[] nums) {
    Array.Sort(nums);
    if (nums[0] + nums[1] <= nums[2]) {
      return "none";
    }
    if (nums[0] == nums[2]) {
      return "equilateral";
    }
    if (nums[0] == nums[1] || nums[1] == nums[2]) {
      return "isosceles";
    }
    return "scalene";
  }
}

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

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

发布评论

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