Skip to content

feat: add solutions to lc problem: No.1925 #3518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 57 additions & 34 deletions solution/1900-1999/1925.Count Square Sum Triples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:枚举

我们在 $[1, n)$ 的范围内枚举 $a$ 和 $b$,然后计算 $c = \sqrt{a^2 + b^2}$,如果 $c$ 是整数且 $c \leq n$,那么就找到了一个平方和三元组,答案加一。

枚举结束后,返回答案即可。

时间复杂度 $O(n^2)$,其中 $n$ 是给定的整数。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand All @@ -62,32 +68,32 @@ tags:
```python
class Solution:
def countTriples(self, n: int) -> int:
res = 0
for a in range(1, n + 1):
for b in range(1, n + 1):
t = a**2 + b**2
c = int(sqrt(t))
if c <= n and c**2 == t:
res += 1
return res
ans = 0
for a in range(1, n):
for b in range(1, n):
x = a * a + b * b
c = int(sqrt(x))
if c <= n and c * c == x:
ans += 1
return ans
```

#### Java

```java
class Solution {
public int countTriples(int n) {
int res = 0;
for (int a = 1; a <= n; ++a) {
for (int b = 1; b <= n; ++b) {
int t = a * a + b * b;
int c = (int) Math.sqrt(t);
if (c <= n && c * c == t) {
++res;
int ans = 0;
for (int a = 1; a < n; a++) {
for (int b = 1; b < n; b++) {
int x = a * a + b * b;
int c = (int) Math.sqrt(x);
if (c <= n && c * c == x) {
ans++;
}
}
}
return res;
return ans;
}
}
```
Expand All @@ -98,36 +104,53 @@ class Solution {
class Solution {
public:
int countTriples(int n) {
int res = 0;
for (int a = 1; a <= n; ++a) {
for (int b = 1; b <= n; ++b) {
int t = a * a + b * b;
int c = (int) sqrt(t);
if (c <= n && c * c == t) {
++res;
int ans = 0;
for (int a = 1; a < n; ++a) {
for (int b = 1; b < n; ++b) {
int x = a * a + b * b;
int c = static_cast<int>(sqrt(x));
if (c <= n && c * c == x) {
++ans;
}
}
}
return res;
return ans;
}
};
```

#### Go

```go
func countTriples(n int) int {
res := 0
for a := 1; a <= n; a++ {
for b := 1; b <= n; b++ {
t := a*a + b*b
c := int(math.Sqrt(float64(t)))
if c <= n && c*c == t {
res++
func countTriples(n int) (ans int) {
for a := 1; a < n; a++ {
for b := 1; b < n; b++ {
x := a*a + b*b
c := int(math.Sqrt(float64(x)))
if c <= n && c*c == x {
ans++
}
}
}
return res
return
}
```

#### TypeScript

```ts
function countTriples(n: number): number {
let ans = 0;
for (let a = 1; a < n; a++) {
for (let b = 1; b < n; b++) {
const x = a * a + b * b;
const c = Math.floor(Math.sqrt(x));
if (c <= n && c * c === x) {
ans++;
}
}
}
return ans;
}
```

Expand Down
91 changes: 57 additions & 34 deletions solution/1900-1999/1925.Count Square Sum Triples/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Enumeration

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.

After the enumeration is complete, return the answer.

The time complexity is $O(n^2)$, where $n$ is the given integer. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand All @@ -62,32 +68,32 @@ tags:
```python
class Solution:
def countTriples(self, n: int) -> int:
res = 0
for a in range(1, n + 1):
for b in range(1, n + 1):
t = a**2 + b**2
c = int(sqrt(t))
if c <= n and c**2 == t:
res += 1
return res
ans = 0
for a in range(1, n):
for b in range(1, n):
x = a * a + b * b
c = int(sqrt(x))
if c <= n and c * c == x:
ans += 1
return ans
```

#### Java

```java
class Solution {
public int countTriples(int n) {
int res = 0;
for (int a = 1; a <= n; ++a) {
for (int b = 1; b <= n; ++b) {
int t = a * a + b * b;
int c = (int) Math.sqrt(t);
if (c <= n && c * c == t) {
++res;
int ans = 0;
for (int a = 1; a < n; a++) {
for (int b = 1; b < n; b++) {
int x = a * a + b * b;
int c = (int) Math.sqrt(x);
if (c <= n && c * c == x) {
ans++;
}
}
}
return res;
return ans;
}
}
```
Expand All @@ -98,36 +104,53 @@ class Solution {
class Solution {
public:
int countTriples(int n) {
int res = 0;
for (int a = 1; a <= n; ++a) {
for (int b = 1; b <= n; ++b) {
int t = a * a + b * b;
int c = (int) sqrt(t);
if (c <= n && c * c == t) {
++res;
int ans = 0;
for (int a = 1; a < n; ++a) {
for (int b = 1; b < n; ++b) {
int x = a * a + b * b;
int c = static_cast<int>(sqrt(x));
if (c <= n && c * c == x) {
++ans;
}
}
}
return res;
return ans;
}
};
```

#### Go

```go
func countTriples(n int) int {
res := 0
for a := 1; a <= n; a++ {
for b := 1; b <= n; b++ {
t := a*a + b*b
c := int(math.Sqrt(float64(t)))
if c <= n && c*c == t {
res++
func countTriples(n int) (ans int) {
for a := 1; a < n; a++ {
for b := 1; b < n; b++ {
x := a*a + b*b
c := int(math.Sqrt(float64(x)))
if c <= n && c*c == x {
ans++
}
}
}
return res
return
}
```

#### TypeScript

```ts
function countTriples(n: number): number {
let ans = 0;
for (let a = 1; a < n; a++) {
for (let b = 1; b < n; b++) {
const x = a * a + b * b;
const c = Math.floor(Math.sqrt(x));
if (c <= n && c * c === x) {
ans++;
}
}
}
return ans;
}
```

Expand Down
18 changes: 9 additions & 9 deletions solution/1900-1999/1925.Count Square Sum Triples/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
class Solution {
public:
int countTriples(int n) {
int res = 0;
for (int a = 1; a <= n; ++a) {
for (int b = 1; b <= n; ++b) {
int t = a * a + b * b;
int c = (int) sqrt(t);
if (c <= n && c * c == t) {
++res;
int ans = 0;
for (int a = 1; a < n; ++a) {
for (int b = 1; b < n; ++b) {
int x = a * a + b * b;
int c = static_cast<int>(sqrt(x));
if (c <= n && c * c == x) {
++ans;
}
}
}
return res;
return ans;
}
};
};
19 changes: 9 additions & 10 deletions solution/1900-1999/1925.Count Square Sum Triples/Solution.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
func countTriples(n int) int {
res := 0
for a := 1; a <= n; a++ {
for b := 1; b <= n; b++ {
t := a*a + b*b
c := int(math.Sqrt(float64(t)))
if c <= n && c*c == t {
res++
func countTriples(n int) (ans int) {
for a := 1; a < n; a++ {
for b := 1; b < n; b++ {
x := a*a + b*b
c := int(math.Sqrt(float64(x)))
if c <= n && c*c == x {
ans++
}
}
}
return res
}
return
}
18 changes: 9 additions & 9 deletions solution/1900-1999/1925.Count Square Sum Triples/Solution.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
class Solution {
public int countTriples(int n) {
int res = 0;
for (int a = 1; a <= n; ++a) {
for (int b = 1; b <= n; ++b) {
int t = a * a + b * b;
int c = (int) Math.sqrt(t);
if (c <= n && c * c == t) {
++res;
int ans = 0;
for (int a = 1; a < n; a++) {
for (int b = 1; b < n; b++) {
int x = a * a + b * b;
int c = (int) Math.sqrt(x);
if (c <= n && c * c == x) {
ans++;
}
}
}
return res;
return ans;
}
}
}
16 changes: 8 additions & 8 deletions solution/1900-1999/1925.Count Square Sum Triples/Solution.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class Solution:
def countTriples(self, n: int) -> int:
res = 0
for a in range(1, n + 1):
for b in range(1, n + 1):
t = a**2 + b**2
c = int(sqrt(t))
if c <= n and c**2 == t:
res += 1
return res
ans = 0
for a in range(1, n):
for b in range(1, n):
x = a * a + b * b
c = int(sqrt(x))
if c <= n and c * c == x:
ans += 1
return ans
13 changes: 13 additions & 0 deletions solution/1900-1999/1925.Count Square Sum Triples/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function countTriples(n: number): number {
let ans = 0;
for (let a = 1; a < n; a++) {
for (let b = 1; b < n; b++) {
const x = a * a + b * b;
const c = Math.floor(Math.sqrt(x));
if (c <= n && c * c === x) {
ans++;
}
}
}
return ans;
}
Loading