Skip to content

Commit 45b816f

Browse files
committed
[LeetCode Sync] Runtime - 87 ms (86.69%), Memory - 17.7 MB (54.08%)
1 parent 9676911 commit 45b816f

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<p>A <strong>square triple</strong> <code>(a,b,c)</code> is a triple where <code>a</code>, <code>b</code>, and <code>c</code> are <strong>integers</strong> and <code>a<sup>2</sup> + b<sup>2</sup> = c<sup>2</sup></code>.</p>
2+
3+
<p>Given an integer <code>n</code>, return <em>the number of <strong>square triples</strong> such that </em><code>1 &lt;= a, b, c &lt;= n</code>.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
8+
<pre>
9+
<strong>Input:</strong> n = 5
10+
<strong>Output:</strong> 2
11+
<strong>Explanation</strong>: The square triples are (3,4,5) and (4,3,5).
12+
</pre>
13+
14+
<p><strong class="example">Example 2:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> n = 10
18+
<strong>Output:</strong> 4
19+
<strong>Explanation</strong>: The square triples are (3,4,5), (4,3,5), (6,8,10), and (8,6,10).
20+
</pre>
21+
22+
<p>&nbsp;</p>
23+
<p><strong>Constraints:</strong></p>
24+
25+
<ul>
26+
<li><code>1 &lt;= n &lt;= 250</code></li>
27+
</ul>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def countTriples(self, n: int) -> int:
3+
# result = 0
4+
# for a in range(1, n):
5+
# for b in range(1, n):
6+
# val = a**2 + b**2
7+
# c = int(sqrt(val))
8+
# if c <= n and val == c**2:
9+
# result += 1
10+
# return result
11+
12+
result = 0
13+
squares = set([i**2 for i in range(1, n + 1)])
14+
for a in range(1, n + 1):
15+
for b in range(a + 1, n + 1):
16+
if a**2 + b**2 in squares:
17+
result += 2
18+
return result

0 commit comments

Comments
 (0)