felixz2010
BAN USERmy algorithm is O(m*n), which m,n is the length of row and col.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RepZero
{
class Program
{
static List<List<int>> matrix = new List<List<int>>
{
new List<int> {1,2,3,4},
new List<int> {5,6,7,8},
new List<int> {9,10,0,12},
new List<int> {13,0,15,16}
};
static List<int> row = new List<int> { 1, 1, 1, 1 };
static List<int> col = new List<int> { 1, 1, 1, 1 };
static void Main(string[] args)
{
Console.WriteLine("row : ");
printList(row);
Console.WriteLine();
Console.WriteLine("col : ");
printList(col);
Console.WriteLine();
Console.WriteLine("matrix : ");
printMatrix(matrix);
Console.WriteLine();
Console.WriteLine("progress.");
findZero();
Console.WriteLine("row : ");
printList(row);
Console.WriteLine();
Console.WriteLine("col : ");
printList(col);
Console.WriteLine();
Console.WriteLine("replace Zero.");
replaceZero();
Console.WriteLine("matrix : ");
printMatrix(matrix);
Console.ReadKey();
}
static void findZero()
{
for (int r = 0; r < matrix.Count(); r++)
{
if (row[r] == 0) continue;
for (int c = 0; c < matrix[r].Count(); c++)
{
if (col[c] == 0) continue;
if (matrix[r][c] == 0)
{
row[r] = 0;
col[c] = 0;
}
}
}
}
static void replaceZero()
{
// update row
for (int r = 0; r < row.Count(); r++)
{
if (row[r] == 0)
{
for (int i = 0; i < matrix[r].Count(); i++)
{
matrix[r][i] = 0;
}
}
}
// update col
for (int c = 0; c < col.Count(); c++)
{
if (col[c] == 0)
{
for (int i = 0; i < matrix.Count(); i++)
{
matrix[i][c] = 0;
}
}
}
}
static void printList(List<int> list)
{
foreach (var item in list)
{
Console.Write(item + ",");
}
}
static void printMatrix(List<List<int>> matrix)
{
foreach (var row in matrix)
{
printList(row);
Console.WriteLine();
}
}
}
}
my algorithm is O(m*n), which m,n is the length of row and col.
- felixz2010 February 24, 2014