Skip to content

Instantly share code, notes, and snippets.

@gaoyike
Created June 16, 2014 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gaoyike/bf57c6e80674c04d44b2 to your computer and use it in GitHub Desktop.
Save gaoyike/bf57c6e80674c04d44b2 to your computer and use it in GitHub Desktop.
1.7
/**
* 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