zoukankan      html  css  js  c++  java
  • [算法] 二维数组(长宽相等)逆时针旋转90°算法

    【思路】要完成旋转共需两步

    1.第一次x,y互换

    s1[i][j]=s[j][i]
    1,   2,  3,  4
    5,   6,  7,  8
    9,  10,11,12
    13,14,15,16
    变成了
    1, 5, 9, 13
    2, 6,10,14
    3, 7,11,15
    4, 8,12,16

    然后x逆序

    s2[i][j]=s1[n-i][j]
    变成了
    4, 8,12,16
    3, 7,11,15
    2, 6,10,14
    1, 5, 9, 13
    旋转成功

     1 void swap(int& x,int& y)
     2 {
     3     int temp = x;
     4     x = y;
     5     y = temp;
     6 }
     7 
     8 void rotate(vector< vector<int> > &A)
     9 {
    10     //A[i][j] = A[j][i]
    11     for(int i = 0;i < A.size();i ++)
    12     {
    13         for(int j = i;j < A[0].size();j ++)
    14         {
    15             swap(A[i][j], A[j][i]);
    16         }
    17     }
    18 
    19     //A[i][j] = A[row - i][j]
    20     for(int j = 0;j < A[0].size();j ++)
    21     {
    22         for(int i = 0;i < A.size()/2;i ++)
    23         {
    24             swap(A[i][j], A[A.size() - 1 - i][j]);
    25         }
    26     }
    27 
    28 }
  • 相关阅读:
    idea database testconnection 显示灰色
    idea tomcat热部署
    idea 常见报错问题 记录
    Python-Basis-22th
    Python-Basis-21th
    Python-Basis-20th
    Python-Basis-19th
    Python-Basis-18th
    Python-Basis-17th
    Python-Basis-16th
  • 原文地址:https://www.cnblogs.com/lca1826/p/6476452.html
Copyright © 2011-2022 走看看