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</ ...
随机推荐
- 【bzoj2346】[Baltic 2011]Lamp 堆优化Dijkstra
题目描述 2255是一个傻X,他连自己家灯不亮了都不知道.某天TZ大神路过他家,发现了这一情况,于是TZ开始行侠仗义了.TZ发现是电路板的问题,他打开了电路板,发现线路根本没有连上!!于是他强大的脑力 ...
- CS231n笔记 Lecture 5 Convolutional Neural Networks
一些ConvNets的应用 Face recognition 输入人脸,推测是谁 Video classfication Recognition 识别身体的部位, 医学图像, 星空, 标志牌, 鲸.. ...
- NOJ——1568走走走走走啊走(超级入门DP)
[1568] 走走走走走啊走 时间限制: 1000 ms 内存限制: 65535 K 问题描述 菜菜赚了钱回来,想起要买很多桶回来,不同地方的桶质量是不同的,他在(1,1)点出发因为飞机票有点贵所以他 ...
- 论蛋疼的调戏matrix67的首页
唔,初学js,然后拿matrix67的首页玩玩23333 console 里输入以下这句话 for(var i = 1; i <= 400; i++){var td = document.get ...
- mac上安装chromedriver
默认下载2.35版本,放置到/usr/local/bin #!/usr/bin/env bash # ------------------------------------------------- ...
- scrapy之小试身手
要爬取的网址是:http://quotes.toscrape.com/ 磕磕绊绊的写完了 spiders import scrapy from kkk.items import * class Quo ...
- Codevs 1010 过河卒== 洛谷 1002
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 如图,A 点有一个过河卒,需要走到目标 B 点.卒行走规则:可以向下.或者向右.同 ...
- javascript 函数重载另一种实现办法
最近在读javascript忍者 感受下jquery作者 john Resig对于js的独到见解. 先上代码: function addMethod(object,name,fn){ var old ...
- .net压缩图片质量(附demo)
private void CompressedImage(string fileName, long quality) { FileStream fs = new FileStream(fileNam ...
- LeetCode OJ--Median of Two Sorted Arrays ***
http://oj.leetcode.com/problems/median-of-two-sorted-arrays/ 找两个有序数组的中位数,因为有序数组,并且复杂度要求O(lg(m+n))所以想 ...