From b554d9150ddb559f2cbd86d4567226aa210567dd Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sat, 14 Sep 2024 22:47:03 +0800 Subject: [PATCH] feat: update solutions to lc problem: No.2176 --- .../README.md | 68 ++++++++----------- .../README_EN.md | 68 ++++++++----------- .../Solution.c | 10 ++- .../Solution.cpp | 9 ++- .../Solution.go | 14 ++-- .../Solution.java | 11 ++- .../Solution.py | 11 ++- .../Solution.rs | 8 +-- .../Solution.ts | 7 +- .../README_EN.md | 6 +- 10 files changed, 94 insertions(+), 118 deletions(-) diff --git a/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/README.md b/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/README.md index c615ddc16f258..49e3ff29d40ed 100644 --- a/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/README.md +++ b/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/README.md @@ -56,7 +56,11 @@ tags: -### 方法一:暴力枚举 +### 方法一:枚举 + +我们先在 $[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)$。 @@ -65,12 +69,11 @@ 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 @@ -78,13 +81,10 @@ class Solution: ```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; @@ -98,11 +98,10 @@ class Solution { class Solution { public: int countPairs(vector& 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; @@ -113,17 +112,15 @@ 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 } ``` @@ -131,12 +128,11 @@ func countPairs(nums []int, k int) int { ```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; } } } @@ -149,12 +145,10 @@ function countPairs(nums: number[], k: number): number { ```rust impl Solution { pub fn count_pairs(nums: Vec, 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; } } @@ -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; diff --git a/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/README_EN.md b/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/README_EN.md index 15fbb18d27999..95ca61deb553a 100644 --- a/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/README_EN.md +++ b/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/README_EN.md @@ -56,7 +56,11 @@ There are 4 pairs that meet all the requirements: -### 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)$. @@ -65,12 +69,11 @@ 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 @@ -78,13 +81,10 @@ class Solution: ```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; @@ -98,11 +98,10 @@ class Solution { class Solution { public: int countPairs(vector& 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; @@ -113,17 +112,15 @@ 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 } ``` @@ -131,12 +128,11 @@ func countPairs(nums []int, k int) int { ```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; } } } @@ -149,12 +145,10 @@ function countPairs(nums: number[], k: number): number { ```rust impl Solution { pub fn count_pairs(nums: Vec, 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; } } @@ -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; diff --git a/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.c b/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.c index abc891707a297..5aeaef60d5455 100644 --- a/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.c +++ b/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.c @@ -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; -} \ No newline at end of file +} diff --git a/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.cpp b/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.cpp index d7272b656926f..fd8fd36a64422 100644 --- a/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.cpp +++ b/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.cpp @@ -1,13 +1,12 @@ class Solution { public: int countPairs(vector& 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; } -}; \ No newline at end of file +}; diff --git a/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.go b/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.go index 4ce90ff4724e2..388da49a9bc5c 100644 --- a/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.go +++ b/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.go @@ -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 -} \ No newline at end of file + return +} diff --git a/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.java b/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.java index 998e40db701f1..4d3e463fa49b3 100644 --- a/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.java +++ b/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.java @@ -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; } -} \ No newline at end of file +} diff --git a/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.py b/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.py index 75dfa25de1345..281b0c6c53802 100644 --- a/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.py +++ b/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.py @@ -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 diff --git a/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.rs b/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.rs index 6f500b86e8e98..1cd8ca07a2551 100644 --- a/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.rs +++ b/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.rs @@ -1,11 +1,9 @@ impl Solution { pub fn count_pairs(nums: Vec, 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; } } diff --git a/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.ts b/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.ts index 29b79e3a7ba9e..0582401c7d17a 100644 --- a/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.ts +++ b/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.ts @@ -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; } } } diff --git a/solution/2100-2199/2177.Find Three Consecutive Integers That Sum to a Given Number/README_EN.md b/solution/2100-2199/2177.Find Three Consecutive Integers That Sum to a Given Number/README_EN.md index 0e3704a8613d0..e496c2413f7c7 100644 --- a/solution/2100-2199/2177.Find Three Consecutive Integers That Sum to a Given Number/README_EN.md +++ b/solution/2100-2199/2177.Find Three Consecutive Integers That Sum to a Given Number/README_EN.md @@ -52,7 +52,11 @@ tags: -### 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)$.