基于Java的矩阵归零问题研究与实现

今天在微信公众号上看到一个矩阵归零的问题,自己编写处理了一下,现总结如下:

1、问题描述


2、解决思路:


3、代码实现:

构造一个对象Num,记录X,Y坐标及该数

public classNum {

   //数值

   private int num;

   //X坐标

   private int x;

   //Y坐标

   private int y;

 

   public Num(intnum,intx,inty) {

      this.num =num;

      this.x =x;

      this.y =y;

   }

 

   public int getNum() {

      return num;

   }

 

   public void setNum(intnum) {

      this.num =num;

   }

 

   public int getX() {

      return x;

   }

 

   public void setX(intx) {

      this.x =x;

   }

 

   public int getY() {

      return y;

   }

 

   public void setY(inty) {

      this.y =y;

   }

 

}

数量方法

public classPic {

  

   //打印图

   public void printGraph(int[][]edges) {

      for (introw = 0;row <edges.length;row++) {

         for (intcol = 0;col <edges[row].length;col++) {

            System.out.print(edges[row][col]);

            System.out.print(" ");

         }

         System.out.println();

      }

   }

 

   //加入队列

   public Queue<Num>addZeroQueue(int[][]edges){

      Queue<Num> zeroQueue = newConcurrentLinkedQueue<>();

      for (introw = 0;row <edges.length;row++) {

         for (intcol = 0;col <edges[row].length;col++) {

            intdata =edges[row][col];

            if (data == 0) {

               Num zeroNum = newNum(data,row,col);

                zeroQueue.add(zeroNum);

            }

         }

      }

      return zeroQueue;

   }

  

   //处理数据,若不包含则加入队列,行列分别置空

   public int[][]handlerData(Queue<Num>resultQueue,int[][]edges){

      List<Integer>rowList= newArrayList<Integer>();

      List<Integer>colList= newArrayList<Integer>();

      for (Numnum :resultQueue) {

         int x = num.getX();

         if (!rowList.contains(x)) {

            rowList.add(x);

         }

         int y = num.getY();

         if (!colList.contains(y)) {

            colList.add(y);

         }

      }

 

      for (inti = 0;i <rowList.size();i++) {

         int row = rowList.get(i);

         for (intj = 0;j <edges[row].length;j++) {

            edges[row][j] = 0;

         }

      }

 

      for (inti = 0;i <colList.size();i++) {

         int col = colList.get(i);

         for (intj = 0;j <edges.length;j++) {

            edges[j][col] = 0;

         }

      }

      return edges;

   }

}

 

测试类:

public staticvoidmain(String[] args){

      int[][]edges = { { 1, 6, 3, 2, 4, 5}, { 6, 0, 2, 5, 8, 1 }, { 3, 2, 6, 3, 4, 9 }, { 3, 5, 3, 5, 5, 3 },

            {5, 8, 4, 5, 9, 5 }, { 6, 9, 6, 3, 0, 8 }, { 2, 6, 3, 6, 0, 1 } };

      Picpic= newPic();

      pic.printGraph(edges);

      Queue<Num>resultQueue= pic.addZeroQueue(edges);

      edges = pic.handlerData(resultQueue, edges);

      System.out.println("---处理后---");

      pic.printGraph(edges);

   }

结果图示:




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值