Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.
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.的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- LeetCode 378. 有序矩阵中第K小的元素(Kth Smallest Element in a Sorted Matrix) 13
378. 有序矩阵中第K小的元素 378. Kth Smallest Element in a Sorted Matrix 题目描述 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩 ...
- 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)
[LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- [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 ...
- 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 ...
- [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 ...
- 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 ...
随机推荐
- Scala - error: not found: value SortedMap
先 IMPORT!!!! scala> import scala.collection._import scala.collection._ scala> SortedMap(" ...
- 【转】使用PHP创建基本的爬虫程序
Web Crawler, 也时也称scrapers,即网络爬虫,用于自动搜索internet并从中提取 想要的内容.互联网的发展离不开它们.爬虫是搜索引擎的核心,通过智能算法发现符合 你输入的关键字的 ...
- Nginx中让 重写后的路径 自动增加斜线 /
http://www.111cn.net/sys/nginx/56067.htm(参考文章) 现在有个这样的需求,在重写的url地址后,自动加斜线 / 例如 xx.com/abc/1-2 (默认ur ...
- c++与C# winform的消息通讯--(结构体与byte数组的使用)
近期正在做一个蓝牙驱动的使用程序,其中有一块从c++发送数据到C#的部分,网上查了很多资料,大多都是介绍如何通过调用函数获取用户数据.并且在消息发送中,很少介绍如何发送一个结构体,并且结构体里面有 b ...
- HTML5列表
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- PHP输出图片文件,实现浏览器缓存机制
$ETag = '"'.md5($file).'"'; if(env('HTTP_IF_NONE_MATCH') === $ETag){ header ( 'ETag: '.$ET ...
- SQL 多条件查询
网上有不少人提出过类似的问题:“看到有人写了WHERE 1=1这样的SQL,到底是什么意思?”.其实使用这种用法的开发人员一般都是在使用动态组装的SQL.让我们想像如下的场景:用户要求提供一个灵活的查 ...
- 隐私:网民最常用密码MD5解密
国内知名网络安全商瑞星公司曾发布过一项针对密码强度的专业研究报告,这项研究中列举了中国网民和美国网民最常用的密码集.研究表明,全球互联网大部分用户在密码使用中都存在着种种疏漏,一些极其简单的密码被广泛 ...
- Mvc学习笔记(2)
Razor模板的具体语法使用 因为Razor模板的可以自动识别<>,大大减少了代码量,本节我们一起来探究Razor模板的语法的简单应用: MVC知识点: 1.ASP.NET Mvc框架 是 ...
- 15个Docker基本命令及用法
Docker入门教程:15个Docker基本命令及用法 本文中,我们将学习15个Docker命令以及命令的用法和功能,并通过实践学习它是如何工作的. AD:51CTO 网+ 第十二期沙龙:大话数据 ...