Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.

时间:2021-08-06 12:11:19

 

   1:      /// <summary>
   2:      /// Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.
   3:      /// </summary>
   4:      class Program
   5:      {
   6:          static void Main(string[] args)
   7:          {
   8:              Program p = new Program();
   9:              int[,] matrix = new int[3, 4] { { 1, 2, 3, 4 }, { 5, 6, 7, 0 }, { 9, 10, 11, 0 } };
  10:              p.SetColumnAndRowWithZeroWhileElementIsZero(ref matrix);
  11:              PrintMatrix(matrix);
  12:              Console.ReadKey();
  13:          }
  14:   
  15:          public void SetColumnAndRowWithZeroWhileElementIsZero(ref int[,] matrix)
  16:          {
  17:              int line = matrix.GetLength(0);
  18:              int column = matrix.GetLength(1);
  19:              if (line == 0 || column == 0)
  20:              {
  21:                  return;
  22:              }
  23:   
  24:              int[] rows = new int[line];
  25:              int[] colums = new int[column];
  26:   
  27:              //mark the row or column which will be set to zero
  28:              for (int i = 0; i < line; i++)
  29:              {
  30:                  for (int j = 0; j < column; j++)
  31:                  {
  32:                      if (matrix[i, j] == 0)
  33:                      {
  34:                          rows[i] = 1;
  35:                          colums[j] = 1;
  36:                      }
  37:                  }
  38:              }
  39:   
  40:              for (int i = 0; i < line; i++)
  41:              {
  42:                  for (int j = 0; j < column; j++)
  43:                  {
  44:                      if (rows[i] == 1 || colums[j] == 1)
  45:                      {
  46:                          matrix[i, j] = 0;
  47:                      }
  48:                  }
  49:              }
  50:          }
  51:          /// <summary>
  52:          /// Print the matrix to console
  53:          /// </summary>
  54:          public static void PrintMatrix(int[,] matrix)
  55:          {
  56:              int line = matrix.GetLength(0);
  57:              int column = matrix.GetLength(1);
  58:              if (line == 0 || column == 0)
  59:              {
  60:                  Console.WriteLine("empty array!");
  61:              }
  62:              else
  63:              {
  64:                  Console.WriteLine("lines:" + line.ToString());
  65:                  Console.WriteLine("columns:" + column.ToString());
  66:              }
  67:              for (int i = 0; i < line; i++)
  68:              {
  69:                  Console.WriteLine();
  70:                  for (int j = 0; j < column; j++)
  71:                  {
  72:                      Console.Write(matrix[i, j].ToString().PadLeft(5));
  73:                      Console.Write(" ");
  74:                  }
  75:              }
  76:          }
  77:      }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.的更多相关文章

  1. Leetcode&colon;378&period; Kth Smallest Element in a Sorted Matrix

    题目: Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the ...

  2. Leetcode&colon; Kth Smallest Element in a Sorted Matrix

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

  3. Kth Smallest Element in a Sorted Matrix -- LeetCode

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

  4. LeetCode 378&period; 有序矩阵中第K小的元素&lpar;Kth Smallest Element in a Sorted Matrix&rpar; 13

    378. 有序矩阵中第K小的元素 378. Kth Smallest Element in a Sorted Matrix 题目描述 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩 ...

  5. 【LeetCode】378&period; Kth Smallest Element in a Sorted Matrix 解题报告(Python)

    [LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

  6. &lbrack;LeetCode&rsqb; Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

  7. 378&period; Kth Smallest Element in a Sorted Matrix

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

  8. &lbrack;Swift&rsqb;LeetCode378&period; 有序矩阵中第K小的元素 &vert; Kth Smallest Element in a Sorted Matrix

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

  9. 378&period; Kth Smallest Element in a Sorted Matrix(java,优先队列)

    题目: Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the ...

随机推荐

  1. 新版macbook air OS X El Capitan 10&period;11安装WIN找不到驱动介质???

    这个问题已经解决 首先进入Boot Camp6 以后 顶上会有一个操作 -下载windowns 驱动程序 保存在优盘里面.然后再分区选择ISO(中间和你前面做的一样)电脑重启进入WIN安装 到你们出现 ...

  2. Nginx系列3之Nginx&plus;tomcat

    preface 公司部分应用跑得的tomcat,众所周知,tcomat高并发性能很弱,所以在处理静态请求的时候,我们就把他抛给Nginx处理,而Tomcat专门处理动态请求.所以在这里说说Nginx+ ...

  3. Codeforces Round &num;111 &lpar;Div&period; 2&rpar;

    Codeforces Round #111 (Div. 2) C. Find Pair 题意 给\(N(N \le 10^5)\)个数,在所有\(N^2\)对数中求第\(K(K \le N^2)\)对 ...

  4. Oracle分析函数 — sum&comma; rollup&comma; cube&comma; grouping用法

    本文通过例子展示sum, rollup, cube, grouping的用法. //首先建score表 create table score( class  nvarchar2(20), course ...

  5. javascript在html中使用 第10节

    javascript在html中使用 index.html: <html> <head> <title>javascript</title> <s ...

  6. HDOJ&lpar;HDU&rpar; 2093 考试排名&lpar;Arrays&period;sort排序、类的应用&rpar;

    Problem Description C++编程考试使用的实时提交系统,具有即时获得成绩排名的特点.它的功能是怎么实现的呢? 我们做好了题目的解答,提交之后,要么"AC",要么错 ...

  7. Java时间操作(一):关于UTC格式时间处理

    Java中获取形如:20160811T122132+08 格式,可以通过如下方法 package com.mc.others; public class UTCTimeTest { @Test pub ...

  8. BZOJ5019&lbrack;Snoi2017&rsqb;遗失的答案——FWT&plus;状压DP

    题目描述 小皮球在计算出答案之后,买了一堆皮肤,他心里很开心,但是一不小心,就忘记自己买了哪些皮肤了.==|||万 幸的是,他还记得他把所有皮肤按照1-N来编号,他买来的那些皮肤的编号(他至少买了一款 ...

  9. C&num;虚函数virtual详解

    在面向对象编程中,有两种截然不同的继承方式:实现继承和接口继承.在实现继承时候,在Java中,所有函数默认都是virtual的,而在C#中所有函数并不默认为virtual的,但可以在基类中通过声明关键 ...

  10. 《JavaScript Dom 编程艺术》读书笔记-第6章

    本章继续对图片库进行改进,主要内容包括三个方面: 1. 把事件处理函数移出文档: 2. 向后兼容: 3. 确保可访问. 之前代码的HTML片段,此时如果浏览器不支持JS,图片显示也能正常工作,只是体验 ...