【字符串与数组】

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. BST(Binary Search Tree)

    原文链接:http://blog.csdn.net/jarily/article/details/8679280 /****************************************** ...

  2. 深入浅出MS06-040

    入浅出MS06-040 时至今日,网上已有颇多MS06-040的文章,当中不乏精辟之作.与其相比,本文突显业余,技术上无法超越,徒逞口舌之快.本文适合有一定计算机基础,初步了解溢出攻击原理,略微了解逆 ...

  3. 编译AVX代码,升级Redhat 5.5 GCC至4.7.1

    Redhat 的GCC编译器4.1版本号,为SSE4,AVX,AVX2支持不够好,官方建议4.7以上. 就这样开始了GCC升级之路. 因为Redhat 5.5它安装在一个虚拟机.全然解决,经过若干尝试 ...

  4. Android 2.3.5源码 更新至android 4.4,能够下载,度娘网盘

    Android 4.4源代码下载(linux合并) ==============================切割线结束========================= 旧版本号的能够使用115, ...

  5. MVC提交时验证

    第一种 @using (Html.BeginForm("ProdPromotionEdit", "Product", FormMethod.Post, new ...

  6. WebService它CXF注释错误(两)

    WebService它CXF注解 1.详细报错例如以下 五月 04, 2014 11:24:12 下午 org.apache.cxf.wsdl.service.factory.ReflectionSe ...

  7. c# 官方命名规则

    官方的哦. https://msdn.microsoft.com/zh-cn/library/vstudio/ff926074(v=vs.110).aspx

  8. 用Iconv应对NodeJs对称加密技术在汉字编码与NoSQL的一些坑洞

    ·起因 汉字编码技术在实际应用中总是会存在这样或者那样的问题,尤其是在一些热门NoSQL方面多少会遇到挑战.比方说Cassandra字符集还不直接支持GB2312,要想存储写汉字那可真是麻烦.当然这还 ...

  9. cmd 跟踪路由

    cmd   命令   tracert  ip 地址 用 来 跟踪路由

  10. (c#2.0)serialPort串口通讯

    原文:(c#2.0)serialPort串口通讯 using System; using System.Collections.Generic; using System.ComponentModel ...