Skip to content

Commit ad335e2

Browse files
committed
feat: add solutions to lc problem: No.0832
No.0832.Flipping an Image
1 parent fdd3730 commit ad335e2

File tree

7 files changed

+228
-144
lines changed

7 files changed

+228
-144
lines changed

solution/0800-0899/0832.Flipping an Image/README.md

+88-40
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,15 @@
5757

5858
<!-- 这里可写通用的实现逻辑 -->
5959

60-
遍历矩阵每一行,利用双指针 p, q 进行水平交换翻转,顺便反转图像(1 变 0,0 变 1:`1 ^ 1` = 0,`0 ^ 1` = 1)。
60+
**方法一:双指针**
61+
62+
我们可以遍历矩阵,对于遍历到的每一行 $row$:
63+
64+
我们使用双指针 $i$ 和 $j$ 分别指向该行的首尾元素,如果 $row[i] = row[j]$,交换后两者的值仍然保持不变,因此,我们只需要对 $row[i]$ 和 $row[j]$ 进行异或反转即可,然后将 $i$ 和 $j$ 分别向中间移动一位,直到 $i \geq j$。如果 $row[i] \neq row[j]$,此时交换后再反转两者的值,仍然保持不变,因此,可以不进行任何操作。
65+
66+
最后,如果 $i = j$,我们直接对 $row[i]$ 进行反转即可。
67+
68+
时间复杂度 $O(n^2)$,其中 $n$ 是矩阵的行数或列数。空间复杂度 $O(1)$。
6169

6270
<!-- tabs:start -->
6371

@@ -67,19 +75,18 @@
6775

6876
```python
6977
class Solution:
70-
def flipAndInvertImage(self, A: List[List[int]]) -> List[List[int]]:
71-
m, n = len(A), len(A[0])
72-
for i in range(m):
73-
p, q = 0, n - 1
74-
while p < q:
75-
t = A[i][p] ^ 1
76-
A[i][p] = A[i][q] ^ 1
77-
A[i][q] = t
78-
p += 1
79-
q -= 1
80-
if p == q:
81-
A[i][p] ^= 1
82-
return A
78+
def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:
79+
n = len(image)
80+
for row in image:
81+
i, j = 0, n - 1
82+
while i < j:
83+
if row[i] == row[j]:
84+
row[i] ^= 1
85+
row[j] ^= 1
86+
i, j = i + 1, j - 1
87+
if i == j:
88+
row[i] ^= 1
89+
return image
8390
```
8491

8592
### **Java**
@@ -88,22 +95,20 @@ class Solution:
8895

8996
```java
9097
class Solution {
91-
public int[][] flipAndInvertImage(int[][] A) {
92-
int m = A.length, n = A[0].length;
93-
for (int i = 0; i < m; ++i) {
94-
int p = 0, q = n - 1;
95-
while (p < q) {
96-
int t = A[i][p] ^ 1;
97-
A[i][p] = A[i][q] ^ 1;
98-
A[i][q] = t;
99-
++p;
100-
--q;
98+
public int[][] flipAndInvertImage(int[][] image) {
99+
for (var row : image) {
100+
int i = 0, j = row.length - 1;
101+
for (; i < j; ++i, --j) {
102+
if (row[i] == row[j]) {
103+
row[i] ^= 1;
104+
row[j] ^= 1;
105+
}
101106
}
102-
if (p == q) {
103-
A[i][p] ^= 1;
107+
if (i == j) {
108+
row[i] ^= 1;
104109
}
105110
}
106-
return A;
111+
return image;
107112
}
108113
}
109114
```
@@ -113,23 +118,66 @@ class Solution {
113118
```cpp
114119
class Solution {
115120
public:
116-
vector<vector<int>> flipAndInvertImage(vector<vector<int>>& A) {
117-
int m = A.size(), n = A[0].size();
118-
for (int i = 0; i < m; ++i) {
119-
int p = 0, q = n - 1;
120-
while (p < q) {
121-
int t = A[i][p] ^ 1;
122-
A[i][p] = A[i][q] ^ 1;
123-
A[i][q] = t;
124-
++p;
125-
--q;
121+
vector<vector<int>> flipAndInvertImage(vector<vector<int>>& image) {
122+
for (auto& row : image) {
123+
int i = 0, j = row.size() - 1;
124+
for (; i < j; ++i, --j) {
125+
if (row[i] == row[j]) {
126+
row[i] ^= 1;
127+
row[j] ^= 1;
128+
}
126129
}
127-
if (p == q) {
128-
A[i][p] ^= 1;
130+
if (i == j) {
131+
row[i] ^= 1;
132+
}
133+
}
134+
return image;
135+
}
136+
};
137+
```
138+
139+
### **Go**
140+
141+
```go
142+
func flipAndInvertImage(image [][]int) [][]int {
143+
for _, row := range image {
144+
i, j := 0, len(row)-1
145+
for ; i < j; i, j = i+1, j-1 {
146+
if row[i] == row[j] {
147+
row[i] ^= 1
148+
row[j] ^= 1
149+
}
150+
}
151+
if i == j {
152+
row[i] ^= 1
153+
}
154+
}
155+
return image
156+
}
157+
```
158+
159+
### **JavaScript**
160+
161+
```js
162+
/**
163+
* @param {number[][]} image
164+
* @return {number[][]}
165+
*/
166+
var flipAndInvertImage = function (image) {
167+
for (const row of image) {
168+
let i = 0;
169+
let j = row.length - 1;
170+
for (; i < j; ++i, --j) {
171+
if (row[i] == row[j]) {
172+
row[i] ^= 1;
173+
row[j] ^= 1;
129174
}
130175
}
131-
return A;
176+
if (i == j) {
177+
row[i] ^= 1;
178+
}
132179
}
180+
return image;
133181
};
134182
```
135183

solution/0800-0899/0832.Flipping an Image/README_EN.md

+79-39
Original file line numberDiff line numberDiff line change
@@ -55,41 +55,38 @@ Then invert the image: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
5555

5656
```python
5757
class Solution:
58-
def flipAndInvertImage(self, A: List[List[int]]) -> List[List[int]]:
59-
m, n = len(A), len(A[0])
60-
for i in range(m):
61-
p, q = 0, n - 1
62-
while p < q:
63-
t = A[i][p] ^ 1
64-
A[i][p] = A[i][q] ^ 1
65-
A[i][q] = t
66-
p += 1
67-
q -= 1
68-
if p == q:
69-
A[i][p] ^= 1
70-
return A
58+
def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:
59+
n = len(image)
60+
for row in image:
61+
i, j = 0, n - 1
62+
while i < j:
63+
if row[i] == row[j]:
64+
row[i] ^= 1
65+
row[j] ^= 1
66+
i, j = i + 1, j - 1
67+
if i == j:
68+
row[i] ^= 1
69+
return image
7170
```
7271

7372
### **Java**
7473

7574
```java
7675
class Solution {
77-
public int[][] flipAndInvertImage(int[][] A) {
78-
int m = A.length, n = A[0].length;
79-
for (int i = 0; i < m; ++i) {
80-
int p = 0, q = n - 1;
81-
while (p < q) {
82-
int t = A[i][p] ^ 1;
83-
A[i][p] = A[i][q] ^ 1;
84-
A[i][q] = t;
85-
++p;
86-
--q;
76+
public int[][] flipAndInvertImage(int[][] image) {
77+
for (var row : image) {
78+
int i = 0, j = row.length - 1;
79+
for (; i < j; ++i, --j) {
80+
if (row[i] == row[j]) {
81+
row[i] ^= 1;
82+
row[j] ^= 1;
83+
}
8784
}
88-
if (p == q) {
89-
A[i][p] ^= 1;
85+
if (i == j) {
86+
row[i] ^= 1;
9087
}
9188
}
92-
return A;
89+
return image;
9390
}
9491
}
9592
```
@@ -99,26 +96,69 @@ class Solution {
9996
```cpp
10097
class Solution {
10198
public:
102-
vector<vector<int>> flipAndInvertImage(vector<vector<int>>& A) {
103-
int m = A.size(), n = A[0].size();
104-
for (int i = 0; i < m; ++i) {
105-
int p = 0, q = n - 1;
106-
while (p < q) {
107-
int t = A[i][p] ^ 1;
108-
A[i][p] = A[i][q] ^ 1;
109-
A[i][q] = t;
110-
++p;
111-
--q;
99+
vector<vector<int>> flipAndInvertImage(vector<vector<int>>& image) {
100+
for (auto& row : image) {
101+
int i = 0, j = row.size() - 1;
102+
for (; i < j; ++i, --j) {
103+
if (row[i] == row[j]) {
104+
row[i] ^= 1;
105+
row[j] ^= 1;
106+
}
112107
}
113-
if (p == q) {
114-
A[i][p] ^= 1;
108+
if (i == j) {
109+
row[i] ^= 1;
115110
}
116111
}
117-
return A;
112+
return image;
118113
}
119114
};
120115
```
121116
117+
### **Go**
118+
119+
```go
120+
func flipAndInvertImage(image [][]int) [][]int {
121+
for _, row := range image {
122+
i, j := 0, len(row)-1
123+
for ; i < j; i, j = i+1, j-1 {
124+
if row[i] == row[j] {
125+
row[i] ^= 1
126+
row[j] ^= 1
127+
}
128+
}
129+
if i == j {
130+
row[i] ^= 1
131+
}
132+
}
133+
return image
134+
}
135+
```
136+
137+
### **JavaScript**
138+
139+
```js
140+
/**
141+
* @param {number[][]} image
142+
* @return {number[][]}
143+
*/
144+
var flipAndInvertImage = function (image) {
145+
for (const row of image) {
146+
let i = 0;
147+
let j = row.length - 1;
148+
for (; i < j; ++i, --j) {
149+
if (row[i] == row[j]) {
150+
row[i] ^= 1;
151+
row[j] ^= 1;
152+
}
153+
}
154+
if (i == j) {
155+
row[i] ^= 1;
156+
}
157+
}
158+
return image;
159+
};
160+
```
161+
122162
### **...**
123163

124164
```
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
class Solution {
22
public:
3-
vector<vector<int>> flipAndInvertImage(vector<vector<int>>& A) {
4-
int m = A.size(), n = A[0].size();
5-
for (int i = 0; i < m; ++i) {
6-
int p = 0, q = n - 1;
7-
while (p < q) {
8-
int t = A[i][p] ^ 1;
9-
A[i][p] = A[i][q] ^ 1;
10-
A[i][q] = t;
11-
++p;
12-
--q;
3+
vector<vector<int>> flipAndInvertImage(vector<vector<int>>& image) {
4+
for (auto& row : image) {
5+
int i = 0, j = row.size() - 1;
6+
for (; i < j; ++i, --j) {
7+
if (row[i] == row[j]) {
8+
row[i] ^= 1;
9+
row[j] ^= 1;
10+
}
1311
}
14-
if (p == q) {
15-
A[i][p] ^= 1;
12+
if (i == j) {
13+
row[i] ^= 1;
1614
}
1715
}
18-
return A;
16+
return image;
1917
}
2018
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func flipAndInvertImage(image [][]int) [][]int {
2+
for _, row := range image {
3+
i, j := 0, len(row)-1
4+
for ; i < j; i, j = i+1, j-1 {
5+
if row[i] == row[j] {
6+
row[i] ^= 1
7+
row[j] ^= 1
8+
}
9+
}
10+
if i == j {
11+
row[i] ^= 1
12+
}
13+
}
14+
return image
15+
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
class Solution {
2-
public int[][] flipAndInvertImage(int[][] A) {
3-
int m = A.length, n = A[0].length;
4-
for (int i = 0; i < m; ++i) {
5-
int p = 0, q = n - 1;
6-
while (p < q) {
7-
int t = A[i][p] ^ 1;
8-
A[i][p] = A[i][q] ^ 1;
9-
A[i][q] = t;
10-
++p;
11-
--q;
2+
public int[][] flipAndInvertImage(int[][] image) {
3+
for (var row : image) {
4+
int i = 0, j = row.length - 1;
5+
for (; i < j; ++i, --j) {
6+
if (row[i] == row[j]) {
7+
row[i] ^= 1;
8+
row[j] ^= 1;
9+
}
1210
}
13-
if (p == q) {
14-
A[i][p] ^= 1;
11+
if (i == j) {
12+
row[i] ^= 1;
1513
}
1614
}
17-
return A;
15+
return image;
1816
}
1917
}

0 commit comments

Comments
 (0)