Skip to content

Commit 2ab827c

Browse files
committed
832 833 835
1 parent c0b6fcd commit 2ab827c

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

832/main.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number[][]} A
3+
* @return {number[][]}
4+
*/
5+
var flipAndInvertImage = function (A) {
6+
for (const row of A) {
7+
let i = 0
8+
let j = row.length - 1
9+
while (i <= j) {
10+
[row[i], row[j]] = [row[j], row[i]]
11+
i++
12+
j--
13+
}
14+
for (let i = 0; i < row.length; i++) {
15+
if (row[i] === 0) {
16+
row[i] = 1
17+
} else {
18+
row[i] = 0
19+
}
20+
}
21+
}
22+
return A
23+
}

833/main.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @param {string} S
3+
* @param {number[]} indexes
4+
* @param {string[]} sources
5+
* @param {string[]} targets
6+
* @return {string}
7+
*/
8+
var findReplaceString = function (S, indexes, sources, targets) {
9+
const maps = {}
10+
for (let i = 0; i < indexes.length; i++) {
11+
const idx = indexes[i]
12+
const source = sources[i]
13+
const target = targets[i]
14+
if (S.indexOf(source, idx) === idx) {
15+
maps[idx] = {
16+
source,
17+
target
18+
}
19+
}
20+
}
21+
let ans = ''
22+
let i = 0
23+
while (i < S.length) {
24+
if (maps[i] != null) {
25+
ans += maps[i].target
26+
i += maps[i].source.length
27+
} else {
28+
ans += S[i]
29+
i++
30+
}
31+
}
32+
return ans
33+
}

835/main.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @param {number[][]} A
3+
* @param {number[][]} B
4+
* @return {number}
5+
*/
6+
var largestOverlap = function (A, B) {
7+
function cnt (A, B) {
8+
let ans = 0
9+
for (let i = 0; i < rows; i++) {
10+
for (let j = 0; j < cols; j++) {
11+
if (A[i][j] === B[i][j] && A[i][j] === 1) {
12+
ans += 1
13+
}
14+
}
15+
}
16+
return ans
17+
}
18+
19+
function move (A, x, y) {
20+
const next = Array.from(A, (_, i) => A[i].slice().fill(0))
21+
for (let i = 0; i < rows; i++) {
22+
for (let j = 0; j < cols; j++) {
23+
const nx = x + i
24+
const ny = y + j
25+
if (nx >= rows || ny >= cols) {
26+
continue
27+
}
28+
next[nx][ny] = A[i][j]
29+
}
30+
}
31+
return next
32+
}
33+
34+
const rows = A.length
35+
const cols = A[0].length
36+
let ans = 0
37+
for (let i = 0; i < rows; i++) {
38+
for (let j = 0; j < cols; j++) {
39+
ans = Math.max(ans, cnt(move(A, i, j), B))
40+
ans = Math.max(ans, cnt(move(B, i, j), A))
41+
}
42+
}
43+
return ans
44+
}

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,9 @@
247247
- [809 Expressive Words](./809/main.js)
248248
- [811 Subdomain Visit Count](./811/main.go)
249249
- [817 Linked List Components](./817/main.js)
250+
- [832 Flipping an Image](./832/main.js)
251+
- [833 Find And Replace in String](./833/main.js)
252+
- [835 Image Overlap](./835/main.js)
250253

251254
*Q: What is `LZS`? It almost appears inside all files (like js, cpp)?*
252255
A: Briefly speaking, that is an environment variable defined in my System. For unix/linux, you can add a command `export LZS='true'` in `~/.bashrc` such that `process.env.LZS` evaluates to `true` rather than `undefined.`

0 commit comments

Comments
 (0)