Skip to content

Commit 7b8946b

Browse files
committed
leetcode: CanMakeArithmeticProgression: passed
leetcode: NumberClosedIslands: passed leetcode: OneSwapEqual: passed leetcode SignOfProduct: passed
1 parent f1a4c3c commit 7b8946b

File tree

8 files changed

+263
-0
lines changed

8 files changed

+263
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.leetcode.array;
2+
3+
import java.util.Arrays;
4+
5+
public final class CanMakeArithmeticProgression {
6+
private CanMakeArithmeticProgression() {
7+
8+
}
9+
10+
public static boolean canMakeArithmeticProgression(int[] arr) {
11+
Arrays.sort(arr);
12+
int d = arr[1] - arr[0];
13+
for (int i = 2; i < arr.length; i++) {
14+
if (arr[i] - arr[i - 1] != d) {
15+
return false;
16+
}
17+
}
18+
return true;
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.leetcode.array;
2+
3+
public final class SignOfProduct {
4+
private SignOfProduct() {
5+
}
6+
7+
public static int arraySign(int[] nums) {
8+
int sign = 1;
9+
for (int n : nums) {
10+
if (n == 0) {
11+
return 0;
12+
} else {
13+
if (n < 0) {
14+
sign *= -1;
15+
}
16+
}
17+
}
18+
return sign;
19+
}
20+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.leetcode.graph;
2+
3+
public final class NumberClosedIslands {
4+
private NumberClosedIslands() {
5+
}
6+
7+
public static int closedIsland(int[][] grid) {
8+
return new Solver(grid).solve();
9+
}
10+
11+
12+
private static final class Solver {
13+
private final int[][] grid;
14+
private final boolean[][] explored;
15+
private final boolean[][] processing;
16+
17+
private Solver(int[][] grid) {
18+
this.grid = grid;
19+
this.explored = new boolean[grid.length][grid[0].length];
20+
this.processing = new boolean[grid.length][grid[0].length];
21+
}
22+
23+
public int solve() {
24+
int count = 0;
25+
for (int i = 0; i < grid.length; i++) {
26+
for (int j = 0; j < grid[i].length; j++) {
27+
if (!explored[i][j] && isClosed(i, j)) {
28+
count++;
29+
}
30+
}
31+
}
32+
return count;
33+
}
34+
35+
private boolean isEdge(int i, int j) {
36+
return !(i > 0 && j > 0 && i < grid.length - 1 && j < grid[0].length - 1);
37+
}
38+
39+
private boolean isClosed(int i, int j) {
40+
explored[i][j] = true;
41+
if (isLand(i, j)) {
42+
processing[i][j] = true;
43+
boolean res = !isEdge(i, j)
44+
&& isValidAdjacent(i - 1, j)
45+
&& isValidAdjacent(i + 1, j)
46+
&& isValidAdjacent(i, j + 1)
47+
&& isValidAdjacent(i, j - 1);
48+
processing[i][j] = false;
49+
return res;
50+
}
51+
return false;
52+
}
53+
54+
private boolean isValidAdjacent(int i, int j) {
55+
if (processing[i][j]) {
56+
return true;
57+
}
58+
return !isLand(i, j) || isClosed(i, j);
59+
}
60+
61+
private boolean isLand(int i, int j) {
62+
return grid[i][j] == 0;
63+
}
64+
}
65+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.leetcode.string;
2+
3+
public final class OneSwapEqual {
4+
private OneSwapEqual() {
5+
}
6+
7+
@SuppressWarnings("squid:S3776")
8+
public static boolean areAlmostEqual(String s1, String s2) {
9+
int i = 0;
10+
int j = s1.length() - 1;
11+
int swappedElement = 0;
12+
while (i <= j) {
13+
char leftS1 = s1.charAt(i);
14+
char leftS2 = s2.charAt(i);
15+
char rightS1 = s1.charAt(j);
16+
char rightS2 = s2.charAt(j);
17+
if (leftS1 != leftS2 && rightS1 != rightS2) {
18+
if (leftS1 == rightS2 && rightS1 == leftS2) {
19+
i++;
20+
j--;
21+
swappedElement++;
22+
} else {
23+
return false;
24+
}
25+
}
26+
if (leftS1 == leftS2) {
27+
i++;
28+
}
29+
if (rightS1 == rightS2) {
30+
j--;
31+
}
32+
if (swappedElement > 1) {
33+
return false;
34+
}
35+
}
36+
if (s1.length() == 1) {
37+
return true;
38+
}
39+
return swappedElement != 0 || s1.charAt(i) == s2.charAt(i);
40+
}
41+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.leetcode.array;
2+
3+
import org.junit.jupiter.params.ParameterizedTest;
4+
import org.junit.jupiter.params.converter.ConvertWith;
5+
import org.junit.jupiter.params.provider.CsvSource;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
class CanMakeArithmeticProgressionTest {
10+
11+
@ParameterizedTest
12+
@CsvSource( {
13+
"'3,5,1', true",
14+
"'1,2,4', false"
15+
}
16+
)
17+
void testCanMakeArithmeticProgression(@ConvertWith(IntArrayConverter.class) int[] nums, boolean expected) {
18+
assertEquals(expected, CanMakeArithmeticProgression.canMakeArithmeticProgression(nums));
19+
}
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.leetcode.array;
2+
3+
import org.junit.jupiter.params.ParameterizedTest;
4+
import org.junit.jupiter.params.converter.ConvertWith;
5+
import org.junit.jupiter.params.provider.CsvSource;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
class SignOfProductTest {
10+
@ParameterizedTest
11+
@CsvSource( {
12+
"'-1,-2,-3,-4,3,2,1', 1",
13+
"'1,5,0,2,-3', 0",
14+
"'-1,1,-1,1,-1', -1"
15+
})
16+
void testArraySign(@ConvertWith(IntArrayConverter.class) int[] nums, int sign) {
17+
assertEquals(sign, SignOfProduct.arraySign(nums));
18+
}
19+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.leetcode.graph;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.params.ParameterizedTest;
5+
import org.junit.jupiter.params.provider.Arguments;
6+
import org.junit.jupiter.params.provider.MethodSource;
7+
8+
import java.util.stream.Stream;
9+
10+
class NumberClosedIslandsTest {
11+
12+
static Stream<Arguments> testCases() {
13+
return Stream.of(
14+
Arguments.of(new int[][] {
15+
{1, 1, 1, 1, 1, 1, 1, 0},
16+
{1, 0, 0, 0, 0, 1, 1, 0},
17+
{1, 0, 1, 0, 1, 1, 1, 0},
18+
{1, 0, 0, 0, 0, 1, 0, 1},
19+
{1, 1, 1, 1, 1, 1, 1, 0}
20+
}, 2),
21+
Arguments.of(new int[][] {
22+
{0, 0, 1, 0, 0},
23+
{0, 1, 0, 1, 0},
24+
{0, 1, 1, 1, 0}
25+
}, 1),
26+
Arguments.of(new int[][] {
27+
{1, 1, 1, 1, 1, 1, 1},
28+
{1, 0, 0, 0, 0, 0, 1},
29+
{1, 0, 1, 1, 1, 0, 1},
30+
{1, 0, 1, 0, 1, 0, 1},
31+
{1, 0, 1, 1, 1, 0, 1},
32+
{1, 0, 0, 0, 0, 0, 1},
33+
{1, 1, 1, 1, 1, 1, 1}
34+
}, 2),
35+
Arguments.of(new int[][] {
36+
{0, 0, 1, 1, 0, 1, 0, 0, 1, 0},
37+
{1, 1, 0, 1, 1, 0, 1, 1, 1, 0},
38+
{1, 0, 1, 1, 1, 0, 0, 1, 1, 0},
39+
{0, 1, 1, 0, 0, 0, 0, 1, 0, 1},
40+
{0, 0, 0, 0, 0, 0, 1, 1, 1, 0},
41+
{0, 1, 0, 1, 0, 1, 0, 1, 1, 1},
42+
{1, 0, 1, 0, 1, 1, 0, 0, 0, 1},
43+
{1, 1, 1, 1, 1, 1, 0, 0, 0, 0},
44+
{1, 1, 1, 0, 0, 1, 0, 1, 0, 1},
45+
{1, 1, 1, 0, 1, 1, 0, 1, 1, 0}},
46+
5)
47+
);
48+
}
49+
50+
@ParameterizedTest
51+
@MethodSource("testCases")
52+
void testClosedIsland(int[][] grid, int expected) {
53+
Assertions.assertEquals(expected, NumberClosedIslands.closedIsland(grid));
54+
}
55+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.leetcode.string;
2+
3+
import org.junit.jupiter.params.ParameterizedTest;
4+
import org.junit.jupiter.params.provider.CsvSource;
5+
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
8+
class OneSwapEqualTest {
9+
10+
@ParameterizedTest
11+
@CsvSource( {
12+
"bank,kanb,true",
13+
"attack,defend,false",
14+
"kelb,kelb,true",
15+
"kelb,kekb,false",
16+
"bank,kanr,false",
17+
"siyolsdcjthwsiplccpbuceoxmjjgrauocx,siyolsdcjthwsiplccjbuceoxmpjgrauocx,true",
18+
"glb,bdg,false"
19+
})
20+
void testAreAlmostEqual(String s1, String s2, boolean expected) {
21+
assertEquals(expected, OneSwapEqual.areAlmostEqual(s1, s2));
22+
}
23+
}

0 commit comments

Comments
 (0)