#leetcode刷题之路48-旋转图像
给定一个 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]
]
思路:
先按对角线对称翻转,再按中线翻转
#include <iostream>
#include <vector>
using namespace std;
void rotate(vector<vector<int>>& matrix) {
int len=matrix.size();
for(int i=;i<len;i++)
{
for(int j=i+;j<len;j++)
{
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
for(int i=; i<len; i++)
{
for(int j=; j<len/; j++)
{
int temp = matrix[i][j];
matrix[i][j] = matrix[i][len--j];
matrix[i][len--j] = temp;
}
}
} int main() {
vector<vector<int>> ans={{,,},{,,},{,,}};
rotate(ans);
std::cout << ans[][] << std::endl;
return ;
}
#leetcode刷题之路48-旋转图像的更多相关文章
- python -- leetcode 刷题之路
第一题 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], tar ...
- 使用Java+Kotlin双语言的LeetCode刷题之路(三)
BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...
- 使用Java+Kotlin双语言的LeetCode刷题之路(二)
BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...
- 使用Java+Kotlin双语言的LeetCode刷题之路(一)
LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 1 两数之和 给定一个整数数 ...
- #leetcode刷题之路40-组合总和 II
给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的每个数字在每个组合中只能使用一次.说 ...
- #leetcode刷题之路16-最接近的三数之和
给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...
- #leetcode刷题之路13-罗马数字转整数
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M.字符 数值I 1V 5X 10L 50C 100D 500M 1000例如, 罗马数字 2 写做 II ,即为两个并列的 1.12 写 ...
- #leetcode刷题之路6- Z 字形变换
将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列.比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下:L C I ...
- leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
随机推荐
- JavaScript写九九乘法表
<script language=javascript> for(i=1;i<=9;i++){ for(j=1;j<=9;j++){ document.write (i+&qu ...
- needPrint 不显示打印按钮
客户问题: 客户用的是needPrint 来显示打印按钮,现在访问不能显示后台提示有错误 打开控制台显示: java.security.AccessControlException: ...
- idea 忽略显示文件
最后填写的时候,参考原有参数,1.在末尾加分号2.形式为*.xxx
- leetcode 之 Degree of an Array
1.题目描述 Given a non-empty array of non-negative integers nums, the degree of this array is defined as ...
- AIX常用命令学习(一)
1.prtconf命令 查看AIX主机的结构特征状态 语法: prtconf [ -c ] [ -k ] [ -L ] [ -m ] [ -s ] [ -v ] Flags: -c Displays ...
- 干货:如何使用N点虚拟管理系统?
N点虚拟主机管理系统怎么用呢?最近有许多朋友问我关于这款虚拟主机管理系统如何使用?在讲如何使用N点虚拟主机管理系统之前,我们先来了解一下N点虚拟主机管理系统的介绍. N点虚拟主机管理系统 ...
- [IIS] IIS网站对文件读写无权限的解决方案(Access等)
Access数据库无写权限:在NTFS文件安全属性中加入对IIS_IUSRS用户的修改权限即可.
- 关于使用Entity Framework时遇到的问题 未找到具有固定名称“System.Data.SqlClient”的 ADO.NET 提供程序的实体框架提供程序。请确保在应用程序配置文件的“entityFramework”节中注册了该提供程序
问题描述: 使用Entity Framework获取数据时报以下错误: 未找到具有固定名称“System.Data.SqlClient”的 ADO.NET 提供程序的实体框架提供程序.请确保在应用程序 ...
- 获取索引--------用range()和len()
a = ['Google', 'Baidu', 'Runoob', 'Taobao', 'QQ'] for i in range(len(a)): print(i+1,a[i])
- PHP设计模式系列 - 建造者模式
什么是建造者模式 建造者模式主要是为了消除其它对象复杂的创建过程. 设计场景 有一个用户的UserInfo类,创建这个类,需要创建用户的姓名,年龄,金钱等信息,才能获得用户具体的信息结果. 创建一个U ...