Cracking the coding interview--Q1.6
原文:
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度。 并且要求在原矩阵上进行操作,即不允许开辟额外的存储空间。
解答
(一)Hawstein在他的Blog中http://hawstein.com/posts/1.6.html介绍的方法是:可以分两步走。 第一步交换主对角线两侧的对称元素,第二步交换第i行和第n-1-i行,即得到结果。 看图示:
原图: 第一步操作后: 第二步操作后:
1 2 3 4 1 5 9 13 4 8 12 16
5 6 7 8 2 6 10 14 3 7 11 15
9 10 11 12 3 7 11 15 2 6 10 14
13 14 15 16 4 8 12 16 1 5 9 13
(二)在可以直接考虑旋转,四个方向的元素依次移动。
public class Main {
public static void rotate(int[][] mat, int n) {
for (int i = 0; i < n / 2; i++) {
for (int j = i; j < n - 1 - i; j++) {
// save top
int temp = mat[i][j];
// left --> top
mat[i][j] = mat[n - 1 - j][i];
// down --> left
mat[n - 1 - j][i] = mat[n - 1 - i][n - 1 - j];
// right --> down
mat[n - 1 - i][n - 1 - j] = mat[j][n - 1 - i];
// top --> right
mat[j][n - 1 - i] = temp;
}
}
}
public static void main(String args[]) {
int a[][] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j)
System.out.print(a[i][j] + " ");
System.out.println();
}
rotate(a, 4);
System.out.println("------------------");
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j)
System.out.print(a[i][j] + " ");
System.out.println();
}
}
}
Cracking the coding interview--Q1.6的更多相关文章
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目6
2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...
- 《Cracking the Coding Interview》——第5章:位操作——题目7
2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)
#include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...
随机推荐
- Selenium webdriver 元素操作
本来这些东西网上一搜一大堆,但是本着收集的精神,整理一份放着吧!哈!哈!哈! 1. 输入框(text field or textarea) WebElement element = driver.fi ...
- Swift基础语法学习总结二
1.函数 1.1 func funcNmae()->(){} 这样就定义了一个函数,它的参数为空,返回值为空,如果有参数和返回值直接写在两个括号里就可以了 1.2 参数需要指明类型,而如果没有返 ...
- 利用"NOTIFYICONDATA"实现MFC的托盘程序
本文章为转发百度空间内容,,保存一下,以防以后用到.. 一.自定义信息 在头文件中加入下面这句话: #define WM_SHOWTASK (WM_USER+1) 二.MYDLG.CPP文件中添加_m ...
- linux关于readlink函数获取运行路径的小程序
http://blog.csdn.net/djzhao/article/details/8178375 相关函数: stat, lstat, symlink表头文件: #include <u ...
- Android开发系列(十八):自己定义控件样式在drawable目录下的XML实现
在Android开发的过程中,我们常常须要对控件的样式做一下改变,能够通过用添加背景图片的方式进行改变,可是背景图片放多了肯定会使得APK文件变的非常大. 我们能够用自己定义属性shape来实现. s ...
- boost 相等与等价的区别
- LSI MegaCli 命令使用4
Dell服务器常用管理命令总结准备新版本的 MegaCli-1.01.24-0.i386.rpm 会把程序安装在/opt下,可以自定义安装目录,例如:rpm --relocate /opt/=/usr ...
- TCP Linger的坑
昨天和同事奋战几个小时,解决了一个linger造成的bug. 现象是这样的,这是一个我从原型接手,扩充了各种功能成为可用代码的epoll实现的非阻塞socket server程序,接收大量的短连接,测 ...
- WebStorm修改默认快捷键
打开 "File" -> "Setting" -> "keymap",即可修改.
- Win7启动修复MBR(Win7+Linux删除Linux后进入grub rescue的情况)
事因:我的笔记本原先同时安装了Win7+Linux,昨天发现硬盘实在不够用(才60G,虽然还有个500G的移动硬盘),就想把里面的Ubuntu格了.都是用虚拟机做测试的多.后来就格了Ubuntu所在的 ...