N x N 的矩阵,顺时针旋转
第一种方法:
先打印外圈,再打印内圈
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 的矩阵,顺时针旋转的更多相关文章
- 将n*n矩阵顺时针旋转90度
/** * 将n*n矩阵顺时针旋转90度 * @param mat * @param n 矩阵的阶数 * @date 2016-10-7 * @author shaobn */ public stat ...
- python 矩阵顺时针旋转90度
# 4*4矩阵旋转90度 def matrix_transposition(data): for index,row in enumerate(data): for col in range(inde ...
- Rotate Image,N*N矩阵顺时针旋转90度
public class RotateImage { public void rotate(int[][] matrix) { if(matrix.length == 1 && mat ...
- [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). ...
- 计蒜客模拟赛D1T1 蒜头君打地鼠:矩阵旋转+二维前缀和
题目链接:https://nanti.jisuanke.com/t/16445 题意: 给你一个n*n大小的01矩阵,和一个k*k大小的锤子,锤子只能斜着砸,问只砸一次最多能砸到多少个1. 题解: 将 ...
- 【LeetCode】矩阵操作
1. 矩阵旋转 将 n × n 矩阵顺时针旋转 90°. 我的思路是 “ 从外到内一层一层旋转 ”. 一个 n × n 矩阵有 (n + 1) / 2 层,每层有 4 部分,将这 4 部分旋转. 顺时 ...
- 利用neon技术对矩阵旋转进行加速(2)
上次介绍的是顺时针旋转90度,最近用到了180度和270度,在这里记录一下. 1.利用neon技术将矩阵顺时针旋转180度: 顺时针旋转180度比顺时针旋转90度容易很多,如下图 A1 A2 A3 A ...
- 利用neon技术对矩阵旋转进行加速
一般的矩阵旋转操作都是对矩阵中的元素逐个操作,假设矩阵大小为m*n,那么时间复杂度就是o(mn).如果使用了arm公司提供的neon加速技术,则可以并行的读取多个元素,对多个元素进行操作,虽然时间复杂 ...
- Rotate Image(二位数组顺时针旋转)
问题描述: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockw ...
- OptimalSolution(5)--数组和矩阵问题(1)简单
一.转圈打印矩阵 题目:给定一个整型矩阵matrix,按照转圈的方式打印它. 要求:额外空间复杂度为O(1) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 打印结果为: ...
随机推荐
- 初探Tomcat的架构设计
Tomcat 作为 servlet 容器实现,它是基于 Java 语言开发的轻量级应用服务器.因为 Tomcat 作为应用服务器,它有着完全开源,轻量,性能稳定,部署成本低等优点,所以它成为目前 Ja ...
- 线程状态---Day24
线程状态概述: 当线程被创建并启动以后,它既不是一启动就进入了执行状态,也不是一直处于执行状态.在线程的生命周期中, 有几种状态呢?在API中 java.lang.Thread.State 这个枚举中 ...
- 10. Javascript 前后端数据加密
为了加强项目的接口安全程度,需求如下 var options = { // 前端需要传送的数据加密 data: { abc: 123, bcd: 123, cds: '撒旦教付货款12313', }, ...
- 实体类id的几种生成方式
@Id// @GeneratedValue(strategy = GenerationType.AUTO) // 自增// @GeneratedValue(strategy = GenerationT ...
- QQ空间自动点赞js脚本
这是很久前写的脚本了,在浏览器打开QQ空间,并在控制台输入代码就可 时间间隔最好开大点,不然容易被暂时冻结账号 function autoLike() { var list=document.getE ...
- linq to js 用法
/** * 排序汇总 * */ var result = Enumerable.From(vm.productList).GroupBy("$.goods_id", null, f ...
- .net web api 权限验证
做一个登录权限验证. 开始吧. using System; using System.Collections.Generic; using System.Drawing; using System.D ...
- 分布式CAP定理
根据百度百科的定义,CAP定理又称CAP原则,指的是在一个分布式系统中,Consistency(一致性). Availability(可用性).Partition tolerance(分区容错性),最 ...
- 对NetBackup 问题进行故障排除的步骤
错误消息通常是指出哪里出现故障的手段.如果在界面上没有看到错误消息,但仍怀疑有问题,请检查报告和日志. NetBackup提供了各种报告和日志记录工具, 这些工具可提供错误消息,直接为您指出解决方案. ...
- Codeforces J. Sagheer and Nubian Market(二分枚举)
题目描述: Sagheer and Nubian Market time limit per test 2 seconds memory limit per test 256 megabytes in ...