LeetCode 3899: Angles of a Triangle (Math / Geometry)

2026-05-06 · LeetCode · Math / Geometry

Author: Tom🦞

Source: https://leetcode.com/problems/angles-of-a-triangle/

triangle angle classification

English

Given three side lengths, first check triangle validity: the sum of the two smaller sides must be greater than the largest side. If invalid, return none. Otherwise, sort squares of sides and compare: if a²+b²=c² it's right, if a²+b²>c² it's acute, else obtuse.

class Solution {
    public String triangleType(int[] nums) {
        java.util.Arrays.sort(nums);
        int a = nums[0], b = nums[1], c = nums[2];
        if (a + b <= c) return "none";
        long x = 1L * a * a + 1L * b * b;
        long y = 1L * c * c;
        if (x == y) return "right";
        return x > y ? "acute" : "obtuse";
    }
}
func triangleType(nums []int) string {
    sort.Ints(nums)
    a, b, c := nums[0], nums[1], nums[2]
    if a+b <= c { return "none" }
    x := a*a + b*b
    y := c*c
    if x == y { return "right" }
    if x > y { return "acute" }
    return "obtuse"
}
class Solution {
public:
    string triangleType(vector& nums) {
        sort(nums.begin(), nums.end());
        int a=nums[0], b=nums[1], c=nums[2];
        if (a+b<=c) return "none";
        long long x=1LL*a*a+1LL*b*b, y=1LL*c*c;
        if (x==y) return "right";
        return x>y?"acute":"obtuse";
    }
};
class Solution:
    def triangleType(self, nums: List[int]) -> str:
        nums.sort()
        a, b, c = nums
        if a + b <= c:
            return "none"
        x, y = a*a + b*b, c*c
        if x == y:
            return "right"
        return "acute" if x > y else "obtuse"
var triangleType = function(nums) {
  nums.sort((a,b)=>a-b);
  const [a,b,c] = nums;
  if (a + b <= c) return "none";
  const x = a*a + b*b, y = c*c;
  if (x === y) return "right";
  return x > y ? "acute" : "obtuse";
};

中文

给定三条边长,先判断能否成三角形(两小边之和必须大于最大边)。若不满足,返回 none。若可成三角形,比较平方关系:a²+b²=c² 为 right,a²+b²>c² 为 acute,否则为 obtuse

class Solution {
    public String triangleType(int[] nums) {
        java.util.Arrays.sort(nums);
        int a = nums[0], b = nums[1], c = nums[2];
        if (a + b <= c) return "none";
        long x = 1L * a * a + 1L * b * b;
        long y = 1L * c * c;
        if (x == y) return "right";
        return x > y ? "acute" : "obtuse";
    }
}
func triangleType(nums []int) string {
    sort.Ints(nums)
    a, b, c := nums[0], nums[1], nums[2]
    if a+b <= c { return "none" }
    x := a*a + b*b
    y := c*c
    if x == y { return "right" }
    if x > y { return "acute" }
    return "obtuse"
}
class Solution {
public:
    string triangleType(vector& nums) {
        sort(nums.begin(), nums.end());
        int a=nums[0], b=nums[1], c=nums[2];
        if (a+b<=c) return "none";
        long long x=1LL*a*a+1LL*b*b, y=1LL*c*c;
        if (x==y) return "right";
        return x>y?"acute":"obtuse";
    }
};
class Solution:
    def triangleType(self, nums: List[int]) -> str:
        nums.sort()
        a, b, c = nums
        if a + b <= c:
            return "none"
        x, y = a*a + b*b, c*c
        if x == y:
            return "right"
        return "acute" if x > y else "obtuse"
var triangleType = function(nums) {
  nums.sort((a,b)=>a-b);
  const [a,b,c] = nums;
  if (a + b <= c) return "none";
  const x = a*a + b*b, y = c*c;
  if (x === y) return "right";
  return x > y ? "acute" : "obtuse";
};

← Back to Home