CareerCup之1.6 Rotate Image
【题目】
原文:
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?
译文:
一张图像表示成NxN的矩阵。图像中每一个像素是4个字节,写一个函数把图像旋转90度。 你能原地进行操作吗?(即不开辟额外的存储空间)
【分析】
【代码一】
- /*********************************
- * 日期:2014-05-14
- * 作者:SJF0115
- * 题目: Rotate Image
- * 来源:CareerCup
- **********************************/
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <string.h>
- using namespace std;
- //旋转图片
- void RotateImage(vector<vector<int> > &matrix){
- int i,j,temp;
- int N = matrix.size();
- // 沿着副对角线反转
- for(i = 0; i < N;i++){
- for(j = 0;j < N - i;j++){
- temp = matrix[i][j];
- matrix[i][j] = matrix[N - 1 - j][N - 1 - i];
- matrix[N - 1 - j][N - 1 - i] = temp;
- }
- }
- // 沿着水平中线反转
- for(i = 0; i < N / 2;i++){
- for (j = 0; j < N;j++){
- temp = matrix[i][j];
- matrix[i][j] = matrix[N - 1 - i][j];
- matrix[N - 1 - i][j] = temp;
- }
- }
- }
- int main(){
- vector<int> row1 = {1,2,3};
- vector<int> row2 = {4,5,6};
- vector<int> row3 = {7,8,9};
- vector<vector<int>> matrix;
- matrix.push_back(row1);
- matrix.push_back(row2);
- matrix.push_back(row3);
- RotateImage(matrix);
- for(int i = 0;i < 3;i++){
- for(int j = 0;j < 3;j++){
- cout<<matrix[i][j]<<" ";
- }
- cout<<endl;
- }
- return 0;
- }
【代码二】
- /*********************************
- * 日期:2014-05-14
- * 作者:SJF0115
- * 题目: Rotate Image
- * 来源:CareerCup
- **********************************/
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <string.h>
- using namespace std;
- //旋转图片
- void RotateImage(vector<vector<int> > &matrix){
- int layer,i,top;
- int n = matrix.size();
- //一层一层旋转
- for(layer = 0;layer < n/2;layer++){
- //第layer层第一个元素
- int first = layer;
- //第layer层最后一个个元素
- int last = n -1 - layer;
- for(i = first;i < last;i++){
- //偏移量
- int offset = i - first;
- top = matrix[first][i];
- //left-top
- matrix[first][i] = matrix[last-offset][first];
- //bottom-left
- matrix[last-offset][first] = matrix[last][last-offset];
- //right-bottom
- matrix[last][last-offset] = matrix[i][last];
- //top-right
- matrix[i][last] = top;
- }//for
- }//for
- }
- int main(){
- vector<int> row1 = {1,2,3};
- vector<int> row2 = {4,5,6};
- vector<int> row3 = {7,8,9};
- vector<vector<int>> matrix;
- matrix.push_back(row1);
- matrix.push_back(row2);
- matrix.push_back(row3);
- RotateImage(matrix);
- for(int i = 0;i < 3;i++){
- for(int j = 0;j < 3;j++){
- cout<<matrix[i][j]<<" ";
- }
- cout<<endl;
- }
- return 0;
- }
CareerCup之1.6 Rotate Image的更多相关文章
- [CareerCup] 1.6 Rotate Image 翻转图像
1.6 Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a m ...
- Careercup | Chapter 1
1.1 Implement an algorithm to determine if a string has all unique characters. What if you cannot us ...
- Canvas绘图之平移translate、旋转rotate、缩放scale
画布操作介绍 画布绘图的环境通过translate(),scale(),rotate(), setTransform()和transform()来改变,它们会对画布的变换矩阵产生影响. 函数 方法 描 ...
- [LeetCode] Rotate Array 旋转数组
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- [LeetCode] Rotate List 旋转链表
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- [LeetCode] Rotate Image 旋转图像
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- jQuery.rotate.js参数
CSS3 提供了多种变形效果,比如矩阵变形.位移.缩放.旋转和倾斜等等,让页面更加生动活泼有趣,不再一动不动.然后 IE10 以下版本的浏览器不支持 CSS3 变形,虽然 IE 有私有属性滤镜(fil ...
- CSS3属性transform详解之(旋转:rotate,缩放:scale,倾斜:skew,移动:translate)
CSS3属性transform详解之(旋转:rotate,缩放:scale,倾斜:skew,移动:translate) 在CSS3中,可以利用transform功能来实现文字或图像的旋转.缩放.倾 ...
- 偏移:translate ,旋转:rotate,缩放 scale,不知道什么东东:lineCap 实例
<!DOCTYPE HTML> <head> <meta charset = "utf-8"> <title>canvas</ ...
随机推荐
- 【Luogu】P2254瑰丽华尔兹(堆优化DP)
题目链接 我也不知道为什么脑子一抽就想了个堆优化……然后贼慢…… 因为上午听不懂wys的电音专场(快速傅立叶变换),然后就做了这么一道题. 首先朴素DP很sb都能秒出.就是枚举时刻.位置(两维)然后转 ...
- 用CSS给表格加边框
很久之前,给表格加边框用的方法是给表格加上背景色,然后把cellspacing="1",再给td设置成另一种颜色,这样间接的加边框颜色. 一直没去细研究,今天发现了一种很简单的加边 ...
- HDU-1534 Schedule Problem
四种约束条件..照做就行了.. 最长路建图. #include <cstdio> #include <cstdlib> #include <cstring> #in ...
- 【spring aop切面】基础使用教程
package tpf.aspect; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFact ...
- 【CF1028B】Unnatural Conditions(构造)
题意:给定n与m,要求构造两个长度不超过2230的数字x,y使得x,y各自的数位和>=n,x+y的数位和<=n 思路: #include<cstdio> #include< ...
- 【BZOJ1103】大都市meg(DFS序,树状数组)
题意:有一颗树,1号点为根,保证编号小的点深度较小,初始状态每条边都没有被标记,要求实现两个操作在线: A:将连接x,y的边标记 W:查询从1到x的路径上有多少条边未被标记 n<=2*10^5 ...
- 汇编指令详解--as手册
https://sourceware.org/binutils/docs/as/ Table of Contents 1 Overview 1.1 Structure of this Manual 1 ...
- github每次push提交都要输入账号密码
问题产生的原因是在克隆的时候使用的是https的方式或者用一些特殊的指令来克隆的github项目源,如 golang里的go get github.com/...... 没次提交push的时候都会提示 ...
- Tomcat是怎么工作的(1) -- 开篇
这是一个系列文章的第一篇. 标题还是费了点脑子才确定的,起什么名字比较好呢.Tomcat工作原理?深入浅出Tomcat运行机制?从零开始研究Tomcat?Tomcat是怎么运行起来的?Tomcat是如 ...
- AC日记——[SCOI2010]游戏 bzoj 1854
1854: [Scoi2010]游戏 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 4938 Solved: 1948[Submit][Status] ...