Roate image by 90 degree in place
2014-11-01 05:43
225 查看
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?
public class Solution {
public void rotate(int[][] matrix) {
int n = matrix.length;
// let us trust that it is n by n
int start = 0, end = n-1;
while(start < end){
for(int i=start; i<end; i++){
int tmp = matrix[start][i];
matrix[start][i] = matrix[end+start-i][start];
matrix[end+start-i][start] = matrix[end][end+start-i];
matrix[end][end+start-i] = matrix[i][end];
matrix[i][end] = tmp;
}
start++;
end--;
}
}
}
easy problem.
time complexity is O(n^2)
space complexity is O(1)
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?
public class Solution {
public void rotate(int[][] matrix) {
int n = matrix.length;
// let us trust that it is n by n
int start = 0, end = n-1;
while(start < end){
for(int i=start; i<end; i++){
int tmp = matrix[start][i];
matrix[start][i] = matrix[end+start-i][start];
matrix[end+start-i][start] = matrix[end][end+start-i];
matrix[end][end+start-i] = matrix[i][end];
matrix[i][end] = tmp;
}
start++;
end--;
}
}
}
easy problem.
time complexity is O(n^2)
space complexity is O(1)
相关文章推荐
- Ch1.6: IN-place-ly rotate a pic(NxN matrix) by 90 digree in Verilog(CPP)
- convert in ImageMagick resize by percent
- Cloudera: Start Impala service by cloudera manager in docker quickstart image
- Alter by Table Rename 与 Alter Table in Place 的一些记录
- Cloudera: Start Impala service by cloudera manager in docker quickstart image
- How to load an ImageView by URL in Android
- Preview image in client by JavaScript when upload image in multi-browsers
- Step-by-step guide to installing the Automatic Image Slider w/ CSS & jQuery from Soh Tanaka in Zen Cart
- Q1.6 To rotate a image by 90 degress.
- Region Filling and Object Removal by Exemplar-Based Image Inpainting-code-2004(代码改进)
- Rotate Image------旋转图片(in-place版本)
- [leetcode]Rotate Image, Matrix tranposition, matrix rotation 90 degree @ Python
- How to Add/Drop/Resize Redo Log with Physical Standby in place. [ID 473442.1]
- How to Add/Drop/Resize Redo Log with Physical Standby in place. [ID 473442.1]
- Region Filling and Object Removal by Exemplar-Based Image Inpainting-code-2004
- Preview image in client by JavaScript when upload image in multi-browsers
- pdfrotate:rotates each page in a pdf file by a given angle(now support 90,180,270)
- Rotate a UIImage by 90 degree angles
- ORA-01652:unable to extend temp segment by num in tablespace name
- IOS 字符串中去除特殊符号 stringByTrimmingCharactersInSet 应该用于账号登录等