Skip to content

Commit 01ecd95

Browse files
committed
feat: add solutions to lc problem: No.1726
No.1726.Tuple with Same Product
1 parent e2c2633 commit 01ecd95

File tree

4 files changed

+120
-1
lines changed

4 files changed

+120
-1
lines changed

solution/1700-1799/1726.Tuple with Same Product/README.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,53 @@ class Solution:
7171
return sum(v * (v - 1) // 2 for v in cnt.values()) << 3
7272
```
7373

74-
### **Go**
74+
### **Java**
7575

7676
<!-- 这里可写当前语言的特殊实现逻辑 -->
7777

78+
```java
79+
class Solution {
80+
public int tupleSameProduct(int[] nums) {
81+
Map<Integer, Integer> cnt = new HashMap<>();
82+
for (int i = 1; i < nums.length; ++i) {
83+
for (int j = 0; j < i; ++j) {
84+
int x = nums[i] * nums[j];
85+
cnt.put(x, cnt.getOrDefault(x, 0) + 1);
86+
}
87+
}
88+
int ans = 0;
89+
for (int v : cnt.values()) {
90+
ans += v * (v - 1) / 2;
91+
}
92+
return ans << 3;
93+
}
94+
}
95+
```
96+
97+
### **C++**
98+
99+
```cpp
100+
class Solution {
101+
public:
102+
int tupleSameProduct(vector<int>& nums) {
103+
unordered_map<int, int> cnt;
104+
for (int i = 1; i < nums.size(); ++i) {
105+
for (int j = 0; j < i; ++j) {
106+
int x = nums[i] * nums[j];
107+
++cnt[x];
108+
}
109+
}
110+
int ans = 0;
111+
for (auto& [_, v] : cnt) {
112+
ans += v * (v - 1) / 2;
113+
}
114+
return ans << 3;
115+
}
116+
};
117+
```
118+
119+
### **Go**
120+
78121
```go
79122
func tupleSameProduct(nums []int) int {
80123
product, n := make(map[int]int), len(nums)

solution/1700-1799/1726.Tuple with Same Product/README_EN.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,49 @@ class Solution:
5959
return sum(v * (v - 1) // 2 for v in cnt.values()) << 3
6060
```
6161

62+
### **Java**
63+
64+
```java
65+
class Solution {
66+
public int tupleSameProduct(int[] nums) {
67+
Map<Integer, Integer> cnt = new HashMap<>();
68+
for (int i = 1; i < nums.length; ++i) {
69+
for (int j = 0; j < i; ++j) {
70+
int x = nums[i] * nums[j];
71+
cnt.put(x, cnt.getOrDefault(x, 0) + 1);
72+
}
73+
}
74+
int ans = 0;
75+
for (int v : cnt.values()) {
76+
ans += v * (v - 1) / 2;
77+
}
78+
return ans << 3;
79+
}
80+
}
81+
```
82+
83+
### **C++**
84+
85+
```cpp
86+
class Solution {
87+
public:
88+
int tupleSameProduct(vector<int>& nums) {
89+
unordered_map<int, int> cnt;
90+
for (int i = 1; i < nums.size(); ++i) {
91+
for (int j = 0; j < i; ++j) {
92+
int x = nums[i] * nums[j];
93+
++cnt[x];
94+
}
95+
}
96+
int ans = 0;
97+
for (auto& [_, v] : cnt) {
98+
ans += v * (v - 1) / 2;
99+
}
100+
return ans << 3;
101+
}
102+
};
103+
```
104+
62105
### **Go**
63106
64107
```go
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int tupleSameProduct(vector<int>& nums) {
4+
unordered_map<int, int> cnt;
5+
for (int i = 1; i < nums.size(); ++i) {
6+
for (int j = 0; j < i; ++j) {
7+
int x = nums[i] * nums[j];
8+
++cnt[x];
9+
}
10+
}
11+
int ans = 0;
12+
for (auto& [_, v] : cnt) {
13+
ans += v * (v - 1) / 2;
14+
}
15+
return ans << 3;
16+
}
17+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int tupleSameProduct(int[] nums) {
3+
Map<Integer, Integer> cnt = new HashMap<>();
4+
for (int i = 1; i < nums.length; ++i) {
5+
for (int j = 0; j < i; ++j) {
6+
int x = nums[i] * nums[j];
7+
cnt.put(x, cnt.getOrDefault(x, 0) + 1);
8+
}
9+
}
10+
int ans = 0;
11+
for (int v : cnt.values()) {
12+
ans += v * (v - 1) / 2;
13+
}
14+
return ans << 3;
15+
}
16+
}

0 commit comments

Comments
 (0)