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 a7de3e5

Browse files
feat: add solutions to lc problem: No.1925 (doocs#3518)
1 parent 4e1931e commit a7de3e5

File tree

7 files changed

+162
-104
lines changed

7 files changed

+162
-104
lines changed

‎solution/1900-1999/1925.Count Square Sum Triples/README.md‎

Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,13 @@ tags:
5353

5454
<!-- solution:start -->
5555

56-
### 方法一
56+
### 方法一:枚举
57+
58+
我们在 $[1, n)$ 的范围内枚举 $a$ 和 $b,ドル然后计算 $c = \sqrt{a^2 + b^2},ドル如果 $c$ 是整数且 $c \leq n,ドル那么就找到了一个平方和三元组,答案加一。
59+
60+
枚举结束后,返回答案即可。
61+
62+
时间复杂度 $O(n^2),ドル其中 $n$ 是给定的整数。空间复杂度 $O(1)$。
5763

5864
<!-- tabs:start -->
5965

@@ -62,32 +68,32 @@ tags:
6268
```python
6369
class Solution:
6470
def countTriples(self, n: int) -> int:
65-
res = 0
66-
for a in range(1, n+1):
67-
for b in range(1, n+1):
68-
t = a**2+ b**2
69-
c = int(sqrt(t))
70-
if c <= n and c**2== t:
71-
res += 1
72-
return res
71+
ans = 0
72+
for a in range(1, n):
73+
for b in range(1, n):
74+
x = a* a + b* b
75+
c = int(sqrt(x))
76+
if c <= n and c* c == x:
77+
ans += 1
78+
return ans
7379
```
7480

7581
#### Java
7682

7783
```java
7884
class Solution {
7985
public int countTriples(int n) {
80-
int res = 0;
81-
for (int a = 1; a <= n; ++a) {
82-
for (int b = 1; b <= n; ++b) {
83-
int t = a * a + b * b;
84-
int c = (int) Math.sqrt(t);
85-
if (c <= n && c * c == t) {
86-
++res;
86+
int ans = 0;
87+
for (int a = 1; a < n; a++) {
88+
for (int b = 1; b < n; b++) {
89+
int x = a * a + b * b;
90+
int c = (int) Math.sqrt(x);
91+
if (c <= n && c * c == x) {
92+
ans++;
8793
}
8894
}
8995
}
90-
return res;
96+
return ans;
9197
}
9298
}
9399
```
@@ -98,36 +104,53 @@ class Solution {
98104
class Solution {
99105
public:
100106
int countTriples(int n) {
101-
int res = 0;
102-
for (int a = 1; a <= n; ++a) {
103-
for (int b = 1; b <= n; ++b) {
104-
int t = a * a + b * b;
105-
int c = (int) sqrt(t);
106-
if (c <= n && c * c == t) {
107-
++res;
107+
int ans = 0;
108+
for (int a = 1; a < n; ++a) {
109+
for (int b = 1; b < n; ++b) {
110+
int x = a * a + b * b;
111+
int c = static_cast<int>(sqrt(x));
112+
if (c <= n && c * c == x) {
113+
++ans;
108114
}
109115
}
110116
}
111-
return res;
117+
return ans;
112118
}
113119
};
114120
```
115121
116122
#### Go
117123
118124
```go
119-
func countTriples(n int) int {
120-
res := 0
121-
for a := 1; a <= n; a++ {
122-
for b := 1; b <= n; b++ {
123-
t := a*a + b*b
124-
c := int(math.Sqrt(float64(t)))
125-
if c <= n && c*c == t {
126-
res++
125+
func countTriples(n int) (ans int) {
126+
for a := 1; a < n; a++ {
127+
for b := 1; b < n; b++ {
128+
x := a*a + b*b
129+
c := int(math.Sqrt(float64(x)))
130+
if c <= n && c*c == x {
131+
ans++
127132
}
128133
}
129134
}
130-
return res
135+
return
136+
}
137+
```
138+
139+
#### TypeScript
140+
141+
```ts
142+
function countTriples(n: number): number {
143+
let ans = 0;
144+
for (let a = 1; a < n; a++) {
145+
for (let b = 1; b < n; b++) {
146+
const x = a * a + b * b;
147+
const c = Math.floor(Math.sqrt(x));
148+
if (c <= n && c * c === x) {
149+
ans++;
150+
}
151+
}
152+
}
153+
return ans;
131154
}
132155
```
133156

‎solution/1900-1999/1925.Count Square Sum Triples/README_EN.md‎

Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,13 @@ tags:
5353

5454
<!-- solution:start -->
5555

56-
### Solution 1
56+
### Solution 1: Enumeration
57+
58+
We enumerate $a$ and $b$ in the range $[1, n),ドル then calculate $c = \sqrt{a^2 + b^2}$. If $c$ is an integer and $c \leq n,ドル then we have found a Pythagorean triplet, and we increment the answer by one.
59+
60+
After the enumeration is complete, return the answer.
61+
62+
The time complexity is $O(n^2),ドル where $n$ is the given integer. The space complexity is $O(1)$.
5763

5864
<!-- tabs:start -->
5965

@@ -62,32 +68,32 @@ tags:
6268
```python
6369
class Solution:
6470
def countTriples(self, n: int) -> int:
65-
res = 0
66-
for a in range(1, n+1):
67-
for b in range(1, n+1):
68-
t = a**2+ b**2
69-
c = int(sqrt(t))
70-
if c <= n and c**2== t:
71-
res += 1
72-
return res
71+
ans = 0
72+
for a in range(1, n):
73+
for b in range(1, n):
74+
x = a* a + b* b
75+
c = int(sqrt(x))
76+
if c <= n and c* c == x:
77+
ans += 1
78+
return ans
7379
```
7480

7581
#### Java
7682

7783
```java
7884
class Solution {
7985
public int countTriples(int n) {
80-
int res = 0;
81-
for (int a = 1; a <= n; ++a) {
82-
for (int b = 1; b <= n; ++b) {
83-
int t = a * a + b * b;
84-
int c = (int) Math.sqrt(t);
85-
if (c <= n && c * c == t) {
86-
++res;
86+
int ans = 0;
87+
for (int a = 1; a < n; a++) {
88+
for (int b = 1; b < n; b++) {
89+
int x = a * a + b * b;
90+
int c = (int) Math.sqrt(x);
91+
if (c <= n && c * c == x) {
92+
ans++;
8793
}
8894
}
8995
}
90-
return res;
96+
return ans;
9197
}
9298
}
9399
```
@@ -98,36 +104,53 @@ class Solution {
98104
class Solution {
99105
public:
100106
int countTriples(int n) {
101-
int res = 0;
102-
for (int a = 1; a <= n; ++a) {
103-
for (int b = 1; b <= n; ++b) {
104-
int t = a * a + b * b;
105-
int c = (int) sqrt(t);
106-
if (c <= n && c * c == t) {
107-
++res;
107+
int ans = 0;
108+
for (int a = 1; a < n; ++a) {
109+
for (int b = 1; b < n; ++b) {
110+
int x = a * a + b * b;
111+
int c = static_cast<int>(sqrt(x));
112+
if (c <= n && c * c == x) {
113+
++ans;
108114
}
109115
}
110116
}
111-
return res;
117+
return ans;
112118
}
113119
};
114120
```
115121
116122
#### Go
117123
118124
```go
119-
func countTriples(n int) int {
120-
res := 0
121-
for a := 1; a <= n; a++ {
122-
for b := 1; b <= n; b++ {
123-
t := a*a + b*b
124-
c := int(math.Sqrt(float64(t)))
125-
if c <= n && c*c == t {
126-
res++
125+
func countTriples(n int) (ans int) {
126+
for a := 1; a < n; a++ {
127+
for b := 1; b < n; b++ {
128+
x := a*a + b*b
129+
c := int(math.Sqrt(float64(x)))
130+
if c <= n && c*c == x {
131+
ans++
127132
}
128133
}
129134
}
130-
return res
135+
return
136+
}
137+
```
138+
139+
#### TypeScript
140+
141+
```ts
142+
function countTriples(n: number): number {
143+
let ans = 0;
144+
for (let a = 1; a < n; a++) {
145+
for (let b = 1; b < n; b++) {
146+
const x = a * a + b * b;
147+
const c = Math.floor(Math.sqrt(x));
148+
if (c <= n && c * c === x) {
149+
ans++;
150+
}
151+
}
152+
}
153+
return ans;
131154
}
132155
```
133156

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
class Solution {
22
public:
33
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;
4+
int ans = 0;
5+
for (int a = 1; a < n; ++a) {
6+
for (int b = 1; b < n; ++b) {
7+
int x = a * a + b * b;
8+
int c = static_cast<int>(sqrt(x));
9+
if (c <= n && c * c == x) {
10+
++ans;
1111
}
1212
}
1313
}
14-
return res;
14+
return ans;
1515
}
16-
};
16+
};
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
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++
1+
func countTriples(n int) (ans int) {
2+
for a := 1; a < n; a++ {
3+
for b := 1; b < n; b++ {
4+
x := a*a + b*b
5+
c := int(math.Sqrt(float64(x)))
6+
if c <= n && c*c == x {
7+
ans++
98
}
109
}
1110
}
12-
returnres
13-
}
11+
return
12+
}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
class Solution {
22
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;
3+
int ans = 0;
4+
for (int a = 1; a < n; a++) {
5+
for (int b = 1; b < n; b++) {
6+
int x = a * a + b * b;
7+
int c = (int) Math.sqrt(x);
8+
if (c <= n && c * c == x) {
9+
ans++;
1010
}
1111
}
1212
}
13-
return res;
13+
return ans;
1414
}
15-
}
15+
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
class Solution:
22
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
3+
ans = 0
4+
for a in range(1, n):
5+
for b in range(1, n):
6+
x = a*a+ b*b
7+
c = int(sqrt(x))
8+
if c <= n and c*c== x:
9+
ans += 1
10+
return ans
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function countTriples(n: number): number {
2+
let ans = 0;
3+
for (let a = 1; a < n; a++) {
4+
for (let b = 1; b < n; b++) {
5+
const x = a * a + b * b;
6+
const c = Math.floor(Math.sqrt(x));
7+
if (c <= n && c * c === x) {
8+
ans++;
9+
}
10+
}
11+
}
12+
return ans;
13+
}

0 commit comments

Comments
(0)

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