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. gitlab的安装以及汉化

    gitlab的安装 首先在网上下载好任意版本gitlab的rpm包 推荐下面的地址: https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gi ...

  2. 『奇葩问题集锦』Fedora ubuntu 下使用gulp 报错 Error: watch ENOSPC 解决方案

    用gulp启动,错误如下 Error: watch ENOSPC at exports._errnoException (util.js:746:11) at FSWatcher.start (fs. ...

  3. js事件流、事件处理程序/事件侦听器

    1.事件流 事件冒泡 IE的事件流叫做事件冒泡(event bubbling),即事件开始时由最具体的元素(文档中嵌套层次最深的那个节点)接收,然后逐级向上传播到较为不具体的节点(文档). 事件捕获 ...

  4. android中listview的item滑动删除效果(已解决listview点击问题)

    领导看到iphone上tableview有个滑动删除的效果,要求在android上也实现,搜了下资料,实现起来比较简单,可弄到后面,居然不能点击了,把一篇文章中的代码修改了一下,捣鼓了一番,搞定,下面 ...

  5. LeetCode【第217题】Contains Duplicate

    题目: ''' Given an array of integers, find if the array contains any duplicates. Your function should ...

  6. Android使用adb工具及root权限完成手机抓包

    1.环境准备/注意: 手机要求已经root. 首先需要配置JDK环境变量,这里主要讲解抓包,JDK环境变量配置跳过. 将包内附带的adb.zip解压到C盘根目录.  整个操作过程都需要用手机用数据线连 ...

  7. UIStackView 简单使用

    UIStackView提供了一个高效的接口用于平铺一行或一列的视图组合.对于嵌入到StackView的视图,你不用再添加自动布局的约束了.Stack View管理这些子视图的布局,并帮你自动布局约束. ...

  8. 偶尔转帖:AI会议的总结(by南大周志华)

    偶尔转帖:AI会议的总结(by南大周志华) 说明: 纯属个人看法, 仅供参考. tier-1的列得较全, tier-2的不太全, tier-3的很不全. 同分的按字母序排列. 不很严谨地说, tier ...

  9. 如何配置svn服务器(通过VisualServer服务器)

    如果你已经安装好了VisualServer服务器,现在让我们一起来配置svn服务器吧

  10. FFMPEG之TimeBase成员理解

    http://blog.csdn.net/supermanwg/article/details/14521869