【7】Rotate matrix by 90 degrees
2013-04-13 15:18
423 查看
Question:Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do
this in place?
package CareerCup;
public class RotateMatrix
{
public RotateMatrix(){}
public int[][] rotate(int matrix[][],int max)
{
int[][] result = new int[max][max];
for(int i=0;i<max;i++)
{
for(int j=0;j<max;j++)
result[i][j] = matrix[max-1-j][i];
}
return result;
}
public void print(int matrix[][],int max)
{
for(int i=0;i<max;i++)
{
for(int j=0;j<max;j++)
{
System.out.print(matrix[i][j]+" ");
}
System.out.println();
}
}
public static void main(String[] args)
{
int[][] matrix = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
int max = 4;
RotateMatrix rm = new RotateMatrix();
int[][] result = rm.rotate(matrix,max);
System.out.println("The orignal matrix:");
rm.print(matrix, max);
System.out.println("The rotated matrix:");
rm.print(result, max);
}
}
this in place?
package CareerCup;
public class RotateMatrix
{
public RotateMatrix(){}
public int[][] rotate(int matrix[][],int max)
{
int[][] result = new int[max][max];
for(int i=0;i<max;i++)
{
for(int j=0;j<max;j++)
result[i][j] = matrix[max-1-j][i];
}
return result;
}
public void print(int matrix[][],int max)
{
for(int i=0;i<max;i++)
{
for(int j=0;j<max;j++)
{
System.out.print(matrix[i][j]+" ");
}
System.out.println();
}
}
public static void main(String[] args)
{
int[][] matrix = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
int max = 4;
RotateMatrix rm = new RotateMatrix();
int[][] result = rm.rotate(matrix,max);
System.out.println("The orignal matrix:");
rm.print(matrix, max);
System.out.println("The rotated matrix:");
rm.print(result, max);
}
}
相关文章推荐
- Why are EPS files generated by gnuplot rotated 90 degrees?
- Ch1.6: IN-place-ly rotate a pic(NxN matrix) by 90 digree in Verilog(CPP)
- Rotate Matrix by One
- Q1.6 To rotate a image by 90 degress.
- Rotate a UIImage by 90 degree angles
- pdfrotate:rotates each page in a pdf file by a given angle(now support 90,180,270)
- [leetcode]Rotate Image, Matrix tranposition, matrix rotation 90 degree @ Python
- Rotate bitmap by real angle
- leetcode 90: Search a 2D Matrix
- Step by Step WebMatrix网站开发之二:使用WebMatrix(2)
- Implement a undirected graph,represent by a adjacent matrix
- android Matrix.setRotate 和 postRotate的区别
- Matrix学习3、Matrix的基本三种变换之Translation、Skew、Rotate
- Array(6) -- Maximum Subarray,Rotate Image,Unique Paths II,Spiral Matrix I, II
- leetcode 90: Search a 2D Matrix
- LeetCode-48-Rotate Image 矩阵旋转90
- Step by Step WebMatrix网站开发之二:使用WebMatrix(2)
- Rotate using Flex BitmapData and Matrix
- [Leetcode] Rotate Matrix
- android Matrix.setRotate 和 postRotate的区别