-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
61 lines (56 loc) · 1.89 KB
/
main.go
File metadata and controls
61 lines (56 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Source: https://leetcode.com/problems/type-of-triangle
// Title: Type of Triangle
// Difficulty: Easy
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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`
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
import "slices"
func triangleType(nums []int) string {
slices.Sort(nums)
a, b, c := nums[0], nums[1], nums[2]
if a+b <= c {
return "none"
}
if a == b && b == c {
return "equilateral"
}
if a == b || b == c {
return "isosceles"
}
return "scalene"
}