-
-
Save asdw3276/fda81d69ab0bc1b0bb3b to your computer and use it in GitHub Desktop.
1.7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class matrixsetzero{ | |
| public static void main(String[] args) | |
| { | |
| int[][] matrix = {{3,5,6,78,8},{1,2,4,5,6},{0,1,2,0,8},{0,0,1,1,1}}; | |
| matrix = matrixsetzero(matrix); | |
| for(int i = 0; i < matrix.length; i++) | |
| { | |
| System.out.println(); | |
| for(int j = 0; j < matrix[0].length; j++) | |
| { | |
| System.out.print(matrix[i][j]+","); | |
| } | |
| } | |
| } | |
| public static int[][] matrixsetzero(int[][] matrix) | |
| { | |
| if(matrix.length == 0) | |
| return matrix; | |
| int m = matrix.length; | |
| int n = matrix[0].length; | |
| int[] mflag = new int[m]; | |
| int[] nflag = new int[n]; | |
| for(int i = 0; i < m; i++) | |
| { | |
| for(int j = 0; j < n; j++) | |
| { | |
| if(matrix[i][j] == 0) | |
| { | |
| mflag[i] = 1; | |
| nflag[j] = 1; | |
| } | |
| } | |
| } | |
| for(int i = 0; i < m; i++) | |
| { | |
| for(int j = 0; j < n; j++) | |
| { | |
| if(mflag[i] == 1 || nflag[j] == 1) | |
| matrix[i][j] = 0; | |
| } | |
| } | |
| return matrix; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment