Skip to content

Commit b9f9270

Browse files
committed
flipAndInvertImage
1 parent 0ce2755 commit b9f9270

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
| 0814 | [pruneTree](./code/0814_pruneTree) || Tree | |
7979
| 0821 | [shortestToChar](./code/0821_shortestToChar) || Greedy | |
8080
| 0830 | [largeGroupPositions](./code/0830_largeGroupPositions) || Array | |
81+
| 0832 | [flipAndInvertImage](./code/0832_flipAndInvertImage) || Array | |
8182
| 0860 | [lemonadeChange](./code/0860_lemonadeChange) || Greedy | |
8283
| 0888 | [fairCandySwap](./code/0888_fairCandySwap) || Array | 1️⃣✅ |
8384
| 0922 | [sortArrayByParityII](./code/0922_sortArrayByParityII) || Array, Sort | |

Diff for: code/0030_findSubstring/findSubstringV2.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {string} s
3+
* @param {string[]} words
4+
* @return {number[]}
5+
*/
6+
var findSubstring = function (s, words) {
7+
const wordSize = words[0].length;
8+
const wordsLen = wordSize * words.length;
9+
let map = new Map();
10+
let ans = [];
11+
for (let i = 0; i < words.length; i++) {
12+
map.has(words[i]) ? map.set(words[i], map.get(words[i]) + 1) : map.set(words[i], 1);
13+
}
14+
for (let i = 0; i < s.length - wordsLen + 1; i++) {
15+
const tmap = new Map(map);
16+
let count = words.length;
17+
for (let p = i; p < i + wordsLen; p += wordSize) {
18+
const word = s.slice(p, p + wordSize);
19+
if (!tmap.has(word) || tmap.get(word) <= 0) {
20+
break;
21+
}
22+
tmap.set(word, tmap.get(word) - 1);
23+
count--;
24+
}
25+
if (count === 0) {
26+
ans.push(i);
27+
}
28+
}
29+
return ans;
30+
};

Diff for: code/0832_flipAndInvertImage/flipAndInvertImage.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number[][]} A
3+
* @return {number[][]}
4+
*/
5+
var flipAndInvertImage = function(A) {
6+
for (let i = 0; i < A.length; i++) {
7+
let m = 0, n = A.length - 1;
8+
while (m < n) {
9+
[A[i][m], A[i][n]] = [A[i][n], A[i][m]];
10+
m++;
11+
n--;
12+
}
13+
for (let j = 0; j < A[i].length; j++) {
14+
A[i][j] === 1 ? A[i][j] = 0 : A[i][j] = 1;
15+
}
16+
}
17+
return A;
18+
};
19+
20+
// 时间复杂度 O(N²)
21+
// 空间复杂度 O(1)

0 commit comments

Comments
 (0)