-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2482.行和列中一和零的差值.js
More file actions
48 lines (47 loc) · 1.02 KB
/
2482.行和列中一和零的差值.js
File metadata and controls
48 lines (47 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* @lc app=leetcode.cn id=2482 lang=javascript
*
* [2482] 行和列中一和零的差值
*/
// @lc code=start
/**
* @param {number[][]} grid
* @return {number[][]}
*/
var onesMinusZeros = function (grid) {
let newArr = [];
let row = [];
let col = [];
for (let i = 0; i < grid[0].length; i++) {
let temp = [];
for (let j = 0; j < grid.length; j++) {
temp.push(grid[j][i]);
}
newArr.push(temp);
}
for (let i = 0; i < grid.length; i++) {
row.push([sum(grid[i]), -(grid[i].length - sum(grid[i]))]);
}
for (let i = 0; i < newArr.length; i++) {
col.push([sum(newArr[i]), -(newArr[i].length - sum(newArr[i]))]);
}
const res = [];
for (let i = 0; i < row.length; i++) {
let temp = [];
for (let j = 0; j < col.length; j++) {
temp.push(sum([...row[i], ...col[j]]));
}
res.push(temp);
}
return res;
};
function sum(arr) {
return arr.reduce((a, b) => a + b);
}
const grid = [
[0, 1, 1],
[1, 0, 1],
[0, 0, 1],
];
console.log(onesMinusZeros(grid));
// @lc code=end