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 ...
随机推荐
- Gradle实战教程之依赖管理
这是从我个人网站中复制过来的,原文地址:http://coolshell.info/blog/2015/05/gradle-dependency-management.html,转载请注明出处. 简要 ...
- OpenJudge 2787 算24
1.链接地址: http://poj.org/problem?id=1631 http://bailian.openjudge.cn/practice/2787/ 2.题目: 总时间限制: 3000m ...
- windows下使用xampp一键安装apache+php运行环境
感谢浏览,欢迎交流=.= 想为我老爸开发一套库存管理系统,借此机会打算使用下ext+php+apache+linux环境尝尝鲜. 为了在windows搭建本地开发测试环境,官网下载xampp,一键安装 ...
- Java基础巩固--正则表达式
本篇文章是学习尚学堂的关于正则表达式的视频教程时,所做的笔记.供广大编程爱好者学习之用,也留给日后自己复习使用! 1.为什么要有正则表达式? 正则表达式可以方便的对数据进行匹配,可以进行更加复杂的字符 ...
- 给定任意一个字符串,使用 for in 语句来统计字符出现的个数
//找出字符串中的数字 var str = 'haj123sdk54hask33dkhalsd879'; /*function findNum(str){ var arr = []; var tmp ...
- MySQL数据库原理
我们知道,数据是信息的载体——一种我们约定了如何解释的符号.在计算机系统中,最常见的应该是文本数据.我们用它记录配置信息,写日志,等等.而在应用程序中,按一定的数据结构来组织数据的方式叫做数据库管理系 ...
- html 头部正常用法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 【转】JSONP简介
原文链接:说说JSON和JSONP,也许你会豁然开朗,含jQuery用例 先说说JSONP是怎么产生的: 1.一个众所周知的问题,Ajax直接请求普通文件存在跨域无权限访问的问题,甭管你是静态页面. ...
- 对 HTTP 304 的理解(转)
最近和同事一起看Web的Cache问题,又进一步理解了 HTTP 中的 304 又有了一些了解. 304 的标准解释是:Not Modified 客户端有缓冲的文档并发出了一个条件性的请求(一般是提供 ...
- C# 中的委托和事件 转张子阳
引言 委托 和 事件在 .Net Framework中的应用非常广泛,然而,较好地理解委托和事件对很多接触C#时间不长的人来说并不容易.它们就像是一道槛儿,过了这个槛的人,觉得真是太容易了,而没有过去 ...