原文:

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的更多相关文章

  1. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  2. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  3. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  4. 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 ...

  5. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  6. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  7. 《Cracking the Coding Interview》——第13章:C和C++——题目6

    2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...

  8. 《Cracking the Coding Interview》——第5章:位操作——题目7

    2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...

  9. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  10. 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)

    #include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...

随机推荐

  1. G - Balanced Lineup - poj3264(区间查询)

    题意:给你一组值,然后询问某个区间的最大值和最小值得差 分析:因为没有更新,所以只需要查找即可,节点保存一个最大值最小值就行了 ************************************ ...

  2. C/C++中如何获取数组的长度?

    C.C++中没有提供 直接获取数组长度的函数,对于存放字符串的字符数组提供了一个strlen函数获取长度,那么对于其他类型的数组如何获取他们的长度呢?其中一种方法是使 用sizeof(array) / ...

  3. Java集合中那些类是线程安全的

    线程安全类 在集合框架中,有些类是线程安全的,这些都是jdk1.1中的出现的.在jdk1.2之后,就出现许许多多非线程安全的类. 下面是这些线程安全的同步的类: vector:就比arraylist多 ...

  4. Jquery 的bind(), live(), delegate(), on()绑定事件方式

    1. 因为项目中经常会有利用jquery操作dom元素的增删操作,所以会涉及到dom元素的绑定事件方式,简单的归纳一下bind,live,delegate,on的区别,以便以后查阅. bind() 适 ...

  5. 外键约束列并没有导致大量建筑指数library cache pin/library cache lock

    外键约束列并没有导致大量建筑指数library cache pin/library cache lock 清除一个100大数据表超过一百万线,发现已经运行了几个小时: delete B001.T_B1 ...

  6. [转] Makefile中调用Shell

    1.在Makefile中只能在target中调用Shell脚本,其他地方是不能输出的.比如如下代码就是没有任何输出: VAR="Hello" echo "$(VAR)&q ...

  7. 【移动开发】WIFI热点通信(一)

    之前调查过Android中WIFI模块的使用,也写过两篇学习总结的文章(http://smallwoniu.blog.51cto.com/3911954/1334951),后来发现DEMO里面还是有许 ...

  8. MySQL(11):存储引擎

    1.存储引擎是什么?       MySQL中的数据用各种不同的技术存储在文件(或者内存)中.这些技术中的每一种技术都使用不同的存储机制.索引技巧.锁定水平并且最终提供广泛的不同的功能和能力.通过选择 ...

  9. 【翻译】A (very) short introduction to R R的简短介绍

    [前言] 本文翻译自Paul Torfs & Claudia Brauer的文章A (very) short introduction to R.其中比较简单的地方没有翻译,不好用中文描述的地 ...

  10. mongodb的tailCursor的设计思想

    http://derickrethans.nl/mongodb-and-solr.html 这是mongodb的php客户端的写法