Very clean Accepted Code


  • 1
        public int[,] ImageSmoother(int[,] M)
        {
            int m = M.GetLength(0);
            int n = M.GetLength(1);
    
            int[,] Avg = new int[m, n];
    
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    int startI = (i - 1 >= 0) ? i - 1 : i;
                    int endI = (i + 1 < m) ? i + 1 : i;
                    int startJ = (j - 1 >= 0) ? j - 1 : j;
                    int endJ = (j + 1 < n) ? j + 1 : j;
    
                    Avg[i, j] = FindAverage(M, startI, endI, startJ, endJ);
                }
            }
            return Avg;
        }
        private int FindAverage(int[,] m, int startI, int endI, int startJ, int endJ)
        {
            int count = 0;
            int sum = 0;
            for (int i = startI; i <= endI; i++)
            {
                for (int j = startJ; j <= endJ; j++)
                {
                    sum += m[i, j];
                    count++;
                }
            }
            return sum / count;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.