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?

Hide Tags

Array

 

  一题严格的数组逻辑操作问题,考虑认真就好,思路:
 

1 2 3 1
3 4 4 2
2 4 4 3
1 3 2 1

如上表,一共有两圈,对于n 来说,圈数为(n+1)/2,从外圈到内圈,转换了一圈后到下一圈。

 
我写的代码用了长度len ,其实写j=i 会方便很多。
需要计算好下标,有个简单的确认,一次转换中的4个数,不相邻的两个数的对应下标和为定值(n-1),例如一个是<i,j>,另一个就是<n-1  -i,n-1  -j>,利用这个规律其实只要确认两个就行。
代码如下:
 #include <iostream>
#include <vector>
#include <iterator>
using namespace std; class Solution {
public:
void rotate(vector<vector<int> > &matrix) {
int n =matrix.size();
if(n<) return ;
for(int i=;i<(n+)/;i++){
for(int len=;len<=n-*(i+);len++){
int temp = matrix[i][i+len];
matrix[i][i+len] = matrix[n--i-len][i];
matrix[n--i-len][i] = matrix[n--i][n--i-len];
matrix[n--i][n--i-len] = matrix[i+len][n-i-];
matrix[i+len][n-i-] = temp;
}
}
}
}; int main()
{
vector<vector<int> > matrix;
vector<int> tempm;
int cnt =;
for(int i=;i<;i++){
for(int j=;j<;j++)
tempm.push_back(cnt++);
matrix.push_back(tempm);
tempm.clear();
}
Solution sol;
sol.rotate(matrix);
for(int i=;i<matrix.size();i++){
copy(matrix[i].begin(),matrix[i].end(),ostream_iterator<int>(cout," "));
cout<<endl;
} return ;
}
 

[LeetCode] Rotate Image n-by-n矩阵顺时针旋转的更多相关文章

  1. LeetCode——Rotate Image(二维数组顺时针旋转90度)

      问题: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockw ...

  2. Rotate Image,N*N矩阵顺时针旋转90度

    public class RotateImage { public void rotate(int[][] matrix) { if(matrix.length == 1 && mat ...

  3. LeetCode Rotate Image (模拟)

    题意: 将一个n*n的矩阵顺时针旋转90度. 思路: 都是差不多的思路,交换3次也行,反转再交换也是行的. class Solution { public: void rotate(vector< ...

  4. 将n*n矩阵顺时针旋转90度

    /** * 将n*n矩阵顺时针旋转90度 * @param mat * @param n 矩阵的阶数 * @date 2016-10-7 * @author shaobn */ public stat ...

  5. C++ STL@ list 应用 (leetcode: Rotate Array)

    STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...

  6. [LeetCode]Rotate Image(矩阵旋转)

    48. Rotate Image     Total Accepted: 69437 Total Submissions: 198781 Difficulty: Medium You are give ...

  7. [leetcode]Rotate Image @ Python

    原题地址:https://oj.leetcode.com/problems/rotate-image/ 题意: You are given an n x n 2D matrix representin ...

  8. LeetCode——Rotate Image

    1. Question You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ( ...

  9. [LeetCode] Rotate Array 旋转数组

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

随机推荐

  1. Java第11次作业:什么是继承?继承的好处?什么是覆写?super()?构造代码块?子父类初始化顺序? 抽象类能用final声明吗?final关键字声明类 方法 变量以及全局常量?抽象类的构造方法?

    什么是继承? 继承是以父类为基础,子类可以增加新的数据或新的功能.子类不能选择性地继承父类.这种技术使得复用以前的代码非常容易. JAVA不支持多继承,单继承使JAVA的继承关系很简单,一个类只能有一 ...

  2. wsl基本安装与配置

    wsl简介: Windows Subsystem for Linux(简称WSL)是一个为在Windows 10上能够原生运行Linux二进制可执行文件(ELF格式)的兼容层.它是由微软与Canoni ...

  3. thinkphp5中php7中运行会出现No input file specified. 这个你改个东西

    <IfModule mod_rewrite.c> Options +FollowSymlinks -Multiviews RewriteEngine On RewriteCond %{RE ...

  4. Java-downloadFileByLink

    import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.ByteArrayOutputStr ...

  5. SDUSToj第十一次作业源代码格式问题

    Problem I: 源代码的格式 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 1471  Solved: 634 [Submit][Status][W ...

  6. selenium2设置浏览器窗口

    1.窗口最大化 //设置窗口最大化driver.manage().window().maximize(); 2.指定设置窗口大小 //指定呀设置窗口的宽度为:800,高度为600Dimension d ...

  7. Apache简易快速安装

    转发出处:https://blog.csdn.net/qq_34804120/article/details/78862290 准备安装包 到https://www.apachelounge.com/ ...

  8. luogu1578 奶牛浴场 枚举点最大子矩阵

    建议看看王知昆dalao的论文,讲得很好 #include <algorithm> #include <iostream> #include <cstring> # ...

  9. 转 关于oracle 分区表 表空间以及索引的总结

    关于oracle的表空间,分区表,以及索引的总结关键字: oracle, 表空间, 分区表, 索引 上周第一次做数据库测试碰到了很多问题特此总结: 表空间: Oracle的UNDOTBS01.DBF文 ...

  10. HAZU校赛 Problem K: Deadline

    Problem K: Deadline Time Limit: 2 Sec Memory Limit: 1280 MB Submit: 1106 Solved: 117 [Submit][Status ...