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;
}
时间: 2025-01-27 01:27:31

CareerCup之1.6 Rotate Image的相关文章

[LeetCode]61.Rotate List

[题目] Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. [题意] 给定一个链表,向右旋转k个位置,其中k是非负的. [分析] 先遍历一遍,得出链表长度 len,注意 k 可能大于

CCS3实例教程:rotate和rotateX

文章简介:这样也就可以解释为什么perspective为什么要放在transform的第一位了. 我觉得CCS3的transform中的学问很深,可以出不少面试题了,之前的文章谈到过perspective属性的位置问题,我们今天看看rotate的顺序,首先看看下面两个CSS3的keyframe动画: 两个动画的起始状态和结束状态都是是一摸一样的(其实就是原始位置),不同的只不过是rotate和rotateX的顺序.但是动画效果却有惊人的差别. CSS代码如下: 1 2 3 4 5 6 7 8 9

Delphi与DirectX之DelphiX(40): TDIB.Rotate();

本例效果图: 代码文件: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, DIB, StdCtrls; type TForm1 = class(TForm) DXPaintBox1: TDXPaintBox; Button1: TButton; Button2: TButton; procedure Button1Click

记一道有意思的算法题Rotate Image(旋转图像)

题出自https://leetcode.com/problems/rotate-image/ 内容为: 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? 简单的说就是给出一个n*n的二维数组,然后把这个数组进行90度顺时针旋转,而且不能使用额外的存储空间. 最初拿到这道题

CareerCup之2.2 寻找单链表倒数第n个元素

[题目] 原文: 2.2 Implement an algorithm to find the nth to last element of a singly linked list. 译文: 实现一个算法从一个单链表中返回倒数第n个元素. [分析] (1)创建两个指针p1和p2,指向单链表的开始节点. (2)使p2移动n-1个位置,使之指向从头开始的第n个节点.(意思是就是使p1和p2距离n个位置) (3)接下来检查p2 - > = = null 如果yes返回p1的值,否则继续移动p1和 p

random-g.rotate(-hudu, x, y);//为什么加减号就返回0位置了?

问题描述 g.rotate(-hudu, x, y);//为什么加减号就返回0位置了? Random random = new Random(); int x = 20; int y = 20; for(int i=0;i<4;i++){ // void rotate(double theta, double x, double y) // theta 弧度 // hudu = jiaodu * Math.PI / 180; // 获取正负30之间的角度 int jiaodu = random.

[LeetCode]189.Rotate Array

题目 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 this

CareerCup之1.8 字符串移位包含问题

[题目] 原文: 1.8 Assume you have a method isSubstring which checks if one word is a substring of another. Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to isSubstring ( i.e., "waterbottle" is a rotat

CareerCup之1.7 Set Matrix Zeroes

[题目] 原文: 1.7 Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0. 译文: 写一个函数处理一个MxN的矩阵,如果矩阵中某个元素为0,那么把它所在的行和列都置为0. [分析] [思路一] 遍历一次矩阵,当遇到元素等于0时,记录下这个元素对应的行和列. 可以开一个行数组row和列数组col,当元素a[i][j]等于0时, 就把row[