【字符串与数组】

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. POJ3690 Constellations 【KMP】

    Constellations Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5044   Accepted: 983 Des ...

  2. Nuget介绍及使用技巧

    一.介绍 什么是Nuget? 引用自Nuget网站的原话“NuGet is the package manager for the Microsoft development platform inc ...

  3. 如何监控第三方应用程序(SOAP or RESTful client)访问HTTPS当数据站点?

    随着越来越多的互联网应用,在我们日常的开发和调试,其中(例如,调试SOAP和RESTFul什么时候),我们经常需要访问工具,通过第三方获取HTTPS网站.为了简化叙述说明,如本文所用,IE浏览器访问G ...

  4. 加载xib文件的两种方式

    一.加载xib文件的两种方式 1.方法一(NewsCell是xib文件的名称) NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@&quo ...

  5. phpstorm安装laravel-ide-helper实现自动完成、代码提示和跟踪

    本文讲述laravel-ide-helper的安装方法.phpstorm安装了laravel-ide-helper后可以实现代码提示.跟踪和自动补全,减少查看API文档的次数,提高开发效率. lara ...

  6. 一张地图,告诉你NodeJS命令行调试器语句

    NodeJS提供脚本调试. 进入node debug xx.js您可以进入调试模式. 版权声明:本文博客原创文章,博客,未经同意,不得转载.

  7. C#修改用户名

    string strCmdText; strCmdText = "useraccount where name='" + 旧密码 + "' rename " + ...

  8. 在windows server里,对于同一个账号,禁止或允许多个用户使用该账户,同时登录

    开始 -> 运行 -> gpedit.msc -> 本地计算机 策略 -> 计算机配置 -> 管理模板 -> Windows 组件 -> 远程桌面服务 -&g ...

  9. C#用Open与Add方法打开word文档的区别

    C#打开word文档常用有两种方法:Add与Open. Microsoft.Office.Interop.Word._Document doc = (Document)appWord.Document ...

  10. 屏幕录制H.264视频,AAC音频,MP4复,LibRTMP现场活动

    上周完成了一个屏幕录制节目,实时屏幕捕获.记录,视频H.264压缩,音频应用AAC压缩,复用MP4格公式,这使得计算机和ios设备上直接播放.支持HTML5的播放器都能够放,这是标准格式的优点.抓屏也 ...