Transpose matrix in Java
public int[][] transpose(int[][] m) {
int[][] ans = new int[m[0].length][m.length];
for (int rows = 0; rows < m.length; rows++) {
for (int cols = 0; cols < m[0].length; cols++) {
ans[cols][rows] = m[rows][cols];
}
}
return ans;
}
Written by Bruno Candido Volpato da Cunha
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Java
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#