Skip to content

Commit c0a8003

Browse files
add 832
1 parent 1bf43e4 commit c0a8003

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2424
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
25+
|832|[Flipping an Image](https://leetcode.com/problems/flipping-an-image/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_832.java) | O(n) | O(1) | |Easy|
2526
|830|[Positions of Large Groups](https://leetcode.com/problems/positions-of-large-groups/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_830.java) | O(n) | O(n) | |Easy|
2627
|824|[Goat Latin](https://leetcode.com/problems/goat-latin/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_824.java) | O(n) | O(1) | |Easy|
2728
|821|[Shortest Distance to a Character](https://leetcode.com/problems/shortest-distance-to-a-character/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_821.java) | O(n) | O(k) (k is the number of char C in S) | |Easy|
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 832. Flipping an Image
5+
*
6+
* Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image.
7+
* To flip an image horizontally means that each row of the image is reversed. For example, flipping [1, 1, 0] horizontally results in [0, 1, 1].
8+
* To invert an image means that each 0 is replaced by 1, and each 1 is replaced by 0. For example, inverting [0, 1, 1] results in [1, 0, 0].
9+
*
10+
* Example 1:
11+
* Input: [[1,1,0],[1,0,1],[0,0,0]]
12+
* Output: [[1,0,0],[0,1,0],[1,1,1]]
13+
* Explanation: First reverse each row: [[0,1,1],[1,0,1],[0,0,0]].
14+
* Then, invert the image: [[1,0,0],[0,1,0],[1,1,1]]
15+
*
16+
* Example 2:
17+
* Input: [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]
18+
* Output: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
19+
* Explanation: First reverse each row: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]].
20+
* Then invert the image: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
21+
*
22+
* Notes:
23+
* 1 <= A.length = A[0].length <= 20
24+
* 0 <= A[i][j] <= 1
25+
*/
26+
public class _832 {
27+
public static class Solution1 {
28+
public int[][] flipAndInvertImage(int[][] A) {
29+
int m = A.length;
30+
int n = A[0].length;
31+
int[][] result = new int[m][n];
32+
for (int i = 0; i < m; i++) {
33+
int[] flipped = (reverse(A[i]));
34+
result[i] = invert(flipped);
35+
}
36+
return result;
37+
}
38+
39+
private int[] invert(int[] flipped) {
40+
int[] result = new int[flipped.length];
41+
for (int i = 0; i < flipped.length; i++) {
42+
if (flipped[i] == 0) {
43+
result[i] = 1;
44+
} else {
45+
result[i] = 0;
46+
}
47+
}
48+
return result;
49+
}
50+
51+
private int[] reverse(int[] nums) {
52+
for (int i = 0, j = nums.length - 1; i < j; i++, j--) {
53+
int tmp = nums[i];
54+
nums[i] = nums[j];
55+
nums[j] = tmp;
56+
}
57+
return nums;
58+
}
59+
}
60+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._832;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _832Test {
10+
private static _832.Solution1 solution1;
11+
private static int[][] expected;
12+
private static int[][] A;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _832.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
A = new int[][] {
22+
{1, 1, 0},
23+
{1, 0, 1},
24+
{0, 0, 0}
25+
};
26+
expected = new int[][] {
27+
{1, 0, 0},
28+
{0, 1, 0},
29+
{1, 1, 1}
30+
};
31+
assertArrayEquals(expected, solution1.flipAndInvertImage(A));
32+
}
33+
34+
@Test
35+
public void test2() {
36+
A = new int[][] {
37+
{1, 1, 0, 0},
38+
{1, 0, 0, 1},
39+
{0, 1, 1, 1},
40+
{1, 0, 1, 0}
41+
};
42+
expected = new int[][] {
43+
{1, 1, 0, 0},
44+
{0, 1, 1, 0},
45+
{0, 0, 0, 1},
46+
{1, 0, 1, 0}
47+
};
48+
assertArrayEquals(expected, solution1.flipAndInvertImage(A));
49+
}
50+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy