第一种方法:

先打印外圈,再打印内圈

public class RotateMatrix1 {

    public static void rotate(int[][] matrix) {
int tR = ;
int tC = ;
int bR = matrix.length - ;
int bC = matrix[].length - ;
while (tR < bR) {
rotateEdge(matrix, tR++, tC++, bR--, bC--);
}
} public static void rotateEdge(int[][] m, int a, int b, int c, int d) {
int times = d - b;
int tmp = ;
for (int i = ; i != times; i++) {
tmp = m[a][b + i];
m[a][b + i] = m[c - i][b];
m[c - i][b] = m[c][d - i];
m[c][d - i] = m[a + i][d];
m[a + i][d] = tmp;
}
} public static void printMatrix(int[][] matrix) {
for (int i = ; i != matrix.length; i++) {
for (int j = ; j != matrix[].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
} public static void main(String[] args) {
int[][] matrix = { { , , , },
{ , , , },
{ , , , },
{ , , , } };
printMatrix(matrix);
rotate(matrix);
System.out.println("********************");
printMatrix(matrix); }
}

第二种方法:

按照对角线交换后,再交换列

public class RotateMatrix2 {

    //主对角线不变,主对角线对称的点互换位置
public static void symmetry(int[][] matrix) {
for (int i = ; i < matrix.length; i++) {
for (int j = ; j < i; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
} //总体的效果是交换整列,假如总共有4列,那么第0列和第3列交换,第1列和第2列交换,
//实现过程是可以一行一行交,第0行交换完了,再交换下一行
public static void swapCol(int [][] matrix) {
for(int i = ; i<matrix.length; i++) {
for(int j = ; j<matrix.length/; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[i][matrix[].length - - j];
matrix[i][matrix[].length - - j] = temp;
}
}
} public static void main(String[] args) {
int[][] matrix = { { , , , },
{ , , , },
{ , , , },
{ , , , } };
printMatrix(matrix);
symmetry(matrix);
System.out.println("********************");
printMatrix(matrix);
swapCol(matrix);
System.out.println("********************");
printMatrix(matrix);
} public static void printMatrix(int[][] matrix) {
for (int i = ; i != matrix.length; i++) {
for (int j = ; j != matrix[].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}

N x N 的矩阵,顺时针旋转的更多相关文章

  1. 将n*n矩阵顺时针旋转90度

    /** * 将n*n矩阵顺时针旋转90度 * @param mat * @param n 矩阵的阶数 * @date 2016-10-7 * @author shaobn */ public stat ...

  2. python 矩阵顺时针旋转90度

    # 4*4矩阵旋转90度 def matrix_transposition(data): for index,row in enumerate(data): for col in range(inde ...

  3. Rotate Image,N*N矩阵顺时针旋转90度

    public class RotateImage { public void rotate(int[][] matrix) { if(matrix.length == 1 && mat ...

  4. [LeetCode] Rotate Image n-by-n矩阵顺时针旋转

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  5. 计蒜客模拟赛D1T1 蒜头君打地鼠:矩阵旋转+二维前缀和

    题目链接:https://nanti.jisuanke.com/t/16445 题意: 给你一个n*n大小的01矩阵,和一个k*k大小的锤子,锤子只能斜着砸,问只砸一次最多能砸到多少个1. 题解: 将 ...

  6. 【LeetCode】矩阵操作

    1. 矩阵旋转 将 n × n 矩阵顺时针旋转 90°. 我的思路是 “ 从外到内一层一层旋转 ”. 一个 n × n 矩阵有 (n + 1) / 2 层,每层有 4 部分,将这 4 部分旋转. 顺时 ...

  7. 利用neon技术对矩阵旋转进行加速(2)

    上次介绍的是顺时针旋转90度,最近用到了180度和270度,在这里记录一下. 1.利用neon技术将矩阵顺时针旋转180度: 顺时针旋转180度比顺时针旋转90度容易很多,如下图 A1 A2 A3 A ...

  8. 利用neon技术对矩阵旋转进行加速

    一般的矩阵旋转操作都是对矩阵中的元素逐个操作,假设矩阵大小为m*n,那么时间复杂度就是o(mn).如果使用了arm公司提供的neon加速技术,则可以并行的读取多个元素,对多个元素进行操作,虽然时间复杂 ...

  9. Rotate Image(二位数组顺时针旋转)

    问题描述: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockw ...

  10. OptimalSolution(5)--数组和矩阵问题(1)简单

    一.转圈打印矩阵 题目:给定一个整型矩阵matrix,按照转圈的方式打印它. 要求:额外空间复杂度为O(1) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 打印结果为: ...

随机推荐

  1. Python学习笔记之测试函数

    11-1 城市和国家:编写一个函数,它接受两个形参:一个城市名和一个国家名.这个函数返回一个格式为City, Country 的字符串,如Santiago, Chile.将这个函数存储在一个名为cit ...

  2. C# 构造基础返回值类型-BaseResponse

    学无止境,精益求精 十年河东,十年河西,莫欺少年穷 用于基础返回值类型,如下: using System; using System.Collections.Generic; using System ...

  3. docker启动,重启,关闭命令

    docker启动命令,docker重启命令,docker关闭命令 启动        systemctl start docker守护进程重启   sudo systemctl daemon-relo ...

  4. 本地eyoucms搬家

    1.后台数据备份 2.删除install 里面的install.lock 3.清理缓存文件 data - runtime-删除所有文件: 4.项目中的文件全部压缩 即打包完毕:最后再把打包的文件放置到 ...

  5. uni-app插件ColorUI步骤条

    1. uni-app插件ColorUI步骤条 1.1. 前言 uni-app就不介绍了,前面几篇已经有所介绍,不知道的可以翻看我前面几篇博客 ColorUI-uniApp是uni-app的一款ui组件 ...

  6. Java程序员的魔法杖-Arthas 3.1.2版本发布了

    Arthas已经成为我日常运维.线上排查的必备之品,听说最近更新版本了,今天这篇文章看下又增加了什么新的能力. Arthas是Alibaba开源的Java诊断工具,深受开发者喜爱. Github:ht ...

  7. php超时时间说明【转】

    一,http请求超时时间 可能出现的场景: 1,curl进程运行了一个世纪还木结束,curl的时候设置了超时时间 --connect-timeout 1000 2,operation timed ou ...

  8. windows,linux里的hosts文件

    在解析主机名的IP地址时,会先访问本机的上hosts文件,这样先配置好就可以不通过DNS服务器就获得IP地址. linux vi /etc/hosts IP 空格  主机名 windows C:\Wi ...

  9. SpringBoot整合JavaWeb

    一.SpringBoot整合Servlet的两种方式 1.通过注解扫描完成Servlet组件的注册 编写Servlet package com.example.demo.servlet; import ...

  10. asp.net Server.Transfer

    页面跳转传参. 如果不是通用的跳转可以通过,在原始页面定义对象保存数据 跳转的目标页面可以: SourcePage page=(SourcePage)Context.Handler; //获取源页面的 ...