Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ecdabef

Browse files
feat: add solutions to lc problems: No.1925,1929,1930 (doocs#508)
1 parent f50d174 commit ecdabef

File tree

38 files changed

+1817
-11
lines changed

38 files changed

+1817
-11
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# [1925. 统计平方和三元组的数目](https://leetcode-cn.com/problems/count-square-sum-triples)
2+
3+
[English Version](/solution/1900-1999/1925.Count%20Square%20Sum%20Triples/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>一个 <strong>平方和三元组</strong> <code>(a,b,c)</code> 指的是满足 <code>a<sup>2</sup> + b<sup>2</sup> = c<sup>2</sup></code> 的 <strong>整数 </strong>三元组 <code>a</code>,<code>b</code> 和 <code>c</code> 。</p>
10+
11+
<p>给你一个整数 <code>n</code> ,请你返回满足<em> </em><code>1 &lt;= a, b, c &lt;= n</code> 的 <strong>平方和三元组</strong> 的数目。</p>
12+
13+
<p> </p>
14+
15+
<p><strong>示例 1:</strong></p>
16+
17+
<pre><b>输入:</b>n = 5
18+
<b>输出:</b>2
19+
<b>解释:</b>平方和三元组为 (3,4,5) 和 (4,3,5) 。
20+
</pre>
21+
22+
<p><strong>示例 2:</strong></p>
23+
24+
<pre><b>输入:</b>n = 10
25+
<b>输出:</b>4
26+
<b>解释:</b>平方和三元组为 (3,4,5),(4,3,5),(6,8,10) 和 (8,6,10) 。
27+
</pre>
28+
29+
<p> </p>
30+
31+
<p><strong>提示:</strong></p>
32+
33+
<ul>
34+
<li><code>1 &lt;= n &lt;= 250</code></li>
35+
</ul>
36+
37+
38+
## 解法
39+
40+
<!-- 这里可写通用的实现逻辑 -->
41+
42+
<!-- tabs:start -->
43+
44+
### **Python3**
45+
46+
<!-- 这里可写当前语言的特殊实现逻辑 -->
47+
48+
```python
49+
class Solution:
50+
def countTriples(self, n: int) -> int:
51+
res = 0
52+
for a in range(1, n + 1):
53+
for b in range(1, n + 1):
54+
t = a ** 2 + b ** 2
55+
c = int(sqrt(t))
56+
if c <= n and c ** 2 == t:
57+
res += 1
58+
return res
59+
```
60+
61+
### **Java**
62+
63+
<!-- 这里可写当前语言的特殊实现逻辑 -->
64+
65+
```java
66+
class Solution {
67+
public int countTriples(int n) {
68+
int res = 0;
69+
for (int a = 1; a <= n; ++a) {
70+
for (int b = 1; b <= n; ++b) {
71+
int t = a * a + b * b;
72+
int c = (int) Math.sqrt(t);
73+
if (c <= n && c * c == t) {
74+
++res;
75+
}
76+
}
77+
}
78+
return res;
79+
}
80+
}
81+
```
82+
83+
### **C++**
84+
85+
```cpp
86+
class Solution {
87+
public:
88+
int countTriples(int n) {
89+
int res = 0;
90+
for (int a = 1; a <= n; ++a) {
91+
for (int b = 1; b <= n; ++b) {
92+
int t = a * a + b * b;
93+
int c = (int) sqrt(t);
94+
if (c <= n && c * c == t) {
95+
++res;
96+
}
97+
}
98+
}
99+
return res;
100+
}
101+
};
102+
```
103+
104+
### **Go**
105+
106+
```go
107+
func countTriples(n int) int {
108+
res := 0
109+
for a := 1; a <= n; a++ {
110+
for b := 1; b <= n; b++ {
111+
t := a*a + b*b
112+
c := int(math.Sqrt(float64(t)))
113+
if c <= n && c*c == t {
114+
res++
115+
}
116+
}
117+
}
118+
return res
119+
}
120+
```
121+
122+
### **...**
123+
124+
```
125+
126+
```
127+
128+
<!-- tabs:end -->
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# [1925. Count Square Sum Triples](https://leetcode.com/problems/count-square-sum-triples)
2+
3+
[中文文档](/solution/1900-1999/1925.Count%20Square%20Sum%20Triples/README.md)
4+
5+
## Description
6+
7+
<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>
8+
9+
<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>
10+
11+
<p>&nbsp;</p>
12+
<p><strong>Example 1:</strong></p>
13+
14+
<pre>
15+
<strong>Input:</strong> n = 5
16+
<strong>Output:</strong> 2
17+
<strong>Explanation</strong>: The square triples are (3,4,5) and (4,3,5).
18+
</pre>
19+
20+
<p><strong>Example 2:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> n = 10
24+
<strong>Output:</strong> 4
25+
<strong>Explanation</strong>: The square triples are (3,4,5), (4,3,5), (6,8,10), and (8,6,10).
26+
</pre>
27+
28+
<p>&nbsp;</p>
29+
<p><strong>Constraints:</strong></p>
30+
31+
<ul>
32+
<li><code>1 &lt;= n &lt;= 250</code></li>
33+
</ul>
34+
35+
36+
## Solutions
37+
38+
<!-- tabs:start -->
39+
40+
### **Python3**
41+
42+
```python
43+
class Solution:
44+
def countTriples(self, n: int) -> int:
45+
res = 0
46+
for a in range(1, n + 1):
47+
for b in range(1, n + 1):
48+
t = a ** 2 + b ** 2
49+
c = int(sqrt(t))
50+
if c <= n and c ** 2 == t:
51+
res += 1
52+
return res
53+
```
54+
55+
### **Java**
56+
57+
```java
58+
class Solution {
59+
public int countTriples(int n) {
60+
int res = 0;
61+
for (int a = 1; a <= n; ++a) {
62+
for (int b = 1; b <= n; ++b) {
63+
int t = a * a + b * b;
64+
int c = (int) Math.sqrt(t);
65+
if (c <= n && c * c == t) {
66+
++res;
67+
}
68+
}
69+
}
70+
return res;
71+
}
72+
}
73+
```
74+
75+
### **C++**
76+
77+
```cpp
78+
class Solution {
79+
public:
80+
int countTriples(int n) {
81+
int res = 0;
82+
for (int a = 1; a <= n; ++a) {
83+
for (int b = 1; b <= n; ++b) {
84+
int t = a * a + b * b;
85+
int c = (int) sqrt(t);
86+
if (c <= n && c * c == t) {
87+
++res;
88+
}
89+
}
90+
}
91+
return res;
92+
}
93+
};
94+
```
95+
96+
### **Go**
97+
98+
```go
99+
func countTriples(n int) int {
100+
res := 0
101+
for a := 1; a <= n; a++ {
102+
for b := 1; b <= n; b++ {
103+
t := a*a + b*b
104+
c := int(math.Sqrt(float64(t)))
105+
if c <= n && c*c == t {
106+
res++
107+
}
108+
}
109+
}
110+
return res
111+
}
112+
```
113+
114+
### **...**
115+
116+
```
117+
118+
```
119+
120+
<!-- tabs:end -->
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int countTriples(int n) {
4+
int res = 0;
5+
for (int a = 1; a <= n; ++a) {
6+
for (int b = 1; b <= n; ++b) {
7+
int t = a * a + b * b;
8+
int c = (int) sqrt(t);
9+
if (c <= n && c * c == t) {
10+
++res;
11+
}
12+
}
13+
}
14+
return res;
15+
}
16+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func countTriples(n int) int {
2+
res := 0
3+
for a := 1; a <= n; a++ {
4+
for b := 1; b <= n; b++ {
5+
t := a*a + b*b
6+
c := int(math.Sqrt(float64(t)))
7+
if c <= n && c*c == t {
8+
res++
9+
}
10+
}
11+
}
12+
return res
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int countTriples(int n) {
3+
int res = 0;
4+
for (int a = 1; a <= n; ++a) {
5+
for (int b = 1; b <= n; ++b) {
6+
int t = a * a + b * b;
7+
int c = (int) Math.sqrt(t);
8+
if (c <= n && c * c == t) {
9+
++res;
10+
}
11+
}
12+
}
13+
return res;
14+
}
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def countTriples(self, n: int) -> int:
3+
res = 0
4+
for a in range(1, n + 1):
5+
for b in range(1, n + 1):
6+
t = a ** 2 + b ** 2
7+
c = int(sqrt(t))
8+
if c <= n and c ** 2 == t:
9+
res += 1
10+
return res

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /