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. Linux错误码的含义

    C Name Value Description EPERM 1 Operation not permitted ENOENT 2 No such file or directory ESRCH 3 ...

  2. [CUDA] ubuntu14.04+cuda7.5下安装cudnn7.0

    cuda:7.5 cudnn:cudnn-7.0-linux-x64-v4.0-prod.tgz cudnn样例:cuDNN v4 Code Samples 1. 解压 tar -zxvf cudnn ...

  3. redis 安装及配置

    一.安装Redis 1.到官网下载redis最新版本,我下载的是 http://redis.io/ 2.拷贝redis-3.0.3到/usr/local目录 3.解压缩sudo tar -zxf re ...

  4. linux mysql字符编码问题

    发布:thatboy   来源:脚本学堂     [大 中 小] 本文介绍下,linux环境中mysql字符编码问题的解决办法,有遇到mysql编码问题的朋友,可以参考下本文的介绍,希望对你有一定的帮 ...

  5. wysiwyg editor

    http://www.bootcss.com/p/bootstrap-wysiwyg/

  6. OC修饰词 - 内存管理

    <招聘一个靠谱的 iOS>—参考答案(上) 说明:面试题来源是微博@我就叫Sunny怎么了的这篇博文:<招聘一个靠谱的 iOS>,其中共55题,除第一题为纠错题外,其他54道均 ...

  7. Mac下获取AppStore安装包文件路径

    通过远在大洋彼岸的苹果服务器下载东西,确实有够慢啊!AppStore更甚:甚至都经常提示连不上服务器,而有些软件呢,还必须从AppStore下载安装,所以没办法,谁让上了苹果的贼船呢!公司的网速更是不 ...

  8. C#中的委托事件的分析

    推荐:http://www.cnblogs.com/SkySoot/archive/2012/04/05/2433639.html 委托和事件在 .NET Framework 中的应用非常广泛,然而, ...

  9. avalon中require的实现

    var plugins = { loader: function(builtin) { window.define = builtin ? innerRequire.define : otherDef ...

  10. ubuntu14.04下unix网络编程环境的配置

    建议 unpv13e/README看一下,忽略一下内容 ===================================================================== 操作 ...