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</ ...
随机推荐
- BZOJ2527 [Poi2011]Meteors 【整体二分 + 树状数组】
题目 Byteotian Interstellar Union有N个成员国.现在它发现了一颗新的星球,这颗星球的轨道被分为M份(第M份和第1份相邻),第i份上有第Ai个国家的太空站. 这个星球经常会下 ...
- [暑假集训--数位dp]LightOj1032 Fast Bit Calculations
A bit is a binary digit, taking a logical value of either 1 or 0 (also referred to as "true&quo ...
- 5whys分析法在美团工程师中的实践
转载美团博客:https://tech.meituan.com/5whys-method.html 前言 网站的质量和稳定性对于用户和公司来说至关重要,但是在网站的快速发展过程中,由于各种原因导致事故 ...
- JSON 序列化与弱类型
一.C#中JSON序列化有多种方式: 使用“DataContractJsonSerializer ”类时需要, 1.引用程序集 System.Runtime.Serialization 和 Syste ...
- FootAwesome字体图标
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- linux内核中打印栈回溯信息 - dump_stack()函数分析【转】
转自:http://blog.csdn.net/jasonchen_gbd/article/details/45585133 版权声明:本文为博主原创文章,转载请附上原博链接. 目录(?)[-] ...
- UVA - 10196:Check The Check
类型:简单模拟 大致题意:已知国际象棋行棋规则,给你一个局面,问是否将军?谁将谁的军?(保证不会同时将军) 思路:都以小写字母 测试 是否将 大写字母. 然后一个局面测两次(一次直接测,一次反转棋盘, ...
- AC日记——方格取数 洛谷 P1004
题目描述 设有N*N的方格图(N<=9),我们将其中的某些方格中填入正整数,而其他的方格中则放 人数字0.如下图所示(见样例): A 0 0 0 0 0 0 0 0 0 0 13 0 0 6 0 ...
- js-html音乐播放
<img src="images/music.png" id="music" class="rotate"> <audio ...
- react-native 判断是不是IPhone X
import { Platform, Dimensions } from 'react-native'; // iPhoneX const X_WIDTH = 375; const X_HEIGHT ...