rotate image

[LeetCode] 48. Rotate Image 旋转图像

牧云@^-^@ 提交于 2020-12-12 02:39:26
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Note: 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. Example 1: Given input matrix = [ [1,2,3], [4,5,6], [7,8,9] ], rotate the input matrix in-place such that it becomes: [ [7,4,1], [8,5,2], [9,6,3] ] Example 2: Given input matrix = [ [ 5, 1, 9,11], [ 2, 4, 8,10], [13, 3, 6, 7], [15,14,12,16] ], rotate the input matrix in-place such that it becomes: [ [15,13, 2, 5], [14, 3, 4, 1], [12, 6,

48. Rotate Image

久未见 提交于 2020-12-12 02:36:27
Question 48. Rotate Image Solution 把这个二维数组(矩阵)看成一个一个环,循环每个环,循环每条边,每个边上的点进行旋转 public void rotate(int[][] matrix) { int n = matrix.length; for (int i = 0; i < n / 2; i++) { change(matrix, i); } } private void change(int[][] matrix, int i) { int n = matrix.length - 1; for (int j = i; j < n - i; j++) { int tmp = 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] = tmp; } } https://gitee.com/okokabcd/leetcode http://www.cnblogs.com/boring09/p/4248697.html 来源: oschina 链接: https://my.oschina.net/u/159293

leetcode_rotate-image

我是研究僧i 提交于 2020-12-11 02:02:26
题目链接 rotate-image 题目内容 给定一个 n × n 的二维矩阵表示一个图像。 将图像顺时针旋转 90 度。 说明 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。 示例 1 给定 matrix = [ [1,2,3], [4,5,6], [7,8,9] ], 原地旋转输入矩阵,使其变为: [ [7,4,1], [8,5,2], [9,6,3] ] 示例 2 给定 matrix = [ [ 5, 1, 9,11], [ 2, 4, 8,10], [13, 3, 6, 7], [15,14,12,16] ], 原地旋转输入矩阵,使其变为: [ [15,13, 2, 5], [14, 3, 4, 1], [12, 6, 8, 9], [16, 7,10,11] ] 解题思路 这是一道数学的巧思问题,具体解法如下图: 代码 class Solution { public : void rotate ( vector < vector < int >> & matrix ) { int n = matrix . size ( ) ; for ( int i = 0 ; i < ( n + 1 ) / 2 ; i ++ ) { for ( int j = 0 ; j < n / 2 ; j ++ ) { int temp =

leetcode48(旋转图像)--Java语言实现

五迷三道 提交于 2020-07-24 15:09:05
求: 给定一个 n × n 的二维矩阵表示一个图像。 将图像顺时针旋转 90 度。 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。 示例 1: 给定 matrix = [ [1,2,3], [4,5,6], [7,8,9] ], 原地旋转输入矩阵,使其变为: [ [7,4,1], [8,5,2], [9,6,3] ] 示例 2: 给定 matrix = [ [ 5, 1, 9,11], [ 2, 4, 8,10], [13, 3, 6, 7], [15,14,12,16] ], 原地旋转输入矩阵,使其变为: [ [15,13, 2, 5], [14, 3, 4, 1], [12, 6, 8, 9], [16, 7,10,11] ] 题目链接: https://leetcode-cn.com/problems/rotate-image/ 解: 这道题的关键是找到数组元素在旋转前后的下标对应关系,经过分析可以发现,位置为a[i][j]的元素,经过旋转,会出现在原来位置在a[j][N-1-i]的位置。其中N是矩阵的阶数,与矩阵的行数、列数均相等。(题目给的是N*N的方阵) 因此如果我们不考虑题目要求的原地旋转,很容易得到如下的一种实现: 时间复杂度: O(N^2) 空间复杂度: O(N^2) public void rotate (

【目录】LeetCode Java实现

久未见 提交于 2020-04-25 08:51:22
这里记录一下自己刷的LeetCode题目。 有些博客用英文阐述自己的思路和收获,相当于练习一下英文的表达能力。 比较好的题目有 加粗 。 题目以后将按题型分类,可以从目录寻找相关的分类。 数组 简单题 26. Remove Duplicates from Sorted Array 122. Best Time to Buy and Sell Stock II 189. Rotate Array 217. Contains Duplicate 136. Single Number (思想很好) 350. Intersection of Two Arrays II 66. Plus One 283. Move Zeroes 1. Two Sum 219. Contains Duplicate II 中等题 36. Valid Sudoku 48. Rotate Image 220. Contains Duplicate III 字符串 简单题 344. Reverse String 7. Reverse Integer 387. First Unique Character in a String 242. Valid Anagram 125. Valid Palindrome 28. Implement strStr() KMP 38. Count and Say 14.