首页 > 试题广场 >

清除行列

[编程题]清除行列
  • 热度指数:29246 时间限制:C/C++ 3秒,其他语言6秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

给定一个N阶方阵int[][](C++中为vector<vector><int>>)mat及其阶数n,若方阵中某个元素为0,则将其所在的行与列清零。返回改变后的int[][]方阵(C++中为vector<vector><int>>),保证n小于等于300,矩阵中的元素在nt范围内。</int></vector></int></vector>

测试样例:
[[1,2,3],[0,1,2],[0,0,1]]
返回:[[0,0,3],[0,0,0],[0,0,0]]
推荐

//楼上的Java版本有点复杂,写了个简单点的。
publicclassClearer {
    publicint[][] clearZero(int[][] mat, intn) {
        // write code here
        boolean[] rowArray = newboolean[n];
        boolean[] columnArray = newboolean[n];
         //记录为0的位置,把相应的行列位置设为true
        for(inti=0; i<n;i++)
        {
            for(intj=0;j<n;j++)
            {
                if(mat[i][j]==0)
                {
                    rowArray[i]=true;
                    columnArray[j]=true;
                }
            }         
        }
         //遍历找到之前记录的位置,把相应行列赋值为0
        for(inti=0;i<n;i++)
        {
            for(intj=0;j<n;j++)
            {
                if(rowArray[i]||columnArray[j])
                {
                mat[i][j] = 0;
                }
            }
        }
        returnmat;
    }
}

编辑于 2015-08-18 20:38:06 回复(15)
public class Clearer {
	public int[][] clearZero(int[][] mat, int n) {
		// write code here
		if (mat == null) {
			return mat;
		}
		
		int row_num = mat.length;
		int column_num = mat[0].length;
		boolean[] row_flag = new boolean[row_num];
		boolean[] column_flag = new boolean[column_num];

		for (int i = 0; i < row_num; i++) {
			for (int j = 0; j < column_num; j++) {
				if (mat[i][j] == 0) {
					row_flag[i] = true;
					column_flag[j] = true;
				}
			}
		}

		for (int i = 0; i < row_num; i++) {
			for (int j = 0; j < column_num; j++) {
				if(row_flag[i] || column_flag[j]) {
					mat[i][j] = 0;
				}
			}
		}

		return mat;
	}
}

发表于 2019-12-12 10:47:55 回复(0)
import java.util.*;

public class Clearer {
    public int[][] clearZero(int[][] mat, int n) {
        // write code here
        ArrayList<position> list = new ArrayList<>();
        int x,y;
        for(int i = 0;i<n;i++){
            for(int j = 0;j<n;j++){
                if(mat[i][j]==0){
                    list.add(new position(i,j));
                }
            }
        }
        
        for(position p : list){
            for(int i = 0;i<n;i++){
                mat[i][p.y] = 0;
                mat[p.x][i] = 0;
            }
        }
        return mat;
    }
}
class position{
    int x,y;
    public position(int x,int y){
        this.x = x;
        this.y = y;
    }
}

发表于 2018-11-26 11:15:29 回复(0)
import java.util.*;

public class Clearer {
public int[][] clearZero(int[][] mat, int n) {

ArrayList<Integer> rowTemp = new ArrayList<Integer>();   //定义一个数组用来保存元素为0的行角标
ArrayList<Integer> columnTemp = new ArrayList<Integer>();  //定义一个数组用来保存元素为0的列角标

for(int i = 0; i<n;i++){
for(int j = 0; j<n ; j++)
{
Boolean rowBool = rowTemp.contains(i);            //判断此时的行角标是否在数组里
Boolean columnBool = columnTemp.contains(j);      //判断此时的列角标是否在数组里
if(mat[i][j] == 0 & !rowBool & !columnBool)      //判断此时的元素是否为0且行和列角标都不在记录的数组里面
{
for(int a = 0 ;a < n ;a++)                    //把所在的行和列元素清0
{
mat[i][a] = 0;
mat[a][j] = 0;
}
rowTemp.add(i);                               //记录此时的行角标
columnTemp.add(j);                            //记录此时的列角标
}
}
}

return mat;
}
}








在本地运行没问题,为什么在线调试说不能通过所有的测试用例?求大神帮帮忙
编辑于 2018-04-21 11:48:21 回复(0)
//自己想到的方法,跟大神的不谋而合,两个整形数组改成boolea数组为占用更小空间,懒得改了
public int[][] clearZero(int[][] mat, int n) {
       if(mat==null||n<=0) return mat;
       int[] column=new int[n];
       int[] row=new int[n];
       for(int i=0;i<n;i++){
           for(int j=0;j<n;j++){
               if(mat[i][j]==0){
                   row[i]=1;
                   column[j]=1;
               }  
           }
       }
       for(int i=0;i<n;i++){
           for(int j=0;j<n;j++)if(row[i]==1||column[j]==1) mat[i][j]=0;
       }
       return mat;
    }

编辑于 2017-05-11 00:30:28 回复(0)
import java.util.*;

public class Clearer {
       public int[][] clearZero(int[][] mat, int n) {
    	int [][] res = mat;
        ArrayList<Integer> i_list = new ArrayList<>();
        ArrayList<Integer> j_list = new ArrayList<>();
        for (int i = 0; i < n; i ++) {
            for (int j = 0; j < n; j ++) {
                if (mat[i][j] == 0) {
                    i_list.add(i);
                    j_list.add(j);
                }
            }
        }

		 for (int l = 0; l < i_list.size(); l ++) {
                   for (int k = 0; k < n;k ++) {
                       res[k][j_list.get(l)] = 0;
                       res[i_list.get(l)][k] = 0;
                   }
               }
        return res;
    }
}

发表于 2017-03-14 23:03:44 回复(0)
import java.util.*;

public class Clearer {
    public int[][] clearZero(int[][] mat, int n) {
        // write code here
        boolean[][] loc = new boolean[n][n];
        
        for(int row=0; row<n; row++){
            for(int col=0; col<n; col++){
                if(mat[row][col]==0){
                    loc[row][col] = true;
                }else{
                    loc[row][col] = false;
                }
            }
        }
        
        for(int row=0; row<n; row++){
            for(int col=0; col<n; col++){
                if(loc[row][col]==true){
                    for(int i=0; i<n; i++){
                        mat[row][i] = 0;
                        mat[i][col] = 0;
                    }
                }
            }
        }
        return mat;
    }
}

发表于 2017-03-13 16:52:18 回复(0)
import java.util.*;

public class Clearer {
    public int[][] clearZero(int[][] mat, int n) {
        // write code here
        HashSet<Integer> rowSet = new HashSet<Integer>();
        HashSet<Integer> colSet = new HashSet<Integer>();
        
        for (int i = 0; i < n; i ++) {
            for (int j = 0; j < n; j ++) {
                if (mat[i][j] == 0) {
                    rowSet.add(i);
                    colSet.add(j);
                }
            }
        }
        
        Iterator<Integer> rowIterator = rowSet.iterator();
        while (rowIterator.hasNext()) {
            int zeroRow = rowIterator.next();
            for (int j = 0; j < n; j ++) {
                mat[zeroRow][j] = 0;
            }
        }
        
        Iterator<Integer> colIterator = colSet.iterator();
        while (colIterator.hasNext()) {
            int zeroCol = colIterator.next();
            for (int i = 0; i < n; i ++) {
                mat[i][zeroCol] = 0;
            }
        }
        
        return mat;
    }
}

使用Java最容易想到的应用是使用HashSet来存储的为0的行和列,然后清零就好了。
发表于 2017-01-20 20:06:20 回复(0)
public class Main {
    public int[][] clearZero(int[][] mat, int n) {//找到元素为0的坐标,并且保存在栈里;
    Stack<Integer>stack=new Stack<Integer>();
          for (int i = 0; i < mat.length; i++) {
     for (int j = 0; j < mat.length; j++) {
if (mat[i][j]==0) {//遇到为0的元素就将数组下标入栈,我是先入栈横坐标,再入栈纵坐标;
       stack.push(i);
       stack.push(j);
}
	}
}
        dele(mat,stack,n);//调用dele函数;进行清零操作
       return mat;
    }
     public  int[][] dele(int[][] mat, Stack<Integer> stack,int n)
          {
         int stlen=stack.size();
         for (int i = 0; i <stlen/2; i++) {
         int x=stack.pop();//出栈纵坐标
         int y=stack.pop();//出栈横坐标
         for(int i1=0;i1<n;i1++){
         mat[i1][x]=0;//将纵坐标x所在的列全部清0;
         mat[y][i1]=0;//将横坐标y所在的列全部清0;
          }
}
for (int i = 0; i < mat.length; i++) {//遍历清0后的数组
for (int j = 0; j < mat.length; j++) {
System.out.print("  "+mat[i][j]);
}
System.out.println();
}
return mat;
        }
    public static void main(String[] args) {
int [][]t={{1,2,3,4},{0,1,2,3},{0,0,1,1},{1,2,3,4}};
   Main main=new Main();
main.clearZero(t, t.length);
}
}

这是我的解法,本来打算用数组保存元素为0的下标,不过又感觉麻烦,就试着用栈存了一下,结果可以;不过我个人觉得这个方法不好,希望大家多多指教。
发表于 2016-09-12 19:28:29 回复(0)
import java.util.*;

public class Clearer {
    public int[][] clearZero(int[][] mat, int n) {
        // write code here
        HashMap<Integer,Integer> row=new HashMap<Integer,Integer>();
        HashMap<Integer,Integer> clo=new HashMap<Integer,Integer>();
        ArrayList<Integer> ro=new ArrayList<Integer>();
        ArrayList<Integer> cl=new ArrayList<Integer>();
        
        for(int i=0;i<n;i++)
            {
            for(int j=0;j<n;j++)
                {
                if(mat[i][j]==0)
                    {
                    if(!row.containsKey(i))
                        {
                    		row.put(i,i);
                        	ro.add(i);
                    	}
                     if(!clo.containsKey(j))
                        {
                    		clo.put(j,j);
                        	cl.add(j);
                    	}
                }
            }
        }
        if(ro.size()!=0)
        {
            int num=0;
        	while(num<ro.size())
                {
        	for(int i=0;i<n;i++)
            {
            	mat[ro.get(num)][i]=0;
        	}
                num++;
            }
        }
        if(cl.size()!=0)
        {
            int num=0;
        	while(num<cl.size())
                {
        	for(int i=0;i<n;i++)
            {
            	mat[i][cl.get(num)]=0;
            }
                num++;
            }
        
        }
        
        return mat;
    }
}

发表于 2016-08-31 22:50:23 回复(0)
import java.util.*;

public class Clearer {
    public int[][] clearZero(int[][] mat, int n) {
        // write code here
        HashSet<Integer> xHash = new HashSet<>();
        HashSet<Integer> yHash = new HashSet<>();
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if(mat[i][j] == 0){
                    xHash.add(i);
                    yHash.add(j);
                }
            }
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if(xHash.contains(i) || yHash.contains(j))
                    mat[i][j] = 0;
            }
        }
        return mat;
    }
}

发表于 2016-08-29 11:19:13 回复(0)