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;
}

【代码二】

CareerCup之1.6 Rotate Image,布布扣,bubuko.com

时间: 2024-11-01 02:11:12

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 method to rotate the image by 90 degrees. Can you do this in place? LeetCode中的原题,请参见我之前的博客Rotate Image 旋转图像.

Careercup | Chapter 1

1.1 Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures? 字符串问题,需要先确定是不是只有ASCII码. 如果是,可以用char[256],也可以用位向量.位向量的实现参照<编程珠玑>.i&MASK就是取余.i>>SHIFT就是取商. 1 class BitVector {

CSS3 transform 属性详解(skew, rotate, translate, scale)

写这篇文章是因为在一个前端QQ群里,网友 "小豆豆" (应他要求要出现他的网名......) ,问skew的角度怎么算,因为他看了很多文章还是不能理解skew的原理.于是,我觉得有必要写个博文,帮助那些不懂的人,让他们看了此文就懂. 进入正题: 先说明下,电脑屏幕的XY轴跟我们平时所说的直角坐标系是不一样的.如下图: 图上的盒子就是代表我们的电脑屏幕,原点就是屏幕的左上角,竖直向下为X轴正方向,水平向右为Y轴正方向. 1.倾斜skew 先看图 每个图下方都有skew的参数.粗的红色的线

Leet Code OJ 189. Rotate Array [Difficulty: Easy]

题目: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note: Try to come up as many solutions as you can, there are at least 3 different ways to solve thi

leetcode 【 Rotate Image 】python 实现

题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up:Could you do this in-place? 代码:oj测试通过 Runtime: 53 ms 1 class Solution: 2 # @param matrix, a list of lists of integers 3 # @return a list

Rotate Array

LeetCode 189 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note:Try to come up as many solutions as you can, there are at least 3 different ways to s

CSS3 skew倾斜、rotate旋转动画

css3出现之前.我们实现一个对象的一组连续动画须要通过JavaScript或Jquery编写,脚本代码较为复杂: 若须要实现倾斜.旋转之类的动画难度将更高(我还没试过用JavaScript或Jquery怎样实现),并且即使能实现预计花的时间代价及维护难度是非常大的,非常多时候仅仅能依靠绘图工具制作此类动画文件: 有时候在想假设不用脚本语言,也不用绘图工作制作动画文件.就能在网页上实现倾斜.旋转之类的动画效果多好. 近期挤出一些业余时间学习CSS3,当中就包括非常多动画演示样例,花了点时间学习和

Rotate Image

参照计算机图形学图形变换即可. public class Solution { public void rotate(int[][] matrix) { if(matrix.length<=1)return ; float dip=(float) ((matrix.length-1)/2.0); int[][] res=new int[matrix.length][matrix.length]; for(int i=0;i<matrix.length;i++) for(int j=0;j<

How to Rotate Tomcat catalina.out

If catalina.out becomes 2GB in size, tomcat crashes and fails to start without any error message. To avoid this scenario you should rotate catalina.out frequently. This article describes how to setup auto rotation of catalina.out on a linux/unix mach