【题目】

原文:

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度。 你能原地进行操作吗?(即不开辟额外的存储空间)

【分析】

点击打开链接

【代码一】

  1. /*********************************
  2. * 日期:2014-05-14
  3. * 作者:SJF0115
  4. * 题目: Rotate Image
  5. * 来源:CareerCup
  6. **********************************/
  7. #include <iostream>
  8. #include <algorithm>
  9. #include <vector>
  10. #include <string.h>
  11. using namespace std;
  12. //旋转图片
  13. void RotateImage(vector<vector<int> > &matrix){
  14. int i,j,temp;
  15. int N = matrix.size();
  16. // 沿着副对角线反转
  17. for(i = 0; i < N;i++){
  18. for(j = 0;j < N - i;j++){
  19. temp = matrix[i][j];
  20. matrix[i][j] = matrix[N - 1 - j][N - 1 - i];
  21. matrix[N - 1 - j][N - 1 - i] = temp;
  22. }
  23. }
  24. // 沿着水平中线反转
  25. for(i = 0; i < N / 2;i++){
  26. for (j = 0; j < N;j++){
  27. temp = matrix[i][j];
  28. matrix[i][j] = matrix[N - 1 - i][j];
  29. matrix[N - 1 - i][j] = temp;
  30. }
  31. }
  32. }
  33.  
  34. int main(){
  35. vector<int> row1 = {1,2,3};
  36. vector<int> row2 = {4,5,6};
  37. vector<int> row3 = {7,8,9};
  38. vector<vector<int>> matrix;
  39. matrix.push_back(row1);
  40. matrix.push_back(row2);
  41. matrix.push_back(row3);
  42. RotateImage(matrix);
  43. for(int i = 0;i < 3;i++){
  44. for(int j = 0;j < 3;j++){
  45. cout<<matrix[i][j]<<" ";
  46. }
  47. cout<<endl;
  48. }
  49. return 0;
  50. }

【代码二】

  1. /*********************************
  2. * 日期:2014-05-14
  3. * 作者:SJF0115
  4. * 题目: Rotate Image
  5. * 来源:CareerCup
  6. **********************************/
  7. #include <iostream>
  8. #include <algorithm>
  9. #include <vector>
  10. #include <string.h>
  11. using namespace std;
  12. //旋转图片
  13. void RotateImage(vector<vector<int> > &matrix){
  14. int layer,i,top;
  15. int n = matrix.size();
  16. //一层一层旋转
  17. for(layer = 0;layer < n/2;layer++){
  18. //第layer层第一个元素
  19. int first = layer;
  20. //第layer层最后一个个元素
  21. int last = n -1 - layer;
  22. for(i = first;i < last;i++){
  23. //偏移量
  24. int offset = i - first;
  25. top = matrix[first][i];
  26. //left-top
  27. matrix[first][i] = matrix[last-offset][first];
  28. //bottom-left
  29. matrix[last-offset][first] = matrix[last][last-offset];
  30. //right-bottom
  31. matrix[last][last-offset] = matrix[i][last];
  32. //top-right
  33. matrix[i][last] = top;
  34. }//for
  35. }//for
  36. }
  37.  
  38. int main(){
  39. vector<int> row1 = {1,2,3};
  40. vector<int> row2 = {4,5,6};
  41. vector<int> row3 = {7,8,9};
  42. vector<vector<int>> matrix;
  43. matrix.push_back(row1);
  44. matrix.push_back(row2);
  45. matrix.push_back(row3);
  46. RotateImage(matrix);
  47. for(int i = 0;i < 3;i++){
  48. for(int j = 0;j < 3;j++){
  49. cout<<matrix[i][j]<<" ";
  50. }
  51. cout<<endl;
  52. }
  53. return 0;
  54. }

CareerCup之1.6 Rotate Image的更多相关文章

  1. [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 ...

  2. Careercup | Chapter 1

    1.1 Implement an algorithm to determine if a string has all unique characters. What if you cannot us ...

  3. Canvas绘图之平移translate、旋转rotate、缩放scale

    画布操作介绍 画布绘图的环境通过translate(),scale(),rotate(), setTransform()和transform()来改变,它们会对画布的变换矩阵产生影响. 函数 方法 描 ...

  4. [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  ...

  5. [LeetCode] Rotate List 旋转链表

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  6. [LeetCode] Rotate Image 旋转图像

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  7. jQuery.rotate.js参数

    CSS3 提供了多种变形效果,比如矩阵变形.位移.缩放.旋转和倾斜等等,让页面更加生动活泼有趣,不再一动不动.然后 IE10 以下版本的浏览器不支持 CSS3 变形,虽然 IE 有私有属性滤镜(fil ...

  8. CSS3属性transform详解之(旋转:rotate,缩放:scale,倾斜:skew,移动:translate)

    CSS3属性transform详解之(旋转:rotate,缩放:scale,倾斜:skew,移动:translate)   在CSS3中,可以利用transform功能来实现文字或图像的旋转.缩放.倾 ...

  9. 偏移:translate ,旋转:rotate,缩放 scale,不知道什么东东:lineCap 实例

    <!DOCTYPE HTML> <head> <meta charset = "utf-8"> <title>canvas</ ...

随机推荐

  1. 【Luogu】P2254瑰丽华尔兹(堆优化DP)

    题目链接 我也不知道为什么脑子一抽就想了个堆优化……然后贼慢…… 因为上午听不懂wys的电音专场(快速傅立叶变换),然后就做了这么一道题. 首先朴素DP很sb都能秒出.就是枚举时刻.位置(两维)然后转 ...

  2. 用CSS给表格加边框

    很久之前,给表格加边框用的方法是给表格加上背景色,然后把cellspacing="1",再给td设置成另一种颜色,这样间接的加边框颜色. 一直没去细研究,今天发现了一种很简单的加边 ...

  3. HDU-1534 Schedule Problem

    四种约束条件..照做就行了.. 最长路建图. #include <cstdio> #include <cstdlib> #include <cstring> #in ...

  4. 【spring aop切面】基础使用教程

    package tpf.aspect; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFact ...

  5. 【CF1028B】Unnatural Conditions(构造)

    题意:给定n与m,要求构造两个长度不超过2230的数字x,y使得x,y各自的数位和>=n,x+y的数位和<=n 思路: #include<cstdio> #include< ...

  6. 【BZOJ1103】大都市meg(DFS序,树状数组)

    题意:有一颗树,1号点为根,保证编号小的点深度较小,初始状态每条边都没有被标记,要求实现两个操作在线: A:将连接x,y的边标记 W:查询从1到x的路径上有多少条边未被标记 n<=2*10^5 ...

  7. 汇编指令详解--as手册

    https://sourceware.org/binutils/docs/as/ Table of Contents 1 Overview 1.1 Structure of this Manual 1 ...

  8. github每次push提交都要输入账号密码

    问题产生的原因是在克隆的时候使用的是https的方式或者用一些特殊的指令来克隆的github项目源,如 golang里的go get github.com/...... 没次提交push的时候都会提示 ...

  9. Tomcat是怎么工作的(1) -- 开篇

    这是一个系列文章的第一篇. 标题还是费了点脑子才确定的,起什么名字比较好呢.Tomcat工作原理?深入浅出Tomcat运行机制?从零开始研究Tomcat?Tomcat是怎么运行起来的?Tomcat是如 ...

  10. AC日记——[SCOI2010]游戏 bzoj 1854

    1854: [Scoi2010]游戏 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 4938  Solved: 1948[Submit][Status] ...