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</ ...
随机推荐
- 详解keepalived配置和使用
标签:keepalived 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://lanlian.blog.51cto.com/6790 ...
- PHP中的验证码类(验证码功能设计之二)
运行结果: <!--vcode.class.php内容--> <?php class Vcode { private $width; //宽 private $height; //高 ...
- 【CF173B】Chamber of Secrets(二分图,最短路)
题意:给你一个n*m的地图,现在有一束激光从左上角往右边射出,每遇到‘#’,你可以选择光线往四个方向射出,或者什么都不做,问最少需要多少个‘#’往四个方向射出才能使关系在n行往右边射出. 思路:将每一 ...
- Scrapy学习-17-暂停和重启
Scrapy爬虫暂停和重启 在当前项目下新建一个用于存储中间过程变量的目录 注意不同的spider不能共用一个目录 同一个spider每次运行时也必须使用新的目录 mkdir <spider_p ...
- 【leetcode】Find All Anagrams in a String
[leetcode]438. Find All Anagrams in a String Given a string s and a non-empty string p, find all the ...
- Nginx没有启动文件、nginx服务不支持chkconfig、nginx无法自启
Nginx没有启动文件.nginx服务不支持chkconfig.nginx无法自启 问题描述: Nginx安装后,当想要设置Ngixn为开机启动时, 就需要把nginx的启动命令路径放到/etc/rc ...
- Java后端WebSocket的Tomcat实现 html5 WebSocket 实时聊天
WebSocket协议被提出,它实现了浏览器与服务器的全双工通信,扩展了浏览器与服务端的通信功能,使服务端也能主动向客户端发送数据.Tomcat7.0.47上才能运行. 需要添加Tomcat里lib目 ...
- AC日记——魔术球问题 洛谷 P2765
题目描述 «问题描述: 假设有n根柱子,现要按下述规则在这n根柱子中依次放入编号为1,2,3,...的球. (1)每次只能在某根柱子的最上面放球. (2)在同一根柱子中,任何2个相邻球的编号之和为完全 ...
- vue 之循环添加不同class
在vue中按条件为class动态添加直接使用:class="[{ active: isActive }, errorClass]"之类的表达式就可以 但是如果我们要为一个循环列表按 ...
- WinRT 开发:在 MVVM 模式中,关于绑定的几处技巧
以下会提到三个绑定的技巧,分别是 在 ListView 中为 ListViewItem 的 MenuFlyout 绑定 Command: 在 ListView 的 事件中绑定所选择项目,即其 Sele ...