Skip to content

Commit 5a61525

Browse files
committed
leetcode 832
1 parent 041cba8 commit 5a61525

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- 26.Remove Duplicates from Sorted Array `easy` `array` [link](https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/) [code](./leetcode_26.js)
1212
- 27.Remove Element `easy` `array` [link](https://leetcode.com/problems/remove-element/description/) [code](./leetcode_27.js)
1313
- 203.Remove Linked List Elements `easy` `linked list` [link](https://leetcode.com/problems/remove-linked-list-elements/description/) [code](./leetcode_203.js)
14+
- 832.Flipping an Image `easy` `array` [link](https://leetcode.com/problems/flipping-an-image/description/) [code](./leetcode_832.js)
1415

1516
## lintcode
1617

leetcode_832.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image.
3+
4+
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].
5+
6+
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].
7+
8+
Example 1:
9+
10+
Input: [[1,1,0],[1,0,1],[0,0,0]]
11+
Output: [[1,0,0],[0,1,0],[1,1,1]]
12+
Explanation: First reverse each row: [[0,1,1],[1,0,1],[0,0,0]].
13+
Then, invert the image: [[1,0,0],[0,1,0],[1,1,1]]
14+
15+
16+
Example 2:
17+
18+
Input: [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]
19+
Output: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
20+
Explanation: First reverse each row: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]].
21+
Then invert the image: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
22+
23+
24+
Notes:
25+
26+
1 <= A.length = A[0].length <= 20
27+
0 <= A[i][j] <= 1
28+
*/
29+
30+
31+
/**
32+
* @param {number[][]} A
33+
* @return {number[][]}
34+
*/
35+
var flipAndInvertImage = function (A) {
36+
let temp = 0;
37+
return A.map(a => {
38+
const len = a.length;
39+
for (let i = 0; i < len / 2; ++i) {
40+
temp = a[i];
41+
a[i] = ~a[len - 1 - i] + 2;
42+
a[len - 1 - i] = ~temp + 2;
43+
}
44+
return a;
45+
});
46+
};
47+
48+
console.log(flipAndInvertImage([[1, 1, 0], [1, 0, 1], [0, 0, 0]]));
49+
console.log(flipAndInvertImage([[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]));

0 commit comments

Comments
 (0)