《Cracking the Coding Interview》——第1章:数组和字符串——题目6
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的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- 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>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview 第一章
第一章:数组与字符串 1 数组与字符串 请实现一个算法,确定一个字符串的所有字符是否全都不同.这里我们要求不允许使用额外的存储结构. 给定一个string iniString,请返回一个bool值,T ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- Cracking the Coding Interview 150题(一)
1.数组与字符串 1.1 实现一个算法,确定一个字符串的所有字符是否全都不同.假设不允许使用额外的数据结构,又该如何处理? 1.2 用C或C++实现void reverse(char* str)函数, ...
- C语言 第七章 数组与字符串
一.数组 1.1.数组的概念 用来存储一组相同类型数据的数据结构.有点像班上放手机的手机袋,超市的储物柜. 特点:只能存放一种类型的数据,如全部是int型或者全部是char型,数组里的数据成为元素. ...
随机推荐
- 非常全面的PHP header函数设置HTTP头的示例
突然看到这个,觉得很好,就拿过来了,如下: //定义编码 header( 'Content-Type:text/html;charset=utf-8 '); //Atom header('Conten ...
- pat甲级1107
1107 Social Clusters (30 分) When register on a social network, you are always asked to specify your ...
- C# 安装 Visual Studio IDE
官网: https://visualstudio.microsoft.com/zh-hans/ 下载社区版(免费的) .微软的软件安装都是很nice的.安装过程中选择需要的配置进行安装(比如.net桌 ...
- less通用pc移动库
// less 文件 (移动端通用less文件) // 作者 marchen // 时间 2014/9/1 // 协议 MIT // 只考虑webkit内核手机浏览器和火狐内核浏览器 // 自定义le ...
- jenkins 执行shell命令出错command not found 和No such file or directory
[root@localhost usr]# sh test.sh command not found -bash: cd: usr: No such file or directory 这里碰到了一个 ...
- redis 对cmd的操作
这个是原子递增的知识点: 关于list部分: 利用lpush命令, rpush命令, lrange命令,对列表操作 此前 我已经 在列表(list)中 插入了 部分 元素了 关于集合set 部分 首先 ...
- Nginx + uWSGI + web.py 搭建示例
(1)安装Nginx1.1 下载nginx-1.0.5.tar.gz并解压1.2 ./configure (也可以增加--prefix= path指定安装路径)此时有可能会提示缺少pcre支持,如果要 ...
- ImportError : cannot import name main
当我们有时候安装不成功插件或者其他模块时候,会有pip报错hu@hu-VirtualBox:~/下载/MySQL-python-1.2.4b4$ pip install pymysqlTracebac ...
- mysql存储问题
为什么标题要起这个名字呢?commen sence指的是那些大家都应该知道的事情,但往往大家又会会略这些东西,或者对这些东西一知半解,今天我总结下自己在mysql中遇到的一些commen sense类 ...
- docker swarm使用keepalived+haproxy搭建基于percona-xtradb-cluster方案的高可用mysql集群
一.部署环境 序号 hostname ip 备注 1 manager107 10.0.3.107 centos7;3.10.0-957.1.3.el7.x86_64 2 worker68 10.0.3 ...