给定一个 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-旋转图像的更多相关文章

  1. python -- leetcode 刷题之路

    第一题 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], tar ...

  2. 使用Java+Kotlin双语言的LeetCode刷题之路(三)

    BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...

  3. 使用Java+Kotlin双语言的LeetCode刷题之路(二)

    BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...

  4. 使用Java+Kotlin双语言的LeetCode刷题之路(一)

    LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 1 两数之和 给定一个整数数 ...

  5. #leetcode刷题之路40-组合总和 II

    给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的每个数字在每个组合中只能使用一次.说 ...

  6. #leetcode刷题之路16-最接近的三数之和

    给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...

  7. #leetcode刷题之路13-罗马数字转整数

    罗马数字包含以下七种字符: I, V, X, L,C,D 和 M.字符 数值I 1V 5X 10L 50C 100D 500M 1000例如, 罗马数字 2 写做 II ,即为两个并列的 1.12 写 ...

  8. #leetcode刷题之路6- Z 字形变换

    将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列.比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下:L     C     I ...

  9. 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 ...

随机推荐

  1. CSS 样式初始化

    去除浏览器对html的附加样式,避免不同浏览器之间的样式差异,给前端开发提供统一的样式基础.附加样式: .clearfix - 清除浮动 .wordsBreak - 允许文本在任意位置的换行 .ell ...

  2. 【眼见为实】自己动手实践理解数据库READ UNCOMMITED && SERIALIZABLE

    目录 准备工作 ①准备测试表和测试数据 ②关闭数据库事务自动提交 ③设置InnoDB存储引擎隔离级别 [READ UNCOMMITTED] [READ UNCOMMITTED]能解决的问题 [READ ...

  3. 初探diskstats

    内核很多重要子系统均通过proc文件的方式,将自身的一些统计信息输出,方便最终用户查看各子系统的运行状态,这些统计信息被称为metrics. 直接查看metrics并不能获取到有用的信息,一般都是由特 ...

  4. C# winform 只运行一个应用程序

    应用程序只有一个实例,当启动一次时创建实例,当多次启用时激活当前实例. 创建一个单利管理类 using Microsoft.VisualBasic.ApplicationServices; publi ...

  5. 关于easyUI

    <input class="easyui-combobox" id="s_dataDicName" data-options="panelHei ...

  6. MDT概念说明

    转自:http://www.winos.cn/html/21/t-39621.html         http://hi.baidu.com/popweb/item/95ea6cf3aea966b5 ...

  7. Zabbix日常监控之lvs监控

    参考博文:http://blog.51cto.com/kaibinyuan/1711863 监控环境的搭建请参考:https://www.cnblogs.com/huangyanqi/p/918780 ...

  8. September 22nd 2017 Week 38th Friday

    If we believe that tomorrow will be better, we can bear a hardship today. 如果我们相信明天会更好,今天就能承受艰辛. If y ...

  9. Leuze BCL308i 使用方法整理

    1 硬件连接关系 1.1 接口盒 BCL308i一般选配MK308/MK348/MK358系列接口盒, 单独使用(不组成扫描集群)时需要连接3根线,分别为SERVICE .SW/PWR.HOST/BU ...

  10. codeforces 741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(启发式合并)

    codeforces 741D Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths 题意 给出一棵树,每条边上有一个字符,字符集大小只 ...