From 534ef6458d18a383b14da7af4a9e48761e8b87cd Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Sun, 28 Sep 2025 16:53:36 +0800 Subject: [PATCH] Add solution and test-cases for problem 976 --- .../0976.Largest-Perimeter-Triangle/README.md | 27 +++++++++---------- .../Solution.go | 15 +++++++++-- .../Solution_test.go | 13 +++++---- 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/leetcode/901-1000/0976.Largest-Perimeter-Triangle/README.md b/leetcode/901-1000/0976.Largest-Perimeter-Triangle/README.md index 694ba4d5c..fe648f27d 100644 --- a/leetcode/901-1000/0976.Largest-Perimeter-Triangle/README.md +++ b/leetcode/901-1000/0976.Largest-Perimeter-Triangle/README.md @@ -1,28 +1,27 @@ # [976.Largest Perimeter 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 +Given an integer array `nums`, return the largest perimeter of a triangle with a non-zero area, formed from three of these lengths. If it is impossible to form any triangle of a non-zero area, return `0`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [2,1,2] +Output: 5 +Explanation: You can form a triangle with three side lengths: 1, 2, and 2. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Largest Perimeter Triangle -```go ``` - +Input: nums = [1,2,1,10] +Output: 0 +Explanation: +You cannot use the side lengths 1, 1, and 2 to form a triangle. +You cannot use the side lengths 1, 1, and 10 to form a triangle. +You cannot use the side lengths 1, 2, and 10 to form a triangle. +As we cannot use any three side lengths to form a triangle of non-zero area, we return 0. +``` ## 结语 diff --git a/leetcode/901-1000/0976.Largest-Perimeter-Triangle/Solution.go b/leetcode/901-1000/0976.Largest-Perimeter-Triangle/Solution.go index d115ccf5e..d0fd8da2c 100644 --- a/leetcode/901-1000/0976.Largest-Perimeter-Triangle/Solution.go +++ b/leetcode/901-1000/0976.Largest-Perimeter-Triangle/Solution.go @@ -1,5 +1,16 @@ package Solution -func Solution(x bool) bool { - return x +import "sort" + +func Solution(nums []int) int { + sort.Ints(nums) + // 1, 1, 2, 10 + l := len(nums) + for i := l - 1; i >= 2; i-- { + a, b, c := nums[i], nums[i-1], nums[i-2] + if a+b > c && a+c > b && b+c > a { + return a + b + c + } + } + return 0 } diff --git a/leetcode/901-1000/0976.Largest-Perimeter-Triangle/Solution_test.go b/leetcode/901-1000/0976.Largest-Perimeter-Triangle/Solution_test.go index 14ff50eb4..27a8f0aab 100644 --- a/leetcode/901-1000/0976.Largest-Perimeter-Triangle/Solution_test.go +++ b/leetcode/901-1000/0976.Largest-Perimeter-Triangle/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{2, 1, 2}, 5}, + {"TestCase2", []int{1, 2, 1, 10}, 0}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }