Java and C# Solution - Beats 100% in C#


  • 0
    • Java
    public void rotate(int[][] matrix)
    {
        if (matrix == null) return;
    
        int low = 0;
        int high = matrix[0].length - 1;
    
        while (high>low)
        {
            for (int i = 0; i <= high-low-1; i++)
            {
                int temp1 = matrix[low + i][ high];
                matrix[low + i][ high] = matrix[low][ low + i];
    
                int temp2 = matrix[high][ high - i];
                matrix[high][ high - i] = temp1;
    
                temp1 = matrix[high - i][ low];
                matrix[high - i][ low] = temp2;
    
                matrix[low][ low + i] = temp1;
            }
    		
            low++;
            high--;
        }
    }
    
    • C#
    public static void Rotate(int[,] matrix)
    {
        if (matrix == null) return;
    
        int low = 0;
        int high = matrix.GetLength(0) - 1;
    
        while (high>low)
        {
            for (int i = 0; i <= high-low-1; i++)
            {
                int temp1 = matrix[low + i, high];
                matrix[low + i, high] = matrix[low, low + i];
    
                int temp2 = matrix[high, high - i];
                matrix[high, high - i] = temp1;
    
                temp1 = matrix[high - i, low];
                matrix[high - i, low] = temp2;
    
                matrix[low, low + i] = temp1;
            }
    
            low++;
            high--;
        }
    }
    

Log in to reply
 

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