-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlippingAnImage.java
63 lines (50 loc) · 1.21 KB
/
FlippingAnImage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.leetcode.algorithms.easy.array;
import java.util.Scanner;
public class FlippingAnImage {
public static void main(String...strings) {
// Input
Scanner scanner = new Scanner(System.in);
int columns = scanner.nextInt();
int rows = scanner.nextInt();
int[][] A = new int[rows][columns];
for(int x = 0; x < rows; x++) {
for(int y = 0; y < columns; y++) {
A[x][y] = scanner.nextInt();
}
}
flipAndInvertImage(A);
scanner.close();
}
public static int[][] flipAndInvertImage(int[][] A) {
for(int[] B : A) {
int max = B.length - 1;
int min = 0;
while(max >= min) {
// Reverse
if(max > min) {
int temp = B[max];
B[max] = B[min];
B[min] = temp;
}
// Invert
if(B[max] == 1) {
B[max] = 0;
} else if(B[max] == 0) {
B[max] = 1;
}
if(max > min) {
if(B[min] == 1) {
B[min] = 0;
} else if(B[min] == 0) {
B[min] = 1;
}
}
// Update min & max
--max;
++min;
}
//System.out.println(Arrays.toString(B));
}
return A;
}
}