经典算法面试题目-矩阵旋转90度(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度。 你能原地进行操作吗?(即不开辟额外的存储空间)

解答

我们假设要将图像逆时针旋转90度,顺时针是一个道理。如果原图如下所示:

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

那么逆时针旋转90度后的图应该是:

4 8 12 16
3 7 11 15
2 6 10 14
1 5 9 13

我们要如何原地进行操作以达到上面的效果呢?可以分两步走。 第一步交换主对角线两侧的对称元素,第二步交换第i行和第n-1-i行,即得到结果。 看图示:(如果是顺时针, 第一步交换/对角线两侧的对称元素,第二步交换第i行和第n-1-i行,即得到结果。)

原图:           第一步操作后:   第二步操作后:
1 2 3 4         1 5 9 13        4 8 12 16
5 6 7 8         2 6 10 14       3 7 11 15
9 10 11 12      3 7 11 15       2 6 10 14
13 14 15 16     4 8 12 16       1 5 9 13

顺时针90度与逆时针90度的代码如下:

#include <iostream>
using namespace std;

void swap(int *a, int *b){
    int t = *a;
    *a = *b;
    *b = t;
}
//这2个交换函数,选一个就行了,我只是为了演示它们实现的结果是一样
void swap2(int &a,int &b){
    int t = a;
    a = b;
    b = t;
}

//顺时针
void clockwise(int a[][4],int n){
     for(int i=0; i<n; ++i)
        for(int j=0; j<n-i; ++j)
            swap(a[i][j], a[n-1-j][n-1-i]);

    for(int i=0; i<n/2; ++i)
        for(int j=0; j<n; ++j)
            swap(a[i][j], a[n-1-i][j]);
}

//逆时针
void transpose(int a[][4], int n){
    for(int i=0; i<n; ++i)
        for(int j=i+1; j<n; ++j)
            swap(&a[i][j], &a[j][i]);
    for(int i=0; i<n/2; ++i)
        for(int j=0; j<n; ++j)
            swap(&a[i][j], &a[n-1-i][j]);
}
int main(){
    int a[4][4] = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12},
        {13, 14, 15, 16}
    };
    for(int i=0; i<4; ++i){
        for(int j=0; j<4; ++j)
            cout<<a[i][j]<<" ";
        cout<<endl;
    }
    cout<<endl;
    transpose(a, 4);
    cout<<"逆时针转90度"<<endl;
    for(int i=0; i<4; ++i){
        for(int j=0; j<4; ++j)
            cout<<a[i][j]<<" ";
        cout<<endl;
    }
    cout<<endl;
    a = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12},
        {13, 14, 15, 16}
    };
    for(int i=0; i<4; ++i){
        for(int j=0; j<4; ++j)
            cout<<a[i][j]<<" ";
        cout<<endl;
    }
    clockwise(a,4);
    cout<<endl;
    cout<<"顺时针转90度"<<endl;
    for(int i=0; i<4; ++i){
        for(int j=0; j<4; ++j)
            cout<<a[i][j]<<" ";
        cout<<endl;
    }
    return 0;
}

输出结果:

时间: 2024-09-14 10:20:32

经典算法面试题目-矩阵旋转90度(1.6)的相关文章

经典算法面试题目-置矩阵行列元素为0(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[i]和col[j]置为1. 第二次遍

经典算法面试题目-判断s2是否是s1的旋转字符串(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 rotation of &qu

经典算法面试题目-判断一个字符串中的字符是否唯一(1.1)

题目: Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures? 实现一个算法来判断一个字符串中的字符是否唯一(即没有重复).不能使用额外的数据结构. (即只使用基本的数据结构) 解答: 首先,你可以问面试官,构成字符串的字符集有多大?是ASCII字符,还是只是26个字母? 还是有更大的字符集,对于不同

经典算法面试题目-设计算法移除字符串中重复的字符(1.3)

题目 Design an algorithm and write code to remove the duplicate characters in a string without using any additional buffer. NOTE: One or two additional variables are fine. An extra copy of the array is not. FOLLOW UP Write the test cases for this metho

经典算法面试题目-判断两个字符串是否是变位词(1.4)

题目 Write a method to decide if two strings are anagrams or not. 写一个函数判断两个字符串是否是变位词. 解答 变位词(anagrams)指的是组成两个单词的字符相同,但位置不同的单词. 比如说, abbcd和abcdb就是一对变位词. 也就是说,2个字符串,不管排列顺序如何,只要全部的单个字符能对应上,就是一对变位词! 该题目有两种做法: 时间复杂度为O(nlogn)的解法 由于组成变位词的字符是一模一样的,所以按照字典序排序后,两

经典算法面试题目-翻转一个C风格的字符串(1.2)

题目: Write code to reverse a C-Style String. (C-String means that "abcd" is represented as five characters, including the null character.) 写代码翻转一个C风格的字符串.(C风格的意思是"abcd"需要用5个字符来表示,包含末尾的 结束字符) 解答: 这道题如果就是要考察你有没有注意到C风格字符串最后的那个结束符,那我觉得还是像书

c# 控件能旋转90度吗?

问题描述 c#控件能旋转90度吗?c#控件能旋转90度吗? 解决方案 解决方案二:这个好像不行...拖拽一把还是可以的...解决方案三:我想竖着写解决方案四:吧TextBox的形状拖成竖着的,宽度一个字符不就可以了?解决方案五:如果只是把长和宽的PX交换下用CSS解决解决方案六:引用1楼porschev的回复: 这个好像不行...拖拽一把还是可以的... 我想竖着写解决方案七:引用3楼llf94632525的回复: 吧TextBox的形状拖成竖着的,宽度一个字符不就可以了? 那DATAGRID呢

将位图旋转90度

本文将介绍如何将一张位图旋转90度.向工程添加一个Timage控件,取名为Image1. 工作原理是:创建一个位图缓冲区用于存储中间量,将原位图的每一行的像素转换为每一列然后存放在我们创建的位图缓冲区中.最后,将旋转后的位图从缓冲区存回原位图. //定义缓冲位图并剪切图形区域 Graphics::Tbitmap *bufferbitmap=new Graphics::Tbitmap(); bufferbitmap->Width=Image1->Height; bufferbitmap->

camera-截取的图片上传到服务器上的时候会旋转90度

问题描述 截取的图片上传到服务器上的时候会旋转90度 我做了一个安卓应用程序.在我的应用中,我要截取一个图片并把它发送到服务器上.在某种设备里,截取的图片上传到服务器上的时候会旋转90度.代码如下: Uri selectedImage = data.getData(); File imageFile = new File(selectedImage.toString()); ExifInterface exif; try { exif = new ExifInterface(imageFile.