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 ...
随机推荐
- Hibernate的检索策略
hibernate 的中的session依照load()和get()按照参数的制定OID(ObjctID)去加载一个持久化对象.另外Query.list()方法则按照HQL语句去加载持久化的对象. 以 ...
- Centos学习手册——装逼宝典之强制重置密码
---恢复内容开始--- Centos学习手册by RuffianFish; 痞子鱼 近日闲的无聊,而最近又在搞Centos决定写个Centos详细的学习手册,以便自己在长时间没摸Centos的情况下 ...
- jQuery iframe 自适应高宽度
Html <iframe id="你的id" src="你要嵌入的页面" scrolling="no" frameborder=&qu ...
- Navicat Premium 未保存的SQL如何找回 ?
在使用 Navicat Premium 编辑SQL的过程中为防止程序意外崩溃,已经将编辑的SQL都已经备份. 备份存放目录地址:C:\Users\{登录用户名}\Documents\Navicat\M ...
- 如何得到django中form表单里的复选框(多选框)的值( MultipleChoiceField )
直接写代码吧 CHECKBOX_CHOICES = ( ('Value1','Value1'), ('Value2','Value2'), ) class EditProfileForm(ModelF ...
- C++中二维数组的动态创建与处理
C++中用new动态创建二维数组的格式一般是这样: TYPE (*p)[N] = new TYPE [][N]; 其中,TYPE是某种类型,N是二维数组的列数.采用这种格式,列数必须指出,而行数无需指 ...
- Sprint5
进展:今天开始进行了登录界面的编写及实现. 燃尽图: 工作照:
- bzoj 2852: 强大的区间 辗转相除
2852: 强大的区间 Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 45 Solved: 12[Submit][Status][Discuss] D ...
- bzoj 1914: [Usaco2010 OPen]Triangle Counting 数三角形 容斥
1914: [Usaco2010 OPen]Triangle Counting 数三角形 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 272 Sol ...
- Android判断界面
仿造微信,第一次进入去引导界面,否则进启动界面. package edu.hpu.init; import edu.hpu.logic.R; import android.app.Activ ...