第一种方法:

先打印外圈,再打印内圈

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. 洛谷 P1002过河卒

    洛谷 P1002过河卒 题目描述 棋盘上AA点有一个过河卒,需要走到目标BB点.卒行走的规则:可以向下.或者向右.同时在棋盘上CC点有一个对方的马,该马所在的点和所有跳跃一步可达的点称为对方马的控制点 ...

  2. linux sed、awk、grep同时匹配多个条(并且 or 或者)

    同时匹配ABC 和 abc: sed -n '/ABC/{/abc/p}' awk '/ABC/&&/abc/{ print $0 }' grep -E '(ABC.*abc|abc. ...

  3. pandas基础:Series与DataFrame操作

    pandas包 # 引入包 import pandas as pd import numpy as np import matplotlib.pyplot as plt Series Series 是 ...

  4. 微信小程序开发--常用开发实例

    一.常用商品列表的换行排布 <view class="box_max"> <view class="box_min">限时秒杀</ ...

  5. 集成学习-Majority Voting

    认识 集成学习(Ensemble Methods), 首先是一种思想, 而非某种模型, 是一种 "群体决策" 的思想, 即对某一特定问题, 用多个模型来进行训练. 像常见的单个模型 ...

  6. 资深P7架构师详解淘宝服务端高并发分布式架构演进之路

    1. 概述 本文以淘宝作为例子,介绍从一百个并发到千万级并发情况下服务端的架构的演进过程,同时列举出每个演进阶段会遇到的相关技术,让大家对架构的演进有一个整体的认知,文章最后汇总了一些架构设计的原则. ...

  7. C 语言实现回调函数

    优点 不需要改变调用的主函数,只需添加命令和相应函数. #include "stdio.h" #include "stdlib.h" #include &quo ...

  8. PHP7.2.6安装sodium扩展

    安装libsodium libsodium是安装sodium扩展的必须依赖条件,我这里提供两种安装方式,编译和直接yum 编译安装libsodium wget https://github.com/j ...

  9. java国际化之时区问题处理

    原文:https://moon-walker.iteye.com/blog/2396035 在国际化的项目中需要处理的日期时间问题主要有两点: 1.日期时间的国际化格式问题处理: 2.日期时间的时区问 ...

  10. 1-剑指offer: 数组中出现次数超过一半的数字

    题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2. ...