Exemplo n.º 1
0
        /// <summary>
        /// Finds all zero elements in a NxM matrix and zeroes out their columns and rows
        /// O(n) time and space complexity
        /// </summary>
        /// <param name="inputMatrix">int[,] matrix</param>
        /// <returns>int[,] matrix</returns>
        public static int[,] ZeroMatrix(int[,] inputMatrix)
        {
            if (inputMatrix.GetLength(0) == 0 || inputMatrix.GetLength(1) == 0)
            {
                return(inputMatrix);
            }

            List <MatrixPointer> zeroList = new List <MatrixPointer>();

            for (int row = 0; row < inputMatrix.GetLength(0); row++)
            {
                for (int col = 0; col < inputMatrix.GetLength(1); col++)
                {
                    if (inputMatrix[row, col] == 0)
                    {
                        MatrixPointer zeroPointer = new MatrixPointer(row, col);
                        zeroList.Add(zeroPointer);
                    }
                }
            }
            if (zeroList.Count == 0)
            {
                return(inputMatrix);
            }

            foreach (MatrixPointer pointer in zeroList)
            {
                int zeroRow = pointer.Row;
                int zeroCol = pointer.Col;

                int currRow = 0;
                while (currRow < inputMatrix.GetLength(0))
                {
                    if (inputMatrix[currRow, zeroCol] != 0)
                    {
                        inputMatrix[currRow, zeroCol] = 0;
                    }
                    currRow++;
                }

                int currCol = 0;
                while (currCol < inputMatrix.GetLength(1))
                {
                    if (inputMatrix[zeroRow, currCol] != 0)
                    {
                        inputMatrix[zeroRow, currCol] = 0;
                    }
                    currCol++;
                }
            }

            return(inputMatrix);
        }
Exemplo n.º 2
0
        private static void SwapPointers(int[,] inputMatrix, MatrixPointer p1, MatrixPointer p2, MatrixPointer p3, MatrixPointer p4)
        {
            int temp1, temp2;

            temp1 = inputMatrix[p2.Row, p2.Col];
            inputMatrix[p2.Row, p2.Col] = inputMatrix[p1.Row, p1.Col];
            temp2 = temp1;
            temp1 = inputMatrix[p3.Row, p3.Col];
            inputMatrix[p3.Row, p3.Col] = temp2;
            temp2 = temp1;
            temp1 = inputMatrix[p4.Row, p4.Col];
            inputMatrix[p4.Row, p4.Col] = temp2;
            temp2 = temp1;
            inputMatrix[p1.Row, p1.Col] = temp1;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Rotates matrix in place by 90 degrees.
        /// O(n) time complexity, O(n) space
        /// </summary>
        /// <param name="inputMatrix"></param>
        /// <returns></returns>
        public static int[,] RotateMatrix(int[,] inputMatrix)
        {
            Console.WriteLine(inputMatrix.GetLength(0));
            if (inputMatrix.Length == 0)
            {
                return(inputMatrix);
            }

            int layers = inputMatrix.GetLength(0) / 2;
            int min    = 0;
            int max    = inputMatrix.GetLength(0) - 1;

            while (layers > 0)
            {
                MatrixPointer p1 = new MatrixPointer(min, min);
                MatrixPointer p2 = new MatrixPointer(min, max);
                MatrixPointer p3 = new MatrixPointer(max, max);
                MatrixPointer p4 = new MatrixPointer(max, min);

                int i = min;
                while (i < max)
                {
                    SwapPointers(inputMatrix, p1, p2, p3, p4);
                    p1.Col++;
                    p2.Row++;
                    p3.Col--;
                    p4.Row--;
                    i++;
                }
                min++;
                max--;
                layers--;
            }

            return(inputMatrix);
        }