Leetcode 细节实现 Rotate Image

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie

Rotate Image

Total Accepted: 15609 Total
Submissions: 49679My Submissions

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?

题意:给定一个 n * n 的二维图像,将该图像顺时针旋转 90 度

思路:

先沿副对角线翻转一次,再沿水平中线翻转一次

复杂度:时间O(n^2),空间O(1)

void rotate(vector<vector<int> > &matrix){
	int n = matrix.size();
	//沿副对角线翻转
	for(int i = 0; i < n; ++i){
		for(int j = 0; j < n  - i; ++j){
			int i2 = n - 1 - j, j2 = n - 1 - i;
			swap(matrix[i][j], matrix[i2][j2]);
		}
	}
	//沿水平中线翻转
	for(int i = 0; i < n/2; ++i){
		swap(matrix[i], matrix[n - i - 1]);
	}
}
时间: 2024-11-04 08:17:07

Leetcode 细节实现 Rotate Image的相关文章

LeetCode:(Array-189) Rotate Array

Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note:Try to come up as many solutions as you can, there are at least 3 different ways to s

&lt;LeetCode OJ&gt; 48. Rotate Image

Total Accepted: 69879 Total Submissions: 199786 Difficulty: Medium 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? Subscribe to see which companies asked this

LeetCode OJ 61. Rotate List 考虑边界条件

题目链接:https://leetcode.com/problems/rotate-list/ 61. Rotate List My Submissions QuestionEditorial Solution Total Accepted: 71917 Total Submissions: 311425 Difficulty: Medium Given a list, rotate the list to the right by k places, where k is non-negati

【一天一道LeetCode】#48. Rotate Image

一天一道LeetCode系列 (一)题目 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? (二)解题 90度旋转图像,我们不难看出 matrix[i][j]=tmp[j][n?i?1]注:tmp=matrix 经过这样的变换后,图像就旋转了90度. class Solu

【一天一道LeetCode】#61. Rotate List

一天一道LeetCode系列 (一)题目 Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. (二)解题 本题的思路: 1.找到倒数第K个节点 2.将k以后的节点移动到前面来,与头结点

LeetCode算法题-Rotate String(Java实现)

这是悦乐书的第317次更新,第338篇原创 在开始今天的算法题前,说几句,今天是世界读书日,推荐两本书给大家,<终身成长>和<禅与摩托车维修艺术>,值得好好阅读和反复阅读. 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第186题(顺位题号是796).给定两个字符串A和B,在A上进行移位操作,规则是将A最左边的字符移动到最右边去.例如,如果A ='abcde',那么在A上移位一次后,它将是'bcdea'.当且仅当A在A上移位一定次数后可以变为B时返回True.

【LeetCode】189. Rotate Array 解题小结

题目: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. 这道题目 有很多种解法,我用的是这种,相对也好理解,先所有元素reverse,再0-(n-k)的元素reverse,然后(n-k)-n的元素reverse. class Solution { pub

【LeetCode】048. Rotate Image

题目: 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

LeetCode OJ 61. Rotate List

Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL. Subscribe to see which companies asked this question 解答: 不是很懂这道题的通过率