当前位置:网站首页>[数组]BM99 顺时针旋转矩阵-简单
[数组]BM99 顺时针旋转矩阵-简单
2022-06-27 18:20:00 【51CTO】
描述
有一个nxn整数矩阵,请编写一个算法,将矩阵顺时针旋转90度。
给定一个nxn的矩阵,和矩阵的阶数n,请返回旋转后的nxn矩阵。
数据范围:,矩阵中的值满足
要求:空间复杂度 ,时间复杂度
进阶:空间复杂度
,时间复杂度
示例1
输入:
复制返回值:
题解
按层旋转
思路:
对于一个N阶矩阵的旋转,我们可以通过先旋转一个N-1矩阵,然后再对最外围的一圈进行旋转后得到。因此,题目实际上就是要实现一个算法,将任意阶矩阵的最外围进行旋转。要旋转最外围的一圈,我们可以分别将(0,1),(0,2)...(0,n-1)这些点进行旋转完成,每一步的旋转,只需要进行3次数据交换,假设矩阵阶数是N,则对任意的i >=0且i<=n-1的点(0,i)进行旋转,其交换顺序如下:
- swap(mat[0][i],mat[i][n-1])
- swap(mat[0][i],mat[n-1][n-i-1])
- swap(mat[0][i],mat[n-i-1][0])
代码如下:
using
namespace
std;
struct
point
{
int
x;
int
y;
point()
=
default;
point(
const
point
&
p) :
x(
p.
x),
y(
p.
y) {}
point(
int
x,
int
y) :
x(
x),
y(
y) {}
};
void
rotate_level(
vector
<
vector
<
int
>>
&
mat,
point
left,
point
right)
{
for (
int
i
=
left.
x;
i
<=
right.
x
-
1;
++
i)
{
std::swap(
mat[
left.
y][
i],
mat[
left.
y
+ (
i
-
left.
x)][
right.
x]);
std::swap(
mat[
left.
y][
i],
mat[
right.
y][
right.
x
- (
i
-
left.
x)]);
std::swap(
mat[
left.
y][
i],
mat[
right.
y
- (
i
-
left.
x)][
left.
x]);
}
}
vector
<
vector
<
int
>>
rotateMatrix(
vector
<
vector
<
int
>>
mat,
int
n)
{
point
left(
0,
0);
point
right(
n
-
1,
n
-
1);
while (
left.
x
<
right.
x)
{
rotate_level(
mat,
left,
right);
left.
x
+=
1;
left.
y
+=
1;
right.
x
-=
1;
right.
y
-=
1;
}
return
mat;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
根据矩阵的特性,先交换上下矩阵,然后再对每行进行反转
步骤:
- 交换上下矩阵
- 对每行进行反转
代码如下:
vector
<
vector
<
int
>>
rotateMatrix(
vector
<
vector
<
int
>>
mat,
int
n)
{
//矩阵转置
for (
int
i
=
0;
i
<
n;
i
++)
{
for (
int
j
=
0;
j
<
i;
j
++)
{
swap(
mat[
i][
j],
mat[
j][
i]);
//交换上三角与下三角对应的元素
}
}
//每行翻转
for (
int
i
=
0;
i
<
n;
i
++)
{
reverse(
mat[
i].
begin(),
mat[
i].
end());
}
return
mat;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
边栏推荐
- Postman Chinese tutorial (postman Chinese version)
- BAIC makes a brand new pickup truck, which is safe and comfortable
- muduo
- Embracing cloud Nativity: Practice of Jiangsu Mobile order center
- 使用MySqlBulkLoader批量插入数据
- mime.type文件内容
- Is it safe to buy stocks online and open an account?
- Mobile low code development theme month | visual development one click generation of professional source code
- 1023 Have Fun with Numbers
- 1028 List Sorting
猜你喜欢

UE4实现长按功能

Type the URL to the web page display. What happened during this period?

数智化进入“深水区”,数据治理是关键

Redis持久化

SQL Server - window function - solve the problem of filtering consecutive n records

SQL审核平台权限模块介绍和账号创建教程

Database index

BAIC makes a brand new pickup truck, which is safe and comfortable

Linux系统ORACLE 19C OEM监控管理

Crontab's learning essays
随机推荐
qt中文乱码
Redis cluster
Binary tree related problems 2
1025 PAT Ranking
数据仓库体系之贴源层、历史层
【bug】上传图片出现错误(413 Request Entity Too Large)
SQL Server - window function - solve the problem of filtering consecutive n records
Safety is the last word, Volvo xc40 recharge
网络上开户买股票是否安全呢?刚接触股票,不懂求指导
二叉树相关问题2
PyCharm常用功能 - 断点调试
Wechat IOS version 8.0.24 update release cache subdivision cleaning Online
【debug】平台工程接口调试
云原生安全指南: 从零开始学 Kubernetes 攻防
Linux system Oracle 19C OEM monitoring management
MySQL表的增删改查(基础)
ABAP essays - interview memories hope that everyone's demand will not increase and the number of people will soar
Source code analysis of golang map concurrent read / write problem
Common shell script commands (4)
券商经理的开户二维码开户买股票安全吗?有谁知道啊