-
-
Save gaoyike/bf57c6e80674c04d44b2 to your computer and use it in GitHub Desktop.
1.7
This file contains 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
/** | |
* Created by Readman on 6/16/14. | |
*/ | |
public class setzero { | |
/* | |
time: n^2 | |
space: n | |
* */ | |
public static void main(String[] args) { | |
int[][] image = new int[][] {{1,2,0},{4,5,6},{7,8,9}}; | |
set(image); | |
for (int i = 0; i < image.length; i++) { | |
for (int j = 0; j < image[0].length; j++) { | |
System.out.print(image[i][j] +" "); | |
} | |
System.out.println(" "); | |
} | |
} | |
public static void set(int[][] ary) { | |
boolean[] row = new boolean[ary.length]; | |
boolean[] col = new boolean[ary[0].length]; | |
for (int i = 0; i < ary.length; i++) { | |
for (int j = 0; j < ary[0].length; j++) { | |
if (ary[i][j] == 0) | |
{ | |
row[i] = true; | |
col[j] = true; | |
} | |
} | |
} | |
for (int i = 0; i < ary.length; i++) { | |
for (int j = 0; j < ary[0].length; j++) { | |
if (row[i] || col[j]) | |
{ | |
ary[i][j] = 0; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment