# 2482. Difference Between Ones and Zeros in Row and Column ###### tags: `Leetcode` `Medium` Link: https://leetcode.com/problems/difference-between-ones-and-zeros-in-row-and-column/description/ ## 思路 按照题意来 先算出```onesRow```和```onesCol``` 然后```zerosRow```和```zerosCol```就可以用```m```和```n```减```onesRow```和```onesCol```得到 ## Code ```java= class Solution { public int[][] onesMinusZeros(int[][] grid) { int m = grid.length, n = grid[0].length; int[] onesRow = new int[m]; int[] onesCol = new int[n]; for(int i=0; i<m; i++){ for(int j=0; j<n; j++){ if(grid[i][j]==1) onesRow[i]++; } } for(int i=0; i<n; i++){ for(int j=0; j<m; j++){ if(grid[j][i]==1) onesCol[i]++; } } int[][] diff = new int[m][n]; for(int i=0; i<m; i++){ for(int j=0; j<n; j++){ diff[i][j] = onesRow[i]+onesCol[j]-(m-onesRow[i])-(n-onesCol[j]); } } return diff; } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up