第一种方法:

先打印外圈,再打印内圈

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详细步骤

    安装python分三个步骤: *下载python *安装python *检查是否安装成功 1.下载Python (1)python下载地址https://www.python.org/download ...

  2. SUCTF 2018——Anonymous(php匿名函数 \x00lambda_)

    题目地址:http://45.76.173.177:23334/ <?php $MY = create_function("","die(`cat flag.php ...

  3. Android.mk 使用说明

    Android.mk 详解https://blog.csdn.net/dearsq/article/details/50585537  Android.mk中的主要配置参数: 1.LOCAL_JACK ...

  4. 用jTessBoxEditorFX训练字库

    软件下载:https://sourceforge.net/projects/vietocr/files/jTessBoxEditor/ 官方字库下载:https://github.com/tesser ...

  5. docker的centos7安装与启动相关命令

    Docker 的概念 Docker 是一个开源工具,它可以让创建和管理 Linux 容器变得简单.容器就像是轻量级的虚拟机,并且可以以毫秒级的速度来启动或停止.Docker 帮助系统管理员和程序员在容 ...

  6. 【爬虫】网页抓包工具--Fiddler--Request和Response

    [爬虫]网页抓包工具--Fiddler Fiddler基础知识 Fiddler是强大的抓包工具,它的原理是以web代理服务器的形式进行工作的,使用的代理地址是:127.0.0.1,端口默认为8888, ...

  7. ORACLE ORION测试IO性能

    https://www.oracle.com/technetwork/cn/topics/index-088165-zhs.html 下载地址 Orion是Oracle提供的IO性能测试工具,运行该工 ...

  8. kibana自动创建索引

    一般索引按月.季或年为单位创建索引.我这里写成logstash-www-2019-03,www是URL的二级域名.格式类型完全根据自己方便就行. 当ELK集群中的索引过多时,我这里有100多个不同的日 ...

  9. 谷歌学术出现We're sorry解决办法

    出现这个的原因应该是同ip段的或者就是这个ip曾经是个google的黑名单ip,因为恶意爬取谷歌学术了.解决办法就是申请Hurricane Electric Free IPv6 Tunnel Brok ...

  10. c#引用c++dll和c++导出类出现的各种问题

    最近对一些第三方类库进行c++托管以便c#调用  因为之前没弄过,出现各种各样的问题 fatal error LNK1104: 无法打开文件“xxx.lib”或者xxx.dll 等等等 总结: 1.字 ...