第一种方法:

先打印外圈,再打印内圈

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. echarts 更改tooltip提示框CSS样式

    最近 做项目,用过echarts,发现tooltip提示z-index级别很高,想更改下,看了下文档:https://www.echartsjs.com/zh/option.html#tooltip. ...

  2. WorkFlow三:配BO对象,事件触发工作流

    1.新建个BO对象的字段. 2.新建取数函数: 3.运行事物代码SWO1新建BO对象. 4.新建关键字段: 5.新建BO对象的事件: 6.添加处理方法: 6.调整对象状态,这里是本地对象,不需要释放, ...

  3. logger(二)logback简介及其实现原理

    一.logback简介 logback是log4j创始人写的,性能比log4j要好,目前主要分为3个模块 logback-core:核心代码模块 logback-classic:log4j的一个改良版 ...

  4. XGBoost 引入 - 提升树

    认识提升树 这个boosting 跟 Adaboost 不同. Adaboost 是通过上一轮的误差率来动态给定一下轮样本不同的权重来学习不同的模型. 现在的方式, 更多是基于残差 的方式来训练. 一 ...

  5. 并发编程(六)--进程/线程池、协程、gevent第三方库

    一.进程/线程池 1.进程池 (1)什么是进程池 如果需要创建的子进程数量不大,可以直接利用multiprocess中的Process来创建.但是当需要创建上百个或上千个,手动创建就较为繁琐,这时就可 ...

  6. vue中axios的简单使用

    我们一般在用jq的时候会使用到ajax来进行与服务器之间的交流,vue中也提供了相应的类似于ajax的方法-axios来进行与服务器之间的数据传递 现在的这篇是最简单的使用,后续会添加上来复杂的使用 ...

  7. python网页内容提取神器lxml

    一.Xpath是什么 XPath 是一门在 XML 文档中查找信息的语言.XPath 用于在 XML 文档中通过元素和属性进行导航. XPath 使用路径表达式在 XML 文档中进行导航 XPath ...

  8. robotframework-selenium2library各个版本

    https://github.com/robotframework/Selenium2Library/downloads

  9. 15-C#笔记-结构体

    示例: using System; using System.Text; struct Books { private string title; // 支持 public private strin ...

  10. 01-linux-基本语法-sh文件

    在一些开源库中,往往有一个 xxx.sh 的安装文件.命令“./ xxx.sh” 就可运行之. 其实内部是一些 linux 的语句.整合起来,方便使用而已. 介绍一下常用的一些Linux语句 cd b ...