Leetcode算法题目:Rotate Image
来源:互联网 发布:闪光灯软件哪个好 编辑:程序博客网 时间:2024/05/21 00:34
题目:
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度的目的。
同样我们可以先将图片水平翻转,然后再沿着主对角线翻转,同样可以达到目的,此处代码不予实现。
代码:
class Solution {public: 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++) swap(matrix[i][j],matrix[n-j-1][n-i-1]); } for (int i=0;i<n/2;++i){//水平线 for(int j=0;j<n;j++) swap(matrix[i][j],matrix[n-i-1][j]); } }};
阅读全文
1 0
- Leetcode算法题目:Rotate Image
- LeetCode算法题目:Rotate List
- LeetCode算法题之Rotate Image
- 48. Rotate Image ---leetcode算法笔记
- Leetcode算法学习日志-48 Rotate Image
- 【LeetCode】48 Rotate Image && 【九度】题目1164:旋转矩阵
- 【leetcode】第48题 Rotate Image 题目+解析+代码
- LeetCode: Rotate Image
- [Leetcode] Rotate Image
- LeetCode : Rotate Image
- [LeetCode] Rotate Image
- leetcode 67: Rotate Image
- [leetcode] Rotate Image
- [Leetcode] Rotate Image
- [LeetCode]Rotate Image
- LeetCode-Rotate Image
- [leetcode] Rotate Image
- LeetCode - Rotate Image
- 10年跳槽经验总结 高级人才不用找工作
- Strust2学习之Struts运行原理
- Java中数组复制的四种方法
- 杂谈 | 关于二维码
- Android开发之常用的第三方框架
- Leetcode算法题目:Rotate Image
- intellij Idea热部署失效问题解决方案
- Restful接口实现方法
- WebService开发方法介绍
- 数字分隔(二)
- 创建一个定时作业进行数据库备份
- Linux下mysql配置慢日志查询,把查询慢的sql记录下来
- 跳台阶
- 用GDB调试程序(二)