diff --git a/leetcode/1901-2000/1925.Count-Square-Sum-Triples/README.md b/leetcode/1901-2000/1925.Count-Square-Sum-Triples/README.md index a997cf5cf..3683be278 100755 --- a/leetcode/1901-2000/1925.Count-Square-Sum-Triples/README.md +++ b/leetcode/1901-2000/1925.Count-Square-Sum-Triples/README.md @@ -1,28 +1,25 @@ # [1925.Count Square Sum Triples][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 +A **square triple** `(a,b,c)` is a triple where `a`, `b`, and `c` are **integers** and `a^2 + b^2 = c^2`. + +Given an integer `n`, return the number of **square triples** such that `1 <= a, b, c <= n`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: n = 5 +Output: 2 +Explanation: The square triples are (3,4,5) and (4,3,5). ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Count Square Sum Triples -```go ``` - +Input: n = 10 +Output: 4 +Explanation: The square triples are (3,4,5), (4,3,5), (6,8,10), and (8,6,10). +``` ## 结语 diff --git a/leetcode/1901-2000/1925.Count-Square-Sum-Triples/Solution.go b/leetcode/1901-2000/1925.Count-Square-Sum-Triples/Solution.go index d115ccf5e..bbb3b6093 100644 --- a/leetcode/1901-2000/1925.Count-Square-Sum-Triples/Solution.go +++ b/leetcode/1901-2000/1925.Count-Square-Sum-Triples/Solution.go @@ -1,5 +1,18 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(n int) int { + cache := make(map[int]struct{}) + for i := 1; i <= n; i++ { + cache[i*i] = struct{}{} + } + + var ret int + for i := 1; i <= n-1; i++ { + for j := i + 1; j <= n; j++ { + if _, ok := cache[i*i+j*j]; ok { + ret += 2 + } + } + } + return ret } diff --git a/leetcode/1901-2000/1925.Count-Square-Sum-Triples/Solution_test.go b/leetcode/1901-2000/1925.Count-Square-Sum-Triples/Solution_test.go index 14ff50eb4..8fb31958b 100644 --- a/leetcode/1901-2000/1925.Count-Square-Sum-Triples/Solution_test.go +++ b/leetcode/1901-2000/1925.Count-Square-Sum-Triples/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", 5, 2}, + {"TestCase2", 10, 4}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }