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 ...
随机推荐
- linux管理网络连接指令
ethtool eth0 查看 eth0对应网卡的设置 ethtool -i eth0 查看 eth0网卡的驱动信息 ethtool -S eth0 查看网卡的统计信息 ethtool -s eth ...
- 多语言文本资源的访问(Windows:ini)
目标 本文要讨论对于开发多语言界面程序所需要解决的一个问题,即文本资源组织及访问的方法. 本文主要以Windows平台下讨论具现并提供处理代码. Windows方案 Windows下界面开发,除Dir ...
- python 访问php程序,实现定时
#!/usr/bin/python #test2.py import sys import urllib2 j = True jj = 1##########用于统计,所以分页, url = 'htt ...
- NFS,FTP
一. NFS1. NFS简介NFS全称是network file systemNFS允许一个系统在网络上与他人共享目录和文件.通过使用NFS,用户和程序可以像访问本地文件一样访问远端系统上的文件. 假 ...
- Sublime Text3 个人使用心得
sublime与webstorm的比较: webstorm真心很强大,强大到能够几乎满足所有前端开发者编程的需求,方便的快捷键操作.代码提示.浏览器查看.工程管理.历史记录(可以找到之前编辑的内容,即 ...
- 诡异的XmlSerializer属性字段Specified
自动生成代码时,往往会为一个字段假设为 * , 生成另一个bool型字段: *Specified: 如: [Serializable] public class A { [XmlElement] pu ...
- 『奇葩问题集锦』function * (next){ 执行报错 SyntaxError: Unexpected token *
这是因为 app.use(function * (){ 语句中有一个 * ,这种方式被称为generator functions ,一般写作function *(){...} 的形式,在此类func ...
- php类的方法
方法就是在类中的function,很多时候我们分不清方法与函数有什么差别,在面向过程的程序设计中function叫做函数,在面向对象中function则被称之为方法. 同属性一样,类的方法也具有pub ...
- zzuli oj 1178 单词数
Description 统计一篇文章里不同单词的总数. Input 有多组数据,每组一行,每组就是一篇小文章.每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束. Output 每 ...
- Axure草记
页面控件和DataSet绑定,DataSet和输入控件绑定(通过临时变量) 双击Repeater进入之后,你会发现下面已经默认添加了3行,这代表着,每增加一行将会重复3遍: Repeater可以只是部 ...