Rotate Image(二位数组顺时针旋转)
问题描述:
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?
中文题目:
有一个NxN整数矩阵,请编写一个算法,将矩阵顺时针旋转90度。
给定一个NxN的矩阵,和矩阵的阶数N,请返回旋转后的NxN矩阵,保证N小于等于300。
分析:
过程如下图所示:
step1:
step1的结果为:
step2:
step2的结果为:
step3:
step4:
上图的圆圈中的元素按照上述顺序互换
这样,一层结束了;
step5:
在二维数组的内层继续执行step1–step4,直到最内层结束;
代码如下:
class Rotate {
public:
void swap(int &a,int &b)
{
int temp = a;
a = b;
b = temp;
}
vector<vector<int> > rotateMatrix(vector<vector<int> > mat, int n) {
if(n == 1 || n == 0)
return mat;
int up = 0;
int right = n-1;
int down = n-1;
int left = 0;
while(up < down && left < right)
{
//1,2 swap
int begin1 = up + 1;
for(int i = left + 1;i < right;i++)
{
swap(mat[up][i],mat[begin1++][right]);
}
//1 4 swap
begin1 = down - 1;
for(int i = left + 1;i < right;i++)
{
swap(mat[up][i],mat[begin1--][left]);
}
//3 4 swap
begin1 = up + 1;
for(int i = left + 1;i < right;i++)
{
swap(mat[down][i],mat[begin1++][left]);
}
//圆圈内的四个角上的元素互换
swap(mat[up][left],mat[up][right]);
swap(mat[up][left],mat[down][left]);
swap(mat[down][left],mat[down][right]);
up++;
down--;
left++;
right--;
}
return mat;
}
};
算法实现在数组本地实现,空间复杂度为O(1).
Rotate Image(二位数组顺时针旋转)的更多相关文章
- Python学习笔记 之 递归、二维数组顺时针旋转90°、正则表达式
递归.二维数组顺时针旋转90°.正则表达式 1. 递归算法是一种直接或间接调用自身算法的过程. 特点: 递归就是在过程或函数里调用自身 明确的递归结束条件,即递归出口 简洁,但是不提倡 递归次数多 ...
- C++ 出现bug :二位数组的操作运算,求非对角线的元素的和
编写一个通用程序,求出二位数组(行数和列数必须相等)的非对角线的元素之和,试建立类MATRIX完成上述功能 #include<iostream> using namespace std; ...
- PHP 之二位数组根据某个字段排序封装
/** * @param $array * @param $keys * @param string $sort * @return array */ function arraySort($arra ...
- java中Arrays.sort()对二位数组进行排序
int [][]a = new int [5][2]; //定义一个二维数组,其中所包含的一维数组具有两个元素 对于一个已定义的二位数组a经行如下规则排序,首先按照每一个对应的一维数组第一个元素进行升 ...
- LeetCode——Rotate Image(二维数组顺时针旋转90度)
问题: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockw ...
- php二位数组合并
转自:http://www.cnblogs.com/losesea/archive/2013/06/14/3134900.html 题目:有以下2个二维数组 1$a=Array(0 => Arr ...
- 【shell】构造并遍历二位数组的一种用法
参考shell数组的部分操作用法,实现了构造和遍历二维数组的一种方式,具体如下: #数组元素以空格分割 sites=("www.a.com www.b.com www.c.com www.d ...
- shell二位数组——终端字符下降动画
猜想:Shell支持关联数组,可以利用关联数组模拟二维数组. [验证猜想] #!/bin/bash array[1,1]=1 array[2,1]=2 array[3,1]=3 for i in `s ...
- vector创建二位数组
默认初始化vector vector<vevtor<int> > arr(row, vector<int>(col, 0)); //指定行大小为row,列为col, ...
随机推荐
- scala中隐式转换之隐式转换调用类中本不存在的方法
/** * Created by root * Description : 隐式转换调用类中本不存在的方法 */ class Person(name : String){ def getPersonN ...
- 3dmax导出模型使用相对路径读取纹理贴图
Shift+T快捷键打开“资源跟踪”窗口
- redis 缓存类型为map
// 获取分类列表,以及同类品牌 public Map<String, List> getCatalogInfo(Product product) { String key = Cache ...
- URL地址重写例子(Helicon)
# Helicon ISAPI_Rewrite configuration file# Version 3.1.0.86 #RewriteEngine on RewriteRule ^/esf/.+( ...
- IOS 视频直播/智能家居(一行行敲代码,从零开始)lesson:1整体架构
本文转载至 http://blog.csdn.net/u014011807/article/details/47144027 前段时间由于工作需要做了一个视频直播/智能家居类的应用.算是对iOS音视频 ...
- 【docker】 centos7 下 使用docker 安装 LNMP
一.安装 mysql 1 获取 mysql 镜像 docker pull mysql:5.7 2 创建mysql的镜像,并运行 docker run -d -p : -e MYSQL_ROOT_PAS ...
- ubuntu 用aptitude代替apt-get处理依赖性问题
aptitude 与 apt-get 一样,是 Debian 及其衍生系统中功能极其强大的包管理工具.与 apt-get 不同的是,aptitude 在处理依赖问题上更佳一些.举例来说,aptitud ...
- Linux账号和密码文件 /etc/passwd和/etc/shadow
Linux系统中,所有用户(包括系统管理员)的账号和密码都可以在/etc/passwd和/etc/shadow这两个文件中找到,(用户和密码就放在文件中,不怕被其他人看的或者修改吗?/etc/pass ...
- eclipse安装maven时候如果conf文件夹中有setting文件则会以这个文件为主,如果自己设置了user的配置文件则会无效
eclipse安装maven时候如果conf文件夹中有setting文件则会以这个文件为主,如果自己设置了user的配置文件则会无效
- rabbitmq在centos 7上的安装
一.安装步骤 参考了官网文档: http://www.rabbitmq.com/install-rpm.html#package-dependencies 这里大概介绍下. rabbitmq-serv ...