Skip to content

Commit ecc0f48

Browse files
add 1814
1 parent f572910 commit ecc0f48

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ _If you like this project, please leave me a star._ ★
1010
|-----|----------------|---------------|--------|-------------|-------------
1111
|1817|[Finding the Users Active Minutes](https://leetcode.com/problems/finding-the-users-active-minutes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1817.java) ||Medium|HashTable|
1212
|1816|[Truncate Sentence](https://leetcode.com/problems/truncate-sentence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1816.java) ||Easy|String|
13+
|1814|[Count Nice Pairs in an Array](https://leetcode.com/problems/count-nice-pairs-in-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1814.java) ||Medium|Array, HashTable|
1314
|1813|[Sentence Similarity III](https://leetcode.com/problems/sentence-similarity-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1813.java) ||Medium|String|
1415
|1812|[Determine Color of a Chessboard Square](https://leetcode.com/problems/determine-color-of-a-chessboard-square/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1812.java) ||Easy|String|
1516
|1807|[Evaluate the Bracket Pairs of a String](https://leetcode.com/problems/evaluate-the-bracket-pairs-of-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1807.java) ||Medium|HashTable, String|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class _1814 {
7+
public static class Solution1 {
8+
public int countNicePairs(int[] nums) {
9+
long[] reverses = getRev(nums);
10+
Map<Long, Integer> map = new HashMap<>();
11+
long nicePairs = 0;
12+
for (int i = 0; i < nums.length; i++) {
13+
long diff = nums[i] - reverses[i];
14+
map.put(diff, map.getOrDefault(diff, 0) + 1);
15+
nicePairs = (nicePairs + map.get(nums[i] - reverses[i]) - 1) % 1000000007;
16+
}
17+
return (int) nicePairs;
18+
}
19+
20+
private long[] getRev(int[] nums) {
21+
long[] reverses = new long[nums.length];
22+
for (int i = 0; i < nums.length; i++) {
23+
long reverse = 0;
24+
int num = nums[i];
25+
while (num != 0) {
26+
reverse = reverse * 10 + num % 10;
27+
num /= 10;
28+
}
29+
reverses[i] = reverse;
30+
}
31+
return reverses;
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1814;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1814Test {
10+
private static _1814.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1814.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(2, solution1.countNicePairs(new int[]{42, 11, 1, 97}));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(4, solution1.countNicePairs(new int[]{13, 10, 35, 24, 76}));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(2, solution1.countNicePairs(new int[]{352171103, 442454244, 42644624, 152727101, 413370302, 293999243}));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(678
35+
, solution1.countNicePairs(new int[]{8047408, 192867140, 497837845, 279787822, 151999002, 168514912, 193424242, 399636844, 132424231, 476736524, 267958611, 493350382, 476382727, 232939232, 197000791, 295291645, 126313621, 374645524, 7956597, 1376731, 496463745, 234481430, 359130803, 287625836, 250572050, 42311324, 477434624, 493231448, 493231244, 150494051, 184645534, 365252413, 495764582, 335976531, 384564332, 377151623, 198736741, 335161533, 245552540, 194897341, 83911938, 220562020, 496645745, 287151782, 374635526, 372483324, 485101584, 271797172, 244949442, 254333303, 251635002, 459181805, 472392123, 241350140, 256121502, 336895621, 354635302, 358909704, 194525491, 3606063, 194150341, 63477436, 341936141, 60299206, 69811896, 369928813, 229926920, 435310522, 299542980, 463777364, 164534512, 305885501, 437181734, 74288247, 487281835, 171161022, 423966312, 496989544, 452633252, 252433101, 141565141, 315895501, 478897927, 232532230, 472451262, 160504114, 476666674, 6179716, 251483002, 474777474, 443532332, 475808424, 457514604, 400936002, 384878483, 172616122, 283292232, 165645615, 392000144, 378636873}));
36+
}
37+
38+
}

0 commit comments

Comments
 (0)