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. WORDPRESS插件开发(二)HELLO WORLD改进版

    在上一篇文章中WORDPRESS插件开发(一)HELLO WORLD,演示了Hello World的最简单实现,只是在每篇文章的后面加入Hello World字符,而且字符也是写死的. 如果用户需要自 ...

  2. Checbox的操作含已选、未选及判断代码

    Checbox的操作包括已选.未选.判断等等,下面有个不错的示例,使用jquery完成,感兴趣的朋友可以参考下 $("#chk1").attr("checked" ...

  3. poj 3185 The Water Bowls

    The Water Bowls 题意:给定20个01串(最终的状态),每个点变化时会影响左右点,问最终是20个0所需最少操作数? 水题..直接修改增广矩阵即可:看来最优解不是用高斯消元(若是有Gaus ...

  4. C++ 11 笔记 (六) : 随机数

    以前生成一个随机数都是这样: srand(time(NULL)); rand(); 在C++11中,标准库中增加了随机数引擎 std::default_random_engine 这个好东西,然后我们 ...

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

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

  6. Mvc基础(1)

  7. mysql数据库中某项其中一个值在该项排第几,百分几

    SQL 如下: sql 1. SELECT X.USER_ID, X.TOTAL_NO, X.ORDER_NO, X.ORDER_NO / X.TOTAL_NO AS PERCENTAGE_NO AS ...

  8. COGS 577 蝗灾 [CDQ分治入门题]

    题目链接 昨天mhr神犇,讲分治时的CDQ分治的入门题. 题意: 你又一个w*w正方形的田地. 初始时没有蝗虫. 给你两个操作: 1. 1 x y z: (x,y)这个位置多了z只蝗虫. 2. 2 x ...

  9. 【技术贴】三星Note8 N5100实用教程,关闭相机快门声,增加浏览器退出按钮。

    需要root 增加快门声按钮: 在\system\csc\目录下,有个others.xml的手机功能定制文件,用root explorer之类可以修改系统文件权限的文本修改工具编辑它,在文件最末添加这 ...

  10. SQL2005 安装时 “性能监视器计数器要求(错误)” 解决方案

    转自SQL2005 安装时 "性能监视器计数器要求(错误)" 解决方案 出现此类问题一般都是在非法卸载sql2005出现的 在 "开始" -->  &qu ...