Skip to content

feat: update solutions to lc problem: No.2176 #3526

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 14, 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
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ tags:

<!-- solution:start -->

### 方法一:暴力枚举
### 方法一:枚举

我们先在 $[0, n)$ 的范围内枚举下标 $j$,然后在 $[0, j)$ 的范围内枚举下标 $i$,统计满足 $\textit{nums}[i] = \textit{nums}[j]$ 且 $(i \times j) \bmod k = 0$ 的数对个数。

时间复杂度 $O(n^2)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand All @@ -65,26 +69,22 @@ tags:
```python
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
n = len(nums)
return sum(
nums[i] == nums[j] and (i * j) % k == 0
for i in range(n)
for j in range(i + 1, n)
)
ans = 0
for j, y in enumerate(nums):
for i, x in enumerate(nums[:j]):
ans += int(x == y and i * j % k == 0)
return ans
```

#### Java

```java
class Solution {
public int countPairs(int[] nums, int k) {
int n = nums.length;
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (nums[i] == nums[j] && (i * j) % k == 0) {
++ans;
}
for (int j = 1; j < nums.length; ++j) {
for (int i = 0; i < j; ++i) {
ans += nums[i] == nums[j] && (i * j % k) == 0 ? 1 : 0;
}
}
return ans;
Expand All @@ -98,11 +98,10 @@ class Solution {
class Solution {
public:
int countPairs(vector<int>& nums, int k) {
int n = nums.size();
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (nums[i] == nums[j] && (i * j) % k == 0) ++ans;
for (int j = 1; j < nums.size(); ++j) {
for (int i = 0; i < j; ++i) {
ans += nums[i] == nums[j] && (i * j % k) == 0;
}
}
return ans;
Expand All @@ -113,30 +112,27 @@ public:
#### Go

```go
func countPairs(nums []int, k int) int {
n := len(nums)
ans := 0
for i, v := range nums {
for j := i + 1; j < n; j++ {
if v == nums[j] && (i*j)%k == 0 {
func countPairs(nums []int, k int) (ans int) {
for j, y := range nums {
for i, x := range nums[:j] {
if x == y && (i*j%k) == 0 {
ans++
}
}
}
return ans
return
}
```

#### TypeScript

```ts
function countPairs(nums: number[], k: number): number {
const n = nums.length;
let ans = 0;
for (let i = 0; i < n - 1; i++) {
for (let j = i + 1; j < n; j++) {
for (let j = 1; j < nums.length; ++j) {
for (let i = 0; i < j; ++i) {
if (nums[i] === nums[j] && (i * j) % k === 0) {
ans++;
++ans;
}
}
}
Expand All @@ -149,12 +145,10 @@ function countPairs(nums: number[], k: number): number {
```rust
impl Solution {
pub fn count_pairs(nums: Vec<i32>, k: i32) -> i32 {
let k = k as usize;
let n = nums.len();
let mut ans = 0;
for i in 0..n - 1 {
for j in i + 1..n {
if nums[i] == nums[j] && (i * j) % k == 0 {
for j in 1..nums.len() {
for (i, &x) in nums[..j].iter().enumerate() {
if x == nums[j] && (i * j) as i32 % k == 0 {
ans += 1;
}
}
Expand All @@ -169,11 +163,9 @@ impl Solution {
```c
int countPairs(int* nums, int numsSize, int k) {
int ans = 0;
for (int i = 0; i < numsSize - 1; i++) {
for (int j = i + 1; j < numsSize; j++) {
if (nums[i] == nums[j] && i * j % k == 0) {
ans++;
}
for (int j = 1; j < numsSize; ++j) {
for (int i = 0; i < j; ++i) {
ans += (nums[i] == nums[j] && (i * j % k) == 0);
}
}
return ans;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ There are 4 pairs that meet all the requirements:

<!-- solution:start -->

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

We first enumerate the index $j$ in the range $[0, n)$, and then enumerate the index $i$ in the range $[0, j)$. We count the number of pairs that satisfy $\textit{nums}[i] = \textit{nums}[j]$ and $(i \times j) \bmod k = 0$.

The time complexity is $O(n^2)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand All @@ -65,26 +69,22 @@ There are 4 pairs that meet all the requirements:
```python
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
n = len(nums)
return sum(
nums[i] == nums[j] and (i * j) % k == 0
for i in range(n)
for j in range(i + 1, n)
)
ans = 0
for j, y in enumerate(nums):
for i, x in enumerate(nums[:j]):
ans += int(x == y and i * j % k == 0)
return ans
```

#### Java

```java
class Solution {
public int countPairs(int[] nums, int k) {
int n = nums.length;
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (nums[i] == nums[j] && (i * j) % k == 0) {
++ans;
}
for (int j = 1; j < nums.length; ++j) {
for (int i = 0; i < j; ++i) {
ans += nums[i] == nums[j] && (i * j % k) == 0 ? 1 : 0;
}
}
return ans;
Expand All @@ -98,11 +98,10 @@ class Solution {
class Solution {
public:
int countPairs(vector<int>& nums, int k) {
int n = nums.size();
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (nums[i] == nums[j] && (i * j) % k == 0) ++ans;
for (int j = 1; j < nums.size(); ++j) {
for (int i = 0; i < j; ++i) {
ans += nums[i] == nums[j] && (i * j % k) == 0;
}
}
return ans;
Expand All @@ -113,30 +112,27 @@ public:
#### Go

```go
func countPairs(nums []int, k int) int {
n := len(nums)
ans := 0
for i, v := range nums {
for j := i + 1; j < n; j++ {
if v == nums[j] && (i*j)%k == 0 {
func countPairs(nums []int, k int) (ans int) {
for j, y := range nums {
for i, x := range nums[:j] {
if x == y && (i*j%k) == 0 {
ans++
}
}
}
return ans
return
}
```

#### TypeScript

```ts
function countPairs(nums: number[], k: number): number {
const n = nums.length;
let ans = 0;
for (let i = 0; i < n - 1; i++) {
for (let j = i + 1; j < n; j++) {
for (let j = 1; j < nums.length; ++j) {
for (let i = 0; i < j; ++i) {
if (nums[i] === nums[j] && (i * j) % k === 0) {
ans++;
++ans;
}
}
}
Expand All @@ -149,12 +145,10 @@ function countPairs(nums: number[], k: number): number {
```rust
impl Solution {
pub fn count_pairs(nums: Vec<i32>, k: i32) -> i32 {
let k = k as usize;
let n = nums.len();
let mut ans = 0;
for i in 0..n - 1 {
for j in i + 1..n {
if nums[i] == nums[j] && (i * j) % k == 0 {
for j in 1..nums.len() {
for (i, &x) in nums[..j].iter().enumerate() {
if x == nums[j] && (i * j) as i32 % k == 0 {
ans += 1;
}
}
Expand All @@ -169,11 +163,9 @@ impl Solution {
```c
int countPairs(int* nums, int numsSize, int k) {
int ans = 0;
for (int i = 0; i < numsSize - 1; i++) {
for (int j = i + 1; j < numsSize; j++) {
if (nums[i] == nums[j] && i * j % k == 0) {
ans++;
}
for (int j = 1; j < numsSize; ++j) {
for (int i = 0; i < j; ++i) {
ans += (nums[i] == nums[j] && (i * j % k) == 0);
}
}
return ans;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
int countPairs(int* nums, int numsSize, int k) {
int ans = 0;
for (int i = 0; i < numsSize - 1; i++) {
for (int j = i + 1; j < numsSize; j++) {
if (nums[i] == nums[j] && i * j % k == 0) {
ans++;
}
for (int j = 1; j < numsSize; ++j) {
for (int i = 0; i < j; ++i) {
ans += (nums[i] == nums[j] && (i * j % k) == 0);
}
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
class Solution {
public:
int countPairs(vector<int>& nums, int k) {
int n = nums.size();
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (nums[i] == nums[j] && (i * j) % k == 0) ++ans;
for (int j = 1; j < nums.size(); ++j) {
for (int i = 0; i < j; ++i) {
ans += nums[i] == nums[j] && (i * j % k) == 0;
}
}
return ans;
}
};
};
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
func countPairs(nums []int, k int) int {
n := len(nums)
ans := 0
for i, v := range nums {
for j := i + 1; j < n; j++ {
if v == nums[j] && (i*j)%k == 0 {
func countPairs(nums []int, k int) (ans int) {
for j, y := range nums {
for i, x := range nums[:j] {
if x == y && (i*j%k) == 0 {
ans++
}
}
}
return ans
}
return
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
class Solution {
public int countPairs(int[] nums, int k) {
int n = nums.length;
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (nums[i] == nums[j] && (i * j) % k == 0) {
++ans;
}
for (int j = 1; j < nums.length; ++j) {
for (int i = 0; i < j; ++i) {
ans += nums[i] == nums[j] && (i * j % k) == 0 ? 1 : 0;
}
}
return ans;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
n = len(nums)
return sum(
nums[i] == nums[j] and (i * j) % k == 0
for i in range(n)
for j in range(i + 1, n)
)
ans = 0
for j in range(1, len(nums)):
for i, x in enumerate(nums[:j]):
ans += int(x == nums[j] and i * j % k == 0)
return ans
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
impl Solution {
pub fn count_pairs(nums: Vec<i32>, k: i32) -> i32 {
let k = k as usize;
let n = nums.len();
let mut ans = 0;
for i in 0..n - 1 {
for j in i + 1..n {
if nums[i] == nums[j] && (i * j) % k == 0 {
for j in 1..nums.len() {
for (i, &x) in nums[..j].iter().enumerate() {
if x == nums[j] && (i * j) as i32 % k == 0 {
ans += 1;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
function countPairs(nums: number[], k: number): number {
const n = nums.length;
let ans = 0;
for (let i = 0; i < n - 1; i++) {
for (let j = i + 1; j < n; j++) {
for (let j = 1; j < nums.length; ++j) {
for (let i = 0; i < j; ++i) {
if (nums[i] === nums[j] && (i * j) % k === 0) {
ans++;
++ans;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Mathematics

Assume the three consecutive integers are $x-1$, $x$, and $x+1$. Their sum is $3x$, so $num$ must be a multiple of $3$. If $num$ is not a multiple of $3$, it cannot be expressed as the sum of three consecutive integers, and we return an empty array. Otherwise, let $x = \frac{num}{3}$, then $x-1$, $x$, and $x+1$ are the three consecutive integers whose sum is $num$.

The time complexity is $O(1)$, and the space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down
Loading