Skip to content

Commit 52f1d37

Browse files
committed
832_Flipping_an_Image
1 parent f96c512 commit 52f1d37

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ Also, there are open source implementations for basic data structs and algorithm
181181
| 804 | [Unique Morse Code Words](https://leetcode.com/problems/unique-morse-code-words/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/804_Unique_Morse_Code_Words.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/804_Unique_Morse_Code_Words.java) | String, Hash and Set. Set is recommended. |
182182
| 811 | [Subdomain Visit Count](https://leetcode.com/problems/subdomain-visit-count/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/811_Subdomain_Visit_Count.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/811_Subdomain_Visit_Count.java) | String split and HashMap, O(n) and O(n) |
183183
| 819 | [Most Common Word](https://leetcode.com/problems/most-common-word/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/819_Most_Common_Word.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/819_Most_Common_Word.java) | String processing, be careful about 'b,b,b'. regex is recommended. |
184+
| 832 | [Flipping an Image](https://leetcode.com/problems/flipping-an-image/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/832_Flipping_an_Image.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/832_Flipping_an_Image.java) | Invert and swap can be done at the same time, and careful about (n + 1)/2, O(n^2) and O(1) |
184185
| 844 | [Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/844_Backspace_String_Compare.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/844_Backspace_String_Compare.java) | 1. Stack pop when encounters #, O(n) and O(n)<br>2. Compare string from end to start, O(n) and O(1) |
185186
| 852 | [Peak Index in a Mountain Array](https://leetcode.com/problems/peak-index-in-a-mountain-array/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/852_Peak_Index_in_a_Mountain_Array.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/852_Peak_Index_in_a_Mountain_Array.java) | 1. Scan the array until encountering decline, O(n) and O(1)<br>2. Binary seach with additional check for [i + 1], O(logn) and O(1) |
186187
| 867 | [Transpose Matrix](https://leetcode.com/problems/transpose-matrix/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/867_Transpose_Matrix.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/867_Transpose_Matrix.java) | Res[i][j] = A[j][i] |

java/832_Flipping_an_Image.java

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public int[][] flipAndInvertImage(int[][] A) {
3+
int C = A[0].length;
4+
for (int[] row: A)
5+
for (int i = 0; i < (C + 1) / 2; ++i) {
6+
int tmp = row[i] ^ 1;
7+
row[i] = row[C - 1 - i] ^ 1;
8+
row[C - 1 - i] = tmp;
9+
}
10+
11+
return A;
12+
}
13+
}

python/832_Flipping_an_Image.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution(object):
2+
def flipAndInvertImage(self, A):
3+
for row in A:
4+
for i in xrange((len(row) + 1) / 2):
5+
"""
6+
In Python, the shortcut row[~i] = row[-i-1] = row[len(row) - 1 - i]
7+
helps us find the i-th value of the row, counting from the right.
8+
"""
9+
row[i], row[~i] = row[~i] ^ 1, row[i] ^ 1
10+
return A
11+
# return [[1 ^ i for i in row[::-1]] for row in A]

0 commit comments

Comments
 (0)