一副由NxN矩阵表示的图像,将图像顺时针旋转90度。

package cglib;


/**
 *  有一副由NxN矩阵表示的图像,这里每个像素用一个int表示,请编写一个算法,
 *  在不占用额外内存空间的情况下(即不使用缓存矩阵),
 *  将图像顺时针旋转90度。
 *
 */

public class StringNumber {
     //顺时针
     public static int[][] rotate(int[][] matrix, int n) {
            for (int layer = 0; layer < n / 2; ++layer) {
                int first = layer;// 要处理的第一个元素
                int last = n - 1 - layer;// 最后一个元素,不用处理  
                for(int i = first; i < last; ++i) {
                    int offset = i - first;// offset: 当前处理元素和第一个元素之间之差,用于last元素向前  
                    int top = matrix[first][i]; // save top

                    // left -> top
                    matrix[first][i] = matrix[last-offset][first];          

                    // bottom -> left
                    matrix[last-offset][first] = matrix[last][last - offset];

                    // right -> bottom
                    matrix[last][last - offset] = matrix[i][last];

                    // top -> right
                    matrix[i][last] = top; // right <- saved top
                }
            }
            return matrix;
        }
    
    // 逆时针90度旋转矩阵  
       /* public static int[][] rotateAntiClockwise(int[][] M, int n) {  
            for(int layer=0; layer<n/2; layer++) {  
                int first = layer;  
                int last = n - 1 - layer;  
                for(int i=first; i<last; i++){  
                    int offset = i - first;  
                    int top = M[first][i];                                  // Save top  
                    M[first][i] = M[i][last];                               // right -> top   
                    M[i][last] = M[last][last-offset];                  // bottom -> right  
                    M[last][last-offset] = M[last-offset][first];   // left -> bottom  
                    M[last-offset][first] = top;                            // top->left  
                }  
            }
            return M;  
        } */

        public static void main(String[] args) {
            int[][] matrix =new int[][]{{1,2,3},{4,5,6},{7,8,9}} ;
            int[][] qw=rotate(matrix, 3);
           // int[][] qa=rotateAntiClockwise(matrix, 3);
            
            for (int i=0;i<qw.length ;i++ )
            {
            for (int j=0;j<qw[i].length;j++ )
            {
            System.out.println("顺时针:"+qw[i][j]+" ");
            }
            };
            
           /* for (int k=0;k<qa.length ;k++ )
            {
            for (int l=0;l<qa[k].length;l++ )
            {
            System.out.println("逆时针"+qa[k][l]+" ");
            }
            };*/
        }
    
}
      

 

转载于:https://my.oschina.net/u/2822116/blog/788125

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值