[google面试CTCI] 1-6.图像旋转问题
【字符串与数组】
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
现在看出规律来了没?将上图与最终的旋转结果比较,发现它跟最终的旋转结果是按行首尾对称的,将上图首尾对称的列交换,即可得到最终结果。
即先交换对角线元素,再交换首尾行(第一行与最后一行交换,第二行与倒数第二行交换等…),至此,可以编码如下:
作者:Viidiot 微信公众号:linux-code
[google面试CTCI] 1-6.图像旋转问题的更多相关文章
- [google面试CTCI] 1-8.判断子字符串
[字符串与数组] Q:Assume you have a method isSubstring which checks if one word is a substring of another G ...
- [google面试CTCI] 2-1.移除链表中重复元素
[链表] Q:Write code to remove duplicates from an unsorted linked list FOLLOW UP How would yo ...
- [google面试CTCI] 2-2 找出链表的倒数第n个节点元素
[链表] Q:Implement an algorithm to find the nth to last element of a singly linked list . 题目:找出链表的倒数第 ...
- [google面试CTCI] 2-3 只给定链表中间节点指针,如何删除中间节点?
[链表] Q:Implement an algorithm to delete a node in the middle of a single linked list, given only acc ...
- [google面试CTCI] 2-0.链表的创建
创建链表.往链表中插入数据.删除数据等操作,以单链表为例. 1.使用C语言创建一个链表: typedef struct nd{ int data; struct nd* next; } node; / ...
- [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 ...
- [google面试CTCI] 1-5.替换字符串中特定字符
[字符串与数组] Q:Write a method to replace all spaces in a string with ‘%20’ 题目:写一个算法将一个字符串中的空格替换成%20 解答: ...
- [google面试CTCI] 1-4.判断两个字符串是否由相同字符组成
[字符串与数组] Q:Write a method to decide if two strings are anagrams or not 题目:写一个算法来判断两个字符串是否为换位字符串.(换位字 ...
- [google面试CTCI]1-3.字符串去重
[字符串与数组] Q:Design an algorithm and write code to remove the duplicate characters in a string without ...
随机推荐
- java中的反射,invoke方法[转]
在施老师的项目中需要用到invoke,就是通过函数名反射相应的函数.一下代码简单地介绍了java反射中invoke方法,如果要具体的,可以参考魔乐核心课程的反射部分内容 package org.cur ...
- hibernate中使用sql语句进行表链接查询,对结果集的遍历方法
今天做了一个在hibernate中使用sql语句进行表链接查询的功能,得到的属性是来自两个表中的字段.下面对结果集遍历的方法进行记录. sql语句不写了.部分代码如下: List<Course_ ...
- CodeForces 425E Sereja and Sets
意甲冠军: 集S它包括了很多间隔[l,r] 和1<=l<=r<=n f(S)个不相交的区间 问给出n和f(S) 有几种可能的S集合 思路: dp好题 至于为啥是dp- 我 ...
- .net中的设计模式---单例模式,涉及lock的用法
.客户端代码 static void Main(string[] args) { Singleton singleton2 = Singleton.GetInstance(); Singleton s ...
- hdu 3709 数字dp(小思)
http://acm.hdu.edu.cn/showproblem.php?pid=3709 Problem Description A balanced number is a non-negati ...
- R.layout.main connot be resolved 和R.java消失
出现例如以下问题: 鼠标放到出代码上面: 分析问题: 1.查看R文件是否被生成.假设没有生成,则勾选build Automatically,然后Clean: 2.假设R文件已生成.则删除去掉代码中: ...
- hibernate的orphanRemoval
在@OneToMany与@OneToOne中使用orphanRemoval = true时候 改动保存时候setXXX org.springframework.orm.hibernate3.Hiber ...
- Android ProgressBar 反向进度条/进度条从右到左走
近期的项目,有个需求须要使用条状图显示比例,而且右对齐,见下图: 我想到了使用进度条,这样不就不须要在代码动态绘制条状了,省了非常多活. 那么进度条如何从右向左显示呢? 方案一: 将ProgressB ...
- 如何运行代码apk安装
import java.io.File; import android.app.Activity; import android.content.Intent; import android.net. ...
- bluetooth发展(五岁以下儿童)------蓝牙功能测试(一个)
newton板已出版.下面再组织我调试的一小方面,,蓝牙功能的实现和测试: 转载请注明出处:http://blog.csdn.net/wang_zheng_kai 以下是我写的newton开发板中bl ...