LeetCode OJ Rotate Image
来源:互联网 发布:淘宝五线谱乐器专营店 编辑:程序博客网 时间:2024/05/17 03:42
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?
找规律:
对于n*n的矩阵,只需变换n*n/4次,每次对四个元素变换,如果是单数则中间的那个元素无需变换。
class Solution {public: void rotate(vector<vector<int> > &matrix) { int swapTimes = matrix.size() * matrix[0].size() / 4; int swapStartI, swapStartJ, nowLineEnd, nowLineStart; swapStartI = swapStartJ = 0; nowLineEnd = matrix[0].size() - 1; nowLineStart = 0; for (int i = 0; i < swapTimes; i++) { swapFour(matrix[swapStartI][swapStartJ], matrix[swapStartJ][matrix.size() - 1 - swapStartI], matrix[matrix.size() - 1 - swapStartI][matrix.size() - 1 - swapStartJ], matrix[matrix.size() - 1 - swapStartJ][swapStartI]); // swap four numbers swapStartI++; if (swapStartI == nowLineEnd) { swapStartI = ++nowLineStart; nowLineEnd--; swapStartJ++; } } } void swapFour(int & a, int & b, int & c, int & d) { int temp; //int temp = a, a = b, b = c, c = d, d = temp; // anticlockwise temp = d, d = c, c = b, b = a, a = temp; // clockwise }};
0 0
- LeetCode OJ:Rotate Image
- LeetCode OJ - Rotate Image
- [LeetCode OJ]Rotate Image
- LeetCode OJ Rotate Image
- <LeetCode OJ> 48. Rotate Image
- LeetCode OJ-48-Rotate Image
- LeetCode OJ——Rotate Image
- LeetCode OJ算法题(四十七):Rotate Image
- LeetCode OJ 之 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 OJ Median of Two Sorted Arrays
- iOS开发人员必备App开发工具 ifunbox 支持iPhone, iPad和iPod Touch的文件及应用管理神器 使用实例
- win8.1系统 右键桌面鼠标变成圆圈反应很慢
- LeetCode OJ Valid Parentheses
- FZU Problem 2183 简单题(字符串处理)
- LeetCode OJ Rotate Image
- 7. tictactoe终于调试完成
- LeetCode OJ Anagrams
- 通过Tooz在python中实现分布式群组和锁管理
- Android学习笔记の九
- 创建pdf模板
- LeetCode OJ N-Queens II
- html5简单游戏编程从零开始
- Rotate Array(旋转队列)