Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita352352

Browse files
refactor 1726
1 parent3b20425 commita352352

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

‎src/main/java/com/fishercoder/solutions/_1726.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,26 @@ public int tupleSameProduct(int[] nums) {
1717
}
1818
returncount;
1919
}
20+
}
21+
22+
publicstaticclassSolution2 {
23+
publicinttupleSameProduct(int[]nums) {
24+
Map<Integer,Integer>map =newHashMap<>();
25+
for (inti =0;i <nums.length -1;i++) {
26+
for (intj =i +1;j <nums.length;j++) {
27+
intproduct =nums[i] *nums[j];
28+
map.put(product,map.getOrDefault(product,0) +1);
29+
}
30+
}
31+
intcount =0;
32+
for (intkey :map.keySet()) {
33+
if (map.get(key) >1) {
34+
intfreq =map.get(key);
35+
count += (freq * (freq -1) /2) *8;
36+
}
37+
}
38+
returncount;
39+
}
2040

2141
}
2242
}

‎src/test/java/com/fishercoder/_1726Test.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88

99
publicclass_1726Test {
1010
privatestatic_1726.Solution1solution1;
11+
privatestatic_1726.Solution2solution2;
1112

1213
@BeforeClass
1314
publicstaticvoidsetup() {
1415
solution1 =new_1726.Solution1();
16+
solution2 =new_1726.Solution2();
1517
}
1618

1719
@Test
@@ -39,4 +41,8 @@ public void test5() {
3941
assertEquals(128,solution1.tupleSameProduct(newint[]{1,2,3,4,6,8,12,24}));
4042
}
4143

44+
@Test
45+
publicvoidtest6() {
46+
assertEquals(40,solution2.tupleSameProduct(newint[]{2,3,4,6,8,12}));
47+
}
4248
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp