File tree Expand file tree Collapse file tree 4 files changed +120
-1
lines changed
solution/1700-1799/1726.Tuple with Same Product Expand file tree Collapse file tree 4 files changed +120
-1
lines changed Original file line number Diff line number Diff line change @@ -71,10 +71,53 @@ class Solution:
71
71
return sum (v * (v - 1 ) // 2 for v in cnt.values()) << 3
72
72
```
73
73
74
- ### ** Go **
74
+ ### ** Java **
75
75
76
76
<!-- 这里可写当前语言的特殊实现逻辑 -->
77
77
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
+
78
121
```go
79
122
func tupleSameProduct(nums []int) int {
80
123
product, n := make(map[int]int), len(nums)
Original file line number Diff line number Diff line change @@ -59,6 +59,49 @@ class Solution:
59
59
return sum (v * (v - 1 ) // 2 for v in cnt.values()) << 3
60
60
```
61
61
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
+
62
105
### **Go**
63
106
64
107
```go
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments