Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fcf983c

Browse files
0832 flipping-an-image
1 parent aba201b commit fcf983c

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Reverse Words in a String III
2+
3+
Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
4+
5+
## Example 1
6+
7+
Input: "Let's take LeetCode contest"
8+
9+
Output: "s'teL ekat edoCteeL tsetnoc"
10+
11+
## Note
12+
13+
In the string, each word is separated by single space and there will not be any extra space in the string.
14+
15+
## More Info
16+
17+
<https://leetcode.com/problems/reverse-words-in-a-string-iii/>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number[][]} A
3+
* @return {number[][]}
4+
*/
5+
var flipAndInvertImage = function (A) {
6+
for (let i = 0; i < A.length; i++) {
7+
if (Array.isArray(A[i])) {
8+
A[i] = A[i].reverse();
9+
for (let j = 0; j < A[i].length; j++) {
10+
A[i][j] = 1 - A[i][j];
11+
}
12+
}
13+
}
14+
return A;
15+
// for (let i = 0; i < A.length; i++) {
16+
// A[i] = A[i].map(v => 1-v).reverse()
17+
// }
18+
// return A
19+
20+
//Single line solution
21+
// return A.map(i => i.reverse().map(j => 1 - j))
22+
};
23+
24+
console.log(flipAndInvertImage([[1, 1, 0], [1, 0, 1], [0, 0, 0]]))
25+
console.log(flipAndInvertImage([[1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]]));
26+
console.log(flipAndInvertImage([12]))
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Flipping an Image
2+
3+
Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image.
4+
5+
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].
6+
7+
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].
8+
9+
## Example 1
10+
11+
Input: [[1,1,0],[1,0,1],[0,0,0]]
12+
13+
Output: [[1,0,0],[0,1,0],[1,1,1]]
14+
15+
Explanation: First reverse each row: [[0,1,1],[1,0,1],[0,0,0]].
16+
Then, invert the image: [[1,0,0],[0,1,0],[1,1,1]]
17+
18+
## Example 2
19+
20+
Input: [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]
21+
22+
Output: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
23+
24+
Explanation: First reverse each row: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]].
25+
Then invert the image: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
26+
27+
## Notes
28+
29+
1 <= A.length = A[0].length <= 20
30+
31+
0 <= A[i][j] <= 1
32+
33+
## More Info
34+
35+
<https://leetcode.com/problems/flipping-an-image/>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /