From dd3cec6727155faf651a85a969c1b78667e74601 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Mon, 19 May 2025 08:59:06 +0800 Subject: [PATCH] Add solution and test-cases for problem 3024 --- .../3001-3100/3024.Type-of-Triangle/README.md | 35 +++++++++++-------- .../3024.Type-of-Triangle/Solution.go | 13 +++++-- .../3024.Type-of-Triangle/Solution_test.go | 13 ++++--- 3 files changed, 38 insertions(+), 23 deletions(-) diff --git a/leetcode/3001-3100/3024.Type-of-Triangle/README.md b/leetcode/3001-3100/3024.Type-of-Triangle/README.md index a7c5e1bb2..bb1d74f5d 100755 --- a/leetcode/3001-3100/3024.Type-of-Triangle/README.md +++ b/leetcode/3001-3100/3024.Type-of-Triangle/README.md @@ -1,28 +1,35 @@ # [3024.Type of Triangle][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## 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: a = "11", b = "1" -Output: "100" +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:** -## 题解 - -### 思路1 -> ... -Type of Triangle -```go ``` - +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. +``` ## 结语 diff --git a/leetcode/3001-3100/3024.Type-of-Triangle/Solution.go b/leetcode/3001-3100/3024.Type-of-Triangle/Solution.go index d115ccf5e..5212d42c5 100644 --- a/leetcode/3001-3100/3024.Type-of-Triangle/Solution.go +++ b/leetcode/3001-3100/3024.Type-of-Triangle/Solution.go @@ -1,5 +1,14 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int) string { + if !(nums[0]+nums[1] > nums[2] && nums[0]+nums[2] > nums[1] && nums[1]+nums[2] > nums[0]) { + return "none" + } + if nums[0] == nums[1] && nums[0] == nums[2] { + return "equilateral" + } + if nums[0] == nums[1] || nums[0] == nums[2] || nums[1] == nums[2] { + return "isosceles" + } + return "scalene" } diff --git a/leetcode/3001-3100/3024.Type-of-Triangle/Solution_test.go b/leetcode/3001-3100/3024.Type-of-Triangle/Solution_test.go index 14ff50eb4..6926acccb 100644 --- a/leetcode/3001-3100/3024.Type-of-Triangle/Solution_test.go +++ b/leetcode/3001-3100/3024.Type-of-Triangle/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + expect string }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{3, 3, 3}, "equilateral"}, + {"TestCase2", []int{3, 4, 5}, "scalene"}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }