@@ -55,41 +55,38 @@ Then invert the image: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
55
55
56
56
``` python
57
57
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
71
70
```
72
71
73
72
### ** Java**
74
73
75
74
``` java
76
75
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
+ }
87
84
}
88
- if (p == q ) {
89
- A [i][p ] ^ = 1 ;
85
+ if (i == j ) {
86
+ row[i ] ^ = 1 ;
90
87
}
91
88
}
92
- return A ;
89
+ return image ;
93
90
}
94
91
}
95
92
```
@@ -99,26 +96,69 @@ class Solution {
99
96
``` cpp
100
97
class Solution {
101
98
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
+ }
112
107
}
113
- if (p == q ) {
114
- A [ i ] [ p ] ^= 1;
108
+ if (i == j ) {
109
+ row [ i ] ^= 1;
115
110
}
116
111
}
117
- return A ;
112
+ return image ;
118
113
}
119
114
};
120
115
```
121
116
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
+
122
162
### ** ...**
123
163
124
164
```
0 commit comments