《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型,数组里的数据成为元素. ...
随机推荐
- 使用Excel调用ABAP系统的函数
效果:在excel里创建一个按钮,开发一些VB script,可以连接指定的ABAP系统并执行系统里的ABAP function module. 在这里例子里执行ABAP系统的函数TH_USER_LI ...
- Jenkins使用分组过滤分类
背景:Jenkins项目过多,通过选项卡的方式过滤需要的项目 1.点击选择卡上的加号 2.填写要分组的名字 3.可选择某个job进行分类,或者使用正则表达式的方式进行分类,楼主是根据正则进行匹配, 4 ...
- python 的矩阵运算——numpy
nbarray对象,就类似于C语言的数组!!! 一维数组: nbarray.array([]) 二维数组: nbarray.array([[],[]]) 数组大小: .shape 修改数组的排列: . ...
- vuejs给组件绑定原生事件
给组件绑定事件,该事件是自定义的事件 <div id='root'> <child @click='handleClick'></child> </div&g ...
- orderBy 过滤器
orderBy 过滤器根据表达式排列数组: <!DOCTYPE html><html><head><meta http-equiv="Content ...
- pooling
转自:http://www.gageet.com/2014/09182.php 本文部分参考了:http://www.zhihu.com/question/23437871 卷积层是对图像的一个邻域进 ...
- 服务器操作nginx相关操作命令
服务器操作nginx相关操作命令 登录服务器: ssh root@0.0.0.0 -p 22100 启动nginx: /usr/local/nginx/sbin/nginx 查看nginx是否启动 p ...
- http2.2配置
http: 超文本传输协议,工作在应用层 CentOS 6程序环境:httpd-2.2 配置文件: /etc/httpd/conf/httpd.conf /etc/httpd/conf.d/*.con ...
- datatable行内内容太长,有时不自动换行解决方法
加一个css属性即可 style = "word-wrap:break-word;" js代码: "render": function (data, type, ...
- Zeppelin interperter 模式设置总结图解2
该配置是在zeppelin的Interpreter的后台配置文件:conf/Interpreter.json spark Interpreter的模块定义那里.特别感谢开发团队组长大神的提示,深入挖掘 ...