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:378. 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: 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. 有序矩阵中第K小的元素(Kth Smallest Element in a Sorted Matrix) 13

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

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

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

  6. [LeetCode] 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. 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. [Swift]LeetCode378. 有序矩阵中第K小的元素 | 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. 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. Spring框架的初步学习

    (1) IOC 控制反转 所谓的控制反转就是应用本身不负责依赖对象的创建和维护,依赖对象的创建及维护是由 外部容器负责的(spring是外部容器之一).这样控制权就由应用转移到了外部容器,控制权 的转 ...

  2. 配置trac

    1. enable apache mod_auth_digest 2. 设置Location

  3. BufferedReader、FileReader、FileInputStream的区别

    一.BufferReader BufferedReader   由Reader类扩展而来,提供通用的缓冲方式文本读取,而且提供了很实用的readLine,读取分行文本很适合,BufferedReade ...

  4. 深入了解line-height

    1.定义   行高:两行文字baseline(基线)之间的距离 示意图: 2.为何line-height可以让单行文本垂直居中       其实并没有垂直居中,除非将font-size:0; 3.li ...

  5. 深入了解overflow

    1.如果overflow-x与overflow-y值不同   其中一个赋值为visiable,另一个赋值scroll/auto/hidden,那么visiable会重置为auto 2.overflow ...

  6. $.ajax参数备注-转转转

    jquery中的ajax方法参数总是记不住,这里记录一下.  $,ajax()方法参数详解 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为St ...

  7. VS建立可供外部调用的MFC类DLL,C#调用MFC调用

    建立MFC DLL工程.一般选共享MFC库 关键是在你生成的CPP中,添加外部调用的接口 如下,意思是将这个函数对外公开. 如果你希望对外提供类,就把这个方法做成工厂. 如果你希望对外提供MFC的窗体 ...

  8. VMware Workstation 10.0 下载 – 正版序列号+简体中文官方原版

    1.https://download3.vmware.com/software/wkst/file/VMware-workstation-full-10.0.0-1295980.exe 2.https ...

  9. What is Windows Clustering

    A cluster is a group of independent computer systems, referred to as nodes, working together as a un ...

  10. django之JavaScript的简单学习2

    前言:ajax预备知识:json进阶 1.JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.JSON是用字符串来表示Javascript对象: 请大家记住一 ...