图解算法——矩阵转换(Rotate Image)
2021-03-02 16:29
标签:循环 clock i++ 描述 转换 other height 分析 direct 1. 题目描述 You are given an n x n 2D You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation. 英文不好的同学不要慌,请接着看例子: 2. Examples 示例1: 示例2: 示例3: 示例4: 要求: 3、题目分析 从输入可以看出,矩阵是方阵即行列数相等。 故有两种思路: 【第一种】 是记录左上角和右下角的坐标,一层一层往里,循环交换,类似这样: 【第二种】 是先上下三角置换,再左右水平置换,类似于这样: 第一步: 第二步: 第二种再举一个例子吧: 4、代码实现 Over...... 图解算法——矩阵转换(Rotate Image) 标签:循环 clock i++ 描述 转换 other height 分析 direct 原文地址:https://www.cnblogs.com/gjmhome/p/14398598.htmlmatrix
representing an image, rotate the image by 90 degrees (clockwise).Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [[7,4,1],[8,5,2],[9,6,3]]
Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
Input: nums = [1]
Output: [[1]]
Input: matrix = [[1,2],[3,4]]
Output: [[3,1],[4,2]]
matrix.length == n;
matrix[i].length == n;
1
-1000
// 三行三列:
1 2 3 1 4 7 7 4 1
4 5 6 => 2 5 8 => 8 5 2
7 8 9 3 6 9 9 6 3
//四行四列
5 1 9 11 5 2 13 15 15 13 2 5
2 4 8 10 => 1 4 3 14 => 14 3 4 1
13 3 6 7 9 8 6 12 12 6 8 9
15 14 12 16 11 10 7 16 16 7 10 11
class Solution{
////思路一:转圈依次交换
public void rotateImage(int[][] matrix){
int i = 0;
int leftTop = 0;
int rightDown = matrix.length-1;
int temp=0;
while(leftToprightDown){
for(i=0; i
上一篇:JavaScript 取模与取余
文章标题:图解算法——矩阵转换(Rotate Image)
文章链接:http://soscw.com/index.php/essay/59114.html