leetcode_48题——Rotate Image(矩阵计算)

Rotate Image

Total Accepted: 36552 Total Submissions: 114426My Submissions

Question Solution

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?

Hide Tags

Array

Have you met this question in a re

这道题将矩阵先转置,再将每一行进行旋转就可以了

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;

void rotate(vector<vector<int>>& matrix) {
	int n=matrix.size();
	for(int i=0;i<n;i++)
		for(int j=i;j<n;j++)
		{int temp;temp=matrix[j][i];matrix[j][i]=matrix[i][j];matrix[i][j]=temp;}

		for(int i=0;i<n;i++)
			reverse(matrix[i].begin(),matrix[i].end());
}
int main()
{

}

  

时间: 2024-11-05 20:12:30

leetcode_48题——Rotate Image(矩阵计算)的相关文章

记一道有意思的算法题Rotate Image(旋转图像)

题出自https://leetcode.com/problems/rotate-image/ 内容为: 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度顺时针旋转,而且不能使用额外的存储空间. 最初拿到这道题

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

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

第22题 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. Solution: /** * Definition for singly-linked list. * public class

poj 3070 Fibonacci 矩阵快速幂

题目链接:http://poj.org/problem?id=3070 In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, … An alternative formula for t

hdu 4998 Rotate 点的旋转 银牌题

Rotate Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1232    Accepted Submission(s): 545Special Judge Problem Description Noting is more interesting than rotation! Your little sister likes to

【leetcode刷题笔记】Rotate Image

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? 题解: 如果要原地算法的话,就只能用交换实现了.首先把matrix[i][j]和matrix[j][i]交换,然后把整个矩阵左右翻转,如下图所示: 1 2 3                   1 4 7        

LeetCode第[48]题(Java):Rotate Image

题目:矩阵旋转 难度:Medium 题目内容: 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 anot

Codeforces 523A - Rotate, Flip and Zoom 手速题

1 #include "bits/stdc++.h" 2 using namespace std; 3 char mat[110][110]; 4 int m, n; 5 6 int main() 7 { 8 scanf("%d%d", &m, &n); 9 int i, j; 10 for(i = 0; i <= n - 1; ++i) 11 scanf("%s", mat[i]); 12 for(i = 0; i <

HDU4998 Rotate (2014年鞍山赛区网络赛B题)

1.题目描述:点击打开链接 2.解题思路:本题属于几何变换专题,一开始想着随便枚举两个点,然后都进行一下旋转变换,最后利用原始点和旋转后的点所在直线的中垂线的交点求解.然而发现精度损失很大,而且可能有特殊情况没有考虑到.学习了一下几何变换的方法. 由于旋转操作相当于对一个点构成的矩阵和一个旋转矩阵做乘法运算.最基本的旋转变换就是任意一个点围绕原点进行逆时针旋转.如果改成围绕某个定点(x0,y0)进行旋转可以分解为三步:将被旋转点平移(-x0,-y0),进行基本旋转变换,再平移(x0,y0).在矩