diff options
author | Jack Sangdahl <[email protected]> | 2025-05-19 16:28:17 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-05-19 16:28:17 -0600 |
commit | c585422d643252924194811d6204f773a35ca5e0 (patch) | |
tree | b6ba0ff1e6451078543e827e904d88933c8afa27 | |
parent | 87badbaba07e4cd13c2281e15c96ccf2dd8b3106 (diff) | |
download | leetcode-c585422d643252924194811d6204f773a35ca5e0.tar.gz leetcode-c585422d643252924194811d6204f773a35ca5e0.zip |
3024. type of triangle
-rw-r--r-- | type_of_triangle.c | 36 | ||||
-rw-r--r-- | type_of_triangle.py | 29 |
2 files changed, 65 insertions, 0 deletions
diff --git a/type_of_triangle.c b/type_of_triangle.c new file mode 100644 index 0000000..7f2de53 --- /dev/null +++ b/type_of_triangle.c @@ -0,0 +1,36 @@ +// 3024. Type of Triangle +#include "leetcode.h" + +/* + * you are given a 0-indexed integer array 'nums' of size 3 which can form the + * sides of a triangle. return a string representing the type of triangle that + * can be formed, or none if it cannot form a triangle. + */ + +inline void swap(int *x, int *y) { + int tmp = *x; + *x = *y; + *y = tmp; +} + +char *triangleType(int *nums, int numsSize) { + for (int i = 0; i < 2; i++) { + if (nums[2] < nums[i]) + swap(&nums[i], &nums[2]); + else if (nums[1] < nums[i]) + swap(&nums[i], &nums[1]); + } + if (nums[2] >= nums[0] + nums[1]) + return "none"; + if (nums[0] == nums[2]) + return "equilateral"; + if (nums[0] == nums[1] || nums[1] == nums[2]) + return "isosceles"; + return "scalene"; +} + +int main() { + int n1[] = {3, 3, 3}, n2[] = {3, 4, 5}; + printf("%s\n", triangleType(n1, ARRAY_SIZE(n1))); // expect: equilateral + printf("%s\n", triangleType(n2, ARRAY_SIZE(n2))); // expect: scalene +} diff --git a/type_of_triangle.py b/type_of_triangle.py new file mode 100644 index 0000000..17dd7dd --- /dev/null +++ b/type_of_triangle.py @@ -0,0 +1,29 @@ +# 3024. Type of Triangle + +""" +you are given a 0-indexed integer array 'nums' of size 3 which can form the +sides of a triangle. return a string representing the type of triangle that +can be formed, or none if it cannot form a triangle. +""" + + +class Solution(object): + def triangleType(self, nums): + """ + :type nums: List[int] + :rtype: str + """ + nums.sort() + if nums[2] >= nums[0] + nums[1]: + return "none" + if nums[0] == nums[2]: + return "equilateral" + if nums[0] == nums[1] or nums[1] == nums[2]: + return "isosceles" + return "scalene" + + +if __name__ == "__main__": + obj = Solution() + print(obj.triangleType(nums=[3, 3, 3])) + print(obj.triangleType(nums=[3, 4, 5])) |