2014-03-18 01:45

题目:给定一个NxN的矩阵,就地旋转90度。(没有样例又不说方向的话,随便往哪儿转。)

解法:如果N为奇数,除了中心点以外四等分。如果N为偶数,四等分。按照A->B->C->D->A的方式,轮换赋值,需要O(1)的额外空间保存A的值。

代码:

 // 1.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?
#include <cstdio>
#include <vector>
using namespace std; class Solution {
public:
void rotateImage90(vector<vector<int> > &board) {
int n = (int)board.size();
int i, j; if (n < ) {
return;
}
int w, h;
int tmp; w = n / ;
h = n - w;
for (i = ; i < w; ++i) {
for (j = ; j < h; ++j) {
tmp = board[i][j];
board[i][j] = board[n - - j][i];
board[n - - j][i] = board[n - - i][n - - j];
board[n - - i][n - - j] = board[j][n - - i];
board[j][n - - i] = tmp;
}
}
}
}; int main()
{
vector<vector<int> > board;
int i, j, n;
Solution sol; while (scanf("%d", &n) == && n > ) {
board.resize(n);
for (i = ; i < n; ++i) {
board[i].resize(n);
} for (i = ; i < n; ++i) {
for (j = ; j < n; ++j) {
scanf("%d", &board[i][j]);
}
}
sol.rotateImage90(board);
for (i = ; i < n; ++i) {
printf("%d", board[i][]);
for (j = ; j < n; ++j) {
printf(" %d", board[i][j]);
}
printf("\n");
}
printf("\n"); for (i = ; i < n; ++i) {
board[i].clear();
}
board.clear();
} return ;
}

《Cracking the Coding Interview》——第1章:数组和字符串——题目6的更多相关文章

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

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

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

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

  3. Cracking the coding interview

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

  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>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  6. Cracking the Coding Interview(Trees and Graphs)

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

  7. Cracking the Coding Interview 第一章

    第一章:数组与字符串 1 数组与字符串 请实现一个算法,确定一个字符串的所有字符是否全都不同.这里我们要求不允许使用额外的存储结构. 给定一个string iniString,请返回一个bool值,T ...

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

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

  9. Cracking the Coding Interview 150题(一)

    1.数组与字符串 1.1 实现一个算法,确定一个字符串的所有字符是否全都不同.假设不允许使用额外的数据结构,又该如何处理? 1.2 用C或C++实现void reverse(char* str)函数, ...

  10. C语言 第七章 数组与字符串

    一.数组 1.1.数组的概念 用来存储一组相同类型数据的数据结构.有点像班上放手机的手机袋,超市的储物柜. 特点:只能存放一种类型的数据,如全部是int型或者全部是char型,数组里的数据成为元素. ...

随机推荐

  1. React怎么创建.babelrc文件

    在windows环境下做react开发其实是一件非常让人头疼的事,强烈建议使用Mac或者是Linux系统,否则真的是自己挖坑自己跳了. 不过,这里还是给大家说说如何在windows环境下新建一个.ba ...

  2. c#按钮如何避免重复点击后报错

    前言:感谢51·halcon的绝地武士大佬啊,虽然你不认识我,但是我从你那学到了很多知识,真的感谢您对知识的无私传播哈哈(两天一个博客有在坚持的,都是草稿,等这个实习阶段过去了再回来整理博客~) bt ...

  3. Java 序列化对象工具类

    SerializationUtils.java package javax.utils; import java.io.ByteArrayInputStream; import java.io.Byt ...

  4. lasagne保存网络参数

    # Optionally, you could now dump the network weights to a file like this: # np.savez('model.npz', *l ...

  5. 在IDEA中创建Maven项目和添加tomcat

    IDEA中创建是一种创建maven项目的办法,但不推荐,因为没有使用统一的骨架,可以一开始就选择创建maven项目,步骤如下: 第一步 第二步:设置项目 第三步:进行配置好maven,加上 arche ...

  6. eclipse 插件relo使用

    1. eclipse插件安装 在线安装地址:http://relo.csail.mit.edu/update 本地配置,文件下载:http://download.csdn.net/download/s ...

  7. Http之基础

    简介 HTTP协议(HyperText Transfer Protocol,超文本传输协议)是因特网上应用最为广泛的一种网络传输协议,所有的WWW文件都必须遵守这个标准. HTTP是一个基于TCP/I ...

  8. ajax模仿form上传图片

    <form id="iconForm"> <input class="js_upFile cover1" type="file&qu ...

  9. 搭建大众点评CAT监控平台

    CAT(Central Application Tracking)是基于Java开发的实时应用监控平台,包括实时应用监控,业务监控.关于CAT的具体介绍可移步到CAT官网进行查阅. 1. 环境清单 C ...

  10. iOS App 瘦身方案

    缩减iOS安装包大小是很多中大型APP都要做的事,一般首先会对资源文件下手,压缩图片/音频,去除不必要的资源.这些资源优化做完后,我们还可以尝试对可执行文件进行瘦身,项目越大,可执行文件占用的体积越大 ...