-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0832-flipping-an-image.js
More file actions
33 lines (27 loc) · 999 Bytes
/
0832-flipping-an-image.js
File metadata and controls
33 lines (27 loc) · 999 Bytes
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
/**
* Flipping An Image
* Time Complexity: O(N^2)
* Space Complexity: O(1)
*/
var flipAndInvertImage = function (image) {
const matrixRows = image.length;
for (let rowIterator = 0; rowIterator < matrixRows; rowIterator++) {
let currentRowReference = image[rowIterator];
let rowCellCount = currentRowReference.length;
let leftBoundPointer = 0;
let rightBoundPointer = rowCellCount - 1;
while (leftBoundPointer <= rightBoundPointer) {
let leftCellOriginalValue = currentRowReference[leftBoundPointer];
let rightCellOriginalValue = currentRowReference[rightBoundPointer];
if (leftBoundPointer === rightBoundPointer) {
currentRowReference[leftBoundPointer] = 1 - leftCellOriginalValue;
} else {
currentRowReference[leftBoundPointer] = 1 - rightCellOriginalValue;
currentRowReference[rightBoundPointer] = 1 - leftCellOriginalValue;
}
leftBoundPointer++;
rightBoundPointer--;
}
}
return image;
};