[LeetCode 48]Rotate Image

CSDN学院讲师招募,诚邀您加入!博客Markdown编辑器上线啦PMBOK第五版精讲视频教程火星人敏捷开发1001问

[LeetCode 48]Rotate Image

分类:leetcodejava

题目链接:rotate-image

矩阵原地旋转90度

import java.util.Arrays;/** * You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place? * */public class RotateImage {//20 / 20 test cases passed.//Status: Accepted//Runtime: 181 ms//Submitted: 1 minute agostatic void rotate(int[][] matrix) {int n = matrix.length – 1;for(int i = 0; i <= n / 2; i ++)for(int j = i; j < n – i; j ++) {//旋转四个位置相同的点//旋转前的位置关系//( i j) . . . ( j n-i)//..//..//..//(n-j i) . . . (n-i n-j)int temp1 = matrix[i][j];matrix[i][j] = matrix[n – j][i];matrix[n – j][i] = matrix[n – i][n – j];matrix[n – i][n – j] = matrix[j][n – i];matrix[j][n – i] = temp1;//旋转后的位置关系//(n-j i ) . . . ( i j )//..//..//..//(n-i n-j) . . . ( j n-i)}}public static void main(String[] args) {int[][] matrix = {{1,2}, {3, 4}};rotate(matrix);for(int i = 0; i < matrix.length; i ++)System.out.println(Arrays.toString(matrix[i]));}}

上一篇[LeetCode 63]Unique Paths II下一篇[LeetCode 54]Spiral Matrix

顶0踩0

主题推荐猜你在找

查看评论

* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

核心技术类目

,既有美妙的风景,也会有称不上景只有风的地方。

[LeetCode 48]Rotate Image

相关文章:

你感兴趣的文章:

标签云: