Game development as an Art

Sato Game Dev

Code Exercises


Transpose Matrix


Source: leetcode

Given a matrix A, return the transpose of A.
The transpose of a matrix is the matrix flipped over it's main diagonal, switching the row and column indices of the matrix.

Example 1:

Input: [[1,2,3],[4,5,6],[7,8,9]]
Output: [[1,4,7],[2,5,8],[3,6,9]]

Example 2:

Input: [[1,2,3],[4,5,6]]
Output: [[1,4],[2,5],[3,6]]

Note:

1 <= A.length <= 1000
1 <= A[0].length <= 1000

My solution:
C#:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System;

namespace TransposeMatrix
{
    class Program
    {
        public static int[][] Transpose(int[][] matrix)
        {
            int[][] matrixTrans = new int[matrix.Length][];

            for (int i = 0;i < matrix.Length; i++)
            {
                //instanciate line
                matrixTrans[i] = new int[matrix.Length];
                for (int j = 0; j < matrix.Length; j++)
                {
                    matrixTrans[i][j]= matrix[j][i];
                }
            }
            return matrixTrans;
        }

        static void Main(string[] args)
        {
            //read first line
            int[] firstLine = Array.ConvertAll(Console.ReadLine().Split(' '), lineTemp => Convert.ToInt32(lineTemp));

            int numColumns = firstLine.Length;
            int[][] matrix = new int[numColumns][];
            matrix[0] = new int[numColumns];

            //copying firstLine to matrix first line
            for (int i = 0; i < numColumns; i++)
            {
                matrix[0][i] = firstLine[i];
            }

            //reading the rest of the matrix
            for (int i = 1; i < numColumns; i++)
            {
                matrix[i] = Array.ConvertAll(Console.ReadLine().Split(' '), lineTemp => Convert.ToInt32(lineTemp));
            }

            int[][] result = Transpose(matrix);

        }
    }
}

Java:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.util.Scanner;

public class Transpose {

	public static int[][] Transpose(int[][] matrix)
	{
		int[][] matrixTrans = new int[matrix.length][];
		
        for (int i = 0;i < matrix.length; i++)
        {
            //instanciate line
            matrixTrans[i] = new int[matrix.length];
            for (int j = 0; j < matrix.length; j++)
            {
                matrixTrans[i][j]= matrix[j][i];
            }
        }
        return matrixTrans;
	}
	
	public static void main(String[] args) {
		final Scanner scanner = new Scanner(System.in);
		
		String[] firstLine = scanner.nextLine().split(" ");
		
        int numColumns = firstLine.length;
        int[][] matrix = new int[numColumns][];
        matrix[0] = new int[numColumns];

        //copying firstLine to matrix first line
        for (int i = 0; i < numColumns; i++)
        {
            matrix[0][i] = Integer.parseInt(firstLine[i]);
        }
        
        //reading the rest of the matrix
        for (int i = 1; i < numColumns; i++)
        {
        	String[] otherLines = scanner.nextLine().split(" ");
            //copying firstLine to matrix first line
            for (int j = 0; i < numColumns; i++)
            {
                matrix[i][j] = Integer.parseInt(otherLines[j]);
            }
        }
        
        int[][] result = Transpose(matrix);
        

        
        
	}

}