48-Rotate Image
生活随笔
收集整理的這篇文章主要介紹了
48-Rotate Image
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
【題目】
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?
【analyze】
1.先對角線旋轉
2.再左右對調
【算法】
public class Solution {public void rotate(int[][] matrix) {int r=matrix.length;int c=matrix[0].length;//按主對角線翻轉矩陣for(int i=0;i<r-1;i++) {for(int j=1+i;j<c;j++) {int temp=matrix[i][j];matrix[i][j]=matrix[j][i];matrix[j][i]=temp;}}//左右對調for(int i=0;i<r;i++) {for(int j=0;j<c/2;j++) {int temp=matrix[i][j];matrix[i][j]=matrix[i][c-1-j];matrix[i][c-1-j]=temp;}}} }?
轉載于:https://www.cnblogs.com/hwu2014/p/4482038.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的48-Rotate Image的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《黑马程序员》认识OC的第一个程序(Ob
- 下一篇: Razor练习2