【C++】【LeetCode】48. Rotate Image
来源:互联网 发布:sap数据归档 编辑:程序博客网 时间:2024/05/21 09:57
题目
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?
思路
顺时针旋转一个二维矩阵。通过在纸上演算推出,其实就是把原来坐标为(i, j)的点中的值移到(j, size - i - 1)的点上。但是题目要求少用空间。所以我们在原来的矩阵中移动点,通过将一个矩阵看成多个套层的正方形,每次移动一个点之后,就继续移动受到影响的那个点,经过3次移动,即可将受影响的四个点全部换位完成。
代码
class Solution {public: void rotate(vector<vector<int>>& matrix) { int size = matrix.size(); for (int i = 0; i < size - 1; i++) { for (int j = i; j < size - i - 1; j++) { int ii = j; int jj = size - i - 1; for (int k = 0; k < 3; k ++) { swap(matrix[i][j], matrix[ii][jj]); int ix = ii; ii = jj; jj = size - ix - 1; } } } }};
阅读全文
0 0
- 【C++】【LeetCode】48. Rotate Image
- [LeetCode] [C] 48. Rotate Image
- [LeetCode]48.Rotate Image
- LeetCode 48. Rotate Image
- LeetCode 48.Rotate Image
- LeetCode --- 48. Rotate Image
- [Leetcode] 48. Rotate Image
- [leetcode] 48. Rotate Image
- Leetcode 48. Rotate Image
- 48. Rotate Image LeetCode
- [LeetCode]48. Rotate Image
- leetcode 48. Rotate Image
- Leetcode 48. Rotate Image
- LeetCode 48. Rotate Image
- LeetCode *** 48. Rotate Image
- [LeetCode]48. Rotate Image
- LeetCode 48. Rotate Image
- LeetCode-48.Rotate Image
- 用linux命令和程序实际测试一下单进程能打开的最大的句柄数(也可以说是文件数)------并不一定是1024/2048哈
- 最小支撑(生成)树的prim算法 java
- tomcat中的几个配置文件
- leetcode 419. Battleships in a Board
- 单链表的C语言实现
- 【C++】【LeetCode】48. Rotate Image
- SpringMVC接受PUT和DELETE请求
- webpack-react
- 基本算法-分治算法
- 网络---NAT技术与代理服务器调研
- 23种设计模式-单例模式
- 最优化
- C
- 数据结构之循环队列和栈的应用