Skip to content

Commit 87bb9ec

Browse files
committed
feat: add solutions to lc problem: No.2482
No.2482.Difference Between Ones and Zeros in Row and Column
1 parent e90f17b commit 87bb9ec

File tree

5 files changed

+249
-0
lines changed

5 files changed

+249
-0
lines changed

solution/2400-2499/2482.Difference Between Ones and Zeros in Row and Column/README.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,94 @@ func onesMinusZeros(grid [][]int) [][]int {
179179
}
180180
```
181181

182+
### **TypeScript**
183+
184+
```ts
185+
function onesMinusZeros(grid: number[][]): number[][] {
186+
const m = grid.length;
187+
const n = grid[0].length;
188+
const rows = new Array(m).fill(0);
189+
const cols = new Array(n).fill(0);
190+
for (let i = 0; i < m; i++) {
191+
for (let j = 0; j < n; j++) {
192+
if (grid[i][j]) {
193+
rows[i]++;
194+
cols[j]++;
195+
}
196+
}
197+
}
198+
const ans = Array.from({ length: m }, () => new Array(n).fill(0));
199+
for (let i = 0; i < m; i++) {
200+
for (let j = 0; j < n; j++) {
201+
ans[i][j] = rows[i] + cols[j] - (m - rows[i]) - (n - cols[j]);
202+
}
203+
}
204+
return ans;
205+
}
206+
```
207+
208+
### **Rust**
209+
210+
```rust
211+
impl Solution {
212+
pub fn ones_minus_zeros(grid: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
213+
let m = grid.len();
214+
let n = grid[0].len();
215+
let mut rows = vec![0; m];
216+
let mut cols = vec![0; n];
217+
for i in 0..m {
218+
for j in 0..n {
219+
if grid[i][j] == 1 {
220+
rows[i] += 1;
221+
cols[j] += 1;
222+
}
223+
}
224+
}
225+
let mut ans = vec![vec![0; n]; m];
226+
for i in 0..m {
227+
for j in 0..n {
228+
ans[i][j] = (rows[i] + cols[j] - (m - rows[i]) - (n - cols[j])) as i32;
229+
}
230+
}
231+
ans
232+
}
233+
}
234+
```
235+
236+
### **C**
237+
238+
```c
239+
/**
240+
* Return an array of arrays of size *returnSize.
241+
* The sizes of the arrays are returned as *returnColumnSizes array.
242+
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
243+
*/
244+
int **onesMinusZeros(int **grid, int gridSize, int *gridColSize, int *returnSize, int **returnColumnSizes) {
245+
int *rows = malloc(sizeof(int) * gridSize);
246+
int *cols = malloc(sizeof(int) * gridColSize[0]);
247+
memset(rows, 0, sizeof(int) * gridSize);
248+
memset(cols, 0, sizeof(int) * gridColSize[0]);
249+
for (int i = 0; i < gridSize; i++) {
250+
for (int j = 0; j < gridColSize[0]; j++) {
251+
if (grid[i][j]) {
252+
rows[i]++;
253+
cols[j]++;
254+
}
255+
}
256+
}
257+
int **ans = malloc(sizeof(int *) * gridSize);
258+
for (int i = 0; i < gridSize; i++) {
259+
ans[i] = malloc(sizeof(int) * gridColSize[0]);
260+
for (int j = 0; j < gridColSize[0]; j++) {
261+
ans[i][j] = rows[i] + cols[j] - (gridSize - rows[i]) - (gridColSize[0] - cols[j]);
262+
}
263+
}
264+
*returnSize = gridSize;
265+
*returnColumnSizes = gridColSize;
266+
return ans;
267+
}
268+
```
269+
182270
### **...**
183271
184272
```

solution/2400-2499/2482.Difference Between Ones and Zeros in Row and Column/README_EN.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,94 @@ func onesMinusZeros(grid [][]int) [][]int {
161161
}
162162
```
163163

164+
### **TypeScript**
165+
166+
```ts
167+
function onesMinusZeros(grid: number[][]): number[][] {
168+
const m = grid.length;
169+
const n = grid[0].length;
170+
const rows = new Array(m).fill(0);
171+
const cols = new Array(n).fill(0);
172+
for (let i = 0; i < m; i++) {
173+
for (let j = 0; j < n; j++) {
174+
if (grid[i][j]) {
175+
rows[i]++;
176+
cols[j]++;
177+
}
178+
}
179+
}
180+
const ans = Array.from({ length: m }, () => new Array(n).fill(0));
181+
for (let i = 0; i < m; i++) {
182+
for (let j = 0; j < n; j++) {
183+
ans[i][j] = rows[i] + cols[j] - (m - rows[i]) - (n - cols[j]);
184+
}
185+
}
186+
return ans;
187+
}
188+
```
189+
190+
### **Rust**
191+
192+
```rust
193+
impl Solution {
194+
pub fn ones_minus_zeros(grid: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
195+
let m = grid.len();
196+
let n = grid[0].len();
197+
let mut rows = vec![0; m];
198+
let mut cols = vec![0; n];
199+
for i in 0..m {
200+
for j in 0..n {
201+
if grid[i][j] == 1 {
202+
rows[i] += 1;
203+
cols[j] += 1;
204+
}
205+
}
206+
}
207+
let mut ans = vec![vec![0; n]; m];
208+
for i in 0..m {
209+
for j in 0..n {
210+
ans[i][j] = (rows[i] + cols[j] - (m - rows[i]) - (n - cols[j])) as i32;
211+
}
212+
}
213+
ans
214+
}
215+
}
216+
```
217+
218+
### **C**
219+
220+
```c
221+
/**
222+
* Return an array of arrays of size *returnSize.
223+
* The sizes of the arrays are returned as *returnColumnSizes array.
224+
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
225+
*/
226+
int **onesMinusZeros(int **grid, int gridSize, int *gridColSize, int *returnSize, int **returnColumnSizes) {
227+
int *rows = malloc(sizeof(int) * gridSize);
228+
int *cols = malloc(sizeof(int) * gridColSize[0]);
229+
memset(rows, 0, sizeof(int) * gridSize);
230+
memset(cols, 0, sizeof(int) * gridColSize[0]);
231+
for (int i = 0; i < gridSize; i++) {
232+
for (int j = 0; j < gridColSize[0]; j++) {
233+
if (grid[i][j]) {
234+
rows[i]++;
235+
cols[j]++;
236+
}
237+
}
238+
}
239+
int **ans = malloc(sizeof(int *) * gridSize);
240+
for (int i = 0; i < gridSize; i++) {
241+
ans[i] = malloc(sizeof(int) * gridColSize[0]);
242+
for (int j = 0; j < gridColSize[0]; j++) {
243+
ans[i][j] = rows[i] + cols[j] - (gridSize - rows[i]) - (gridColSize[0] - cols[j]);
244+
}
245+
}
246+
*returnSize = gridSize;
247+
*returnColumnSizes = gridColSize;
248+
return ans;
249+
}
250+
```
251+
164252
### **...**
165253
166254
```
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Return an array of arrays of size *returnSize.
3+
* The sizes of the arrays are returned as *returnColumnSizes array.
4+
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
5+
*/
6+
int **onesMinusZeros(int **grid, int gridSize, int *gridColSize, int *returnSize, int **returnColumnSizes) {
7+
int *rows = malloc(sizeof(int) * gridSize);
8+
int *cols = malloc(sizeof(int) * gridColSize[0]);
9+
memset(rows, 0, sizeof(int) * gridSize);
10+
memset(cols, 0, sizeof(int) * gridColSize[0]);
11+
for (int i = 0; i < gridSize; i++) {
12+
for (int j = 0; j < gridColSize[0]; j++) {
13+
if (grid[i][j]) {
14+
rows[i]++;
15+
cols[j]++;
16+
}
17+
}
18+
}
19+
int **ans = malloc(sizeof(int *) * gridSize);
20+
for (int i = 0; i < gridSize; i++) {
21+
ans[i] = malloc(sizeof(int) * gridColSize[0]);
22+
for (int j = 0; j < gridColSize[0]; j++) {
23+
ans[i][j] = rows[i] + cols[j] - (gridSize - rows[i]) - (gridColSize[0] - cols[j]);
24+
}
25+
}
26+
*returnSize = gridSize;
27+
*returnColumnSizes = gridColSize;
28+
return ans;
29+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
impl Solution {
2+
pub fn ones_minus_zeros(grid: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
3+
let m = grid.len();
4+
let n = grid[0].len();
5+
let mut rows = vec![0; m];
6+
let mut cols = vec![0; n];
7+
for i in 0..m {
8+
for j in 0..n {
9+
if grid[i][j] == 1 {
10+
rows[i] += 1;
11+
cols[j] += 1;
12+
}
13+
}
14+
}
15+
let mut ans = vec![vec![0; n]; m];
16+
for i in 0..m {
17+
for j in 0..n {
18+
ans[i][j] = (rows[i] + cols[j] - (m - rows[i]) - (n - cols[j])) as i32;
19+
}
20+
}
21+
ans
22+
}
23+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function onesMinusZeros(grid: number[][]): number[][] {
2+
const m = grid.length;
3+
const n = grid[0].length;
4+
const rows = new Array(m).fill(0);
5+
const cols = new Array(n).fill(0);
6+
for (let i = 0; i < m; i++) {
7+
for (let j = 0; j < n; j++) {
8+
if (grid[i][j]) {
9+
rows[i]++;
10+
cols[j]++;
11+
}
12+
}
13+
}
14+
const ans = Array.from({ length: m }, () => new Array(n).fill(0));
15+
for (let i = 0; i < m; i++) {
16+
for (let j = 0; j < n; j++) {
17+
ans[i][j] = rows[i] + cols[j] - (m - rows[i]) - (n - cols[j]);
18+
}
19+
}
20+
return ans;
21+
}

0 commit comments

Comments
 (0)