【字符串与数组】

Q:Given an image represented by an NxN matrix, where each pixel in the image is 4
bytes, write a method to rotate the image by 90 degrees Can you do this in place?

题目:假定一幅图像能用NxN的矩阵表示,每个像素是四字节。写一个算法将图像旋转90度,你能否在原地进行操作(也即不分配额外的存储空间)?

解答:


我们不知道具体该如何操作,但有一点可以肯定的是,需要通过交换一些元素的位置来达到旋转的目的。具体怎么交换,我们可以通过画一个小矩阵来寻找方法。

假设我们将图像逆时针旋转90度  ,对于一个4x4的图像,假设其各个值如下:

1      2    3    4

5      6    7    8

9     10  11  12

13  14  15   16

目标状态如下:

4    8    12     16

3    7    11     15

2    6    10     14

1    5    9       13

是不是还没看出规律来?要将图像旋转90度,我们有一个很直观的观察结果是:对角线两边对称元素有一个互换的过程。要不我们先交换对角线两边的元素看看:

1    5    9     13

2    6    10   14

3    7    11   15

4    8    12    16

现在看出规律来了没?将上图与最终的旋转结果比较,发现它跟最终的旋转结果是按行首尾对称的,将上图首尾对称的列交换,即可得到最终结果。

即先交换对角线元素,再交换首尾行(第一行与最后一行交换,第二行与倒数第二行交换等…),至此,可以编码如下:

#define N 4 void rotate(int matrix[][N]){ int i,j; int iTmp; for(i=0;i<N;++i){ for(j=i+1;j<N;++j){ iTmp=matrix[i][j]; matrix[i][j]=matrix[j][i]; matrix[j][i]=iTmp; } } for(i=0;i<N/2;++i){ for(j=0;j<N;++j){ iTmp=matrix[i][j]; matrix[i][j]=matrix[n-i-1][j]; matrix[n-i-1][j]=iTmp; } } }

作者:Viidiot  微信公众号:linux-code

[google面试CTCI] 1-6.图像旋转问题的更多相关文章

  1. [google面试CTCI] 1-8.判断子字符串

    [字符串与数组] Q:Assume you have a method isSubstring which checks if one word is a substring of another G ...

  2. [google面试CTCI] 2-1.移除链表中重复元素

    [链表] Q:Write code to remove duplicates from an unsorted linked list      FOLLOW UP      How would yo ...

  3. [google面试CTCI] 2-2 找出链表的倒数第n个节点元素

    [链表] Q:Implement an algorithm to find the nth to last element of a singly  linked list . 题目:找出链表的倒数第 ...

  4. [google面试CTCI] 2-3 只给定链表中间节点指针,如何删除中间节点?

    [链表] Q:Implement an algorithm to delete a node in the middle of a single linked list, given only acc ...

  5. [google面试CTCI] 2-0.链表的创建

    创建链表.往链表中插入数据.删除数据等操作,以单链表为例. 1.使用C语言创建一个链表: typedef struct nd{ int data; struct nd* next; } node; / ...

  6. [google面试CTCI] 1-7.将矩阵中特定行、列置0

    [字符串与数组] Q:Write an algorithm such that if an element in an MxN matrix is 0, its entire row and colu ...

  7. [google面试CTCI] 1-5.替换字符串中特定字符

    [字符串与数组] Q:Write a method to replace all spaces in a string with ‘%20’ 题目:写一个算法将一个字符串中的空格替换成%20 解答: ...

  8. [google面试CTCI] 1-4.判断两个字符串是否由相同字符组成

    [字符串与数组] Q:Write a method to decide if two strings are anagrams or not 题目:写一个算法来判断两个字符串是否为换位字符串.(换位字 ...

  9. [google面试CTCI]1-3.字符串去重

    [字符串与数组] Q:Design an algorithm and write code to remove the duplicate characters in a string without ...

随机推荐

  1. shell变量赋值进阶

    首先,要理解shell中变量的3种赋值情况: unset 例子. unset a 空字符串, null 例子. a='' 非空,即不是unset,并且不是空字符串 例子: a=1 or a=b等 然后 ...

  2. linux_awk_内部正则过滤

    awk -F'\t' '{if($3 !~ /^<p><img/){print $0}}' latex500.db|wc -l

  3. easyui datagrid datagrid-filter bug

    问题描述:空字符串.数字过滤 过滤异常 修改js源码: $.fn.datagrid.defaults.operators = { nofilter: { text: 'No Filter' }, co ...

  4. NPOI mvc easyui 根据Excel模板 生成Excel

    1.首先下载 NPOI  https://npoi.codeplex.com/releases  只要dll 就好 示例代码库太难懂了. NPOI 是一个开源  免费的 东西.而且不依赖 office ...

  5. php rsa 加密、解密、签名、验签

    由于对接第三方机构使用的是Java版本的rsa加解密方法,所有刚开始在网上搜到很多PHP版本的rsa加解密,但是对接java大多都不适用. 以下php版本是适用于对接java接口,java适用密钥再p ...

  6. MongoDB的upsert状态判断和pymongo使用方法

    在mongo中,有一个命令非常的方便,就是upsert,顾名思义就是update+insert的作用 根据条件判断有无记录,有的话就更新记录,没有的话就插入一条记录 upsert的使用方法: Mong ...

  7. HBuilder CSS 自定义代码块

    =begin 本文档是CSS代码块的编辑文件.注意不要把其他语言的设置放到css里来. HBuilder可使用ruby脚本来编辑代码块和增强操作命令. 1.编辑代码块 如果要新增一个代码块,复制如下一 ...

  8. 如何去除configure的默认选择-g O2

    http://lists.gnu.org/archive/html/autoconf/2006-04/msg00002.html http://www.linuxidc.com/Linux/2013- ...

  9. 设计与实现的简单和经常使用的权限系统(五岁以下儿童):不维护节点的深度level,手工计算level,树形结构

     以这种方式.和第三的类似介绍.所不同的是.深度未在数据库中存储节点level,添加和更改时间,护.而是,在程序中,实时去计算的. 至于后面的,依照level升序排序,再迭代全部的节点构造树,与第三篇 ...

  10. HDU4565 && 2013年长沙邀请赛A题

    部分转自http://blog.csdn.net/crazy______/article/details/9021169 #include<cstdio> using namespace ...