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


目录
相关文章
|
6月前
|
前端开发 API 开发者
给Web开发者的HarmonyOS指南02-布局样式
本系列教程适合鸿蒙 HarmonyOS 初学者,为那些熟悉用 HTML 与 CSS 语法的 Web 前端开发者准备的。
267 5
给Web开发者的HarmonyOS指南02-布局样式
|
7月前
|
安全
联邦学习潜在威胁
本文将联邦学习中的潜在威胁分为安全威胁和隐私威胁。安全威胁如数据投毒、女巫攻击等,影响完整性和可用性;隐私威胁如样本隐私泄露、模型提取攻击等,破坏机密性。不同阶段面临不同威胁:数据收集阶段有数据投毒、隐私泄露;训练阶段有模型投毒、推理攻击;推理阶段有对抗样本、模型提取攻击。
|
9月前
|
机器学习/深度学习 存储 人工智能
EfficientTAM:Meta AI推出的视频对象分割和跟踪模型
EfficientTAM是Meta AI推出的轻量级视频对象分割和跟踪模型,旨在解决SAM 2模型在移动设备上部署时的高计算复杂度问题。该模型采用非层次化Vision Transformer(ViT)作为图像编码器,并引入高效记忆模块,以降低计算复杂度,同时保持高质量的分割结果。EfficientTAM在多个视频分割基准测试中表现出与SAM 2相当的性能,具有更快的处理速度和更少的参数,特别适用于移动设备上的视频对象分割应用。
257 9
EfficientTAM:Meta AI推出的视频对象分割和跟踪模型
|
Docker 容器
使用rootfs制作docker容器镜像
使用rootfs制作docker容器镜像
|
运维 监控 网络架构
云服务的稳定性
【4月更文挑战第29天】云服务的稳定性
440 2
|
分布式计算 Hadoop
【细节拉满】Hadoop课程设计项目,使用idea编写基于MapReduce的学生成绩分析系统(附带源码、项目文件下载地址)(二)
【细节拉满】Hadoop课程设计项目,使用idea编写基于MapReduce的学生成绩分析系统(附带源码、项目文件下载地址)(二)
746 0
|
弹性计算 负载均衡 定位技术
阿里云服务器地域怎么选?地域选择四大因素考虑和建议
阿里云服务器地域怎么选?地域选择四大因素考虑和建议,阿里云服务器地域选择方法,如何选择速度更快、网络延迟更低的地域节点,地域指云服务器所在的地理位置区域,地域以城市划分,如北京、杭州、深圳及上海等,如何选择地域?建议根据用户所在地区就近选择地域,用户距离地域所在城市越近,网络延迟越低,速度越快。阿小云从速度延迟、备案限制、多产品内网互通、不同地域价格等四点因素来考虑地域的选择因素
|
搜索推荐 前端开发 JavaScript
毕业设计|基于SpringBoot+Vue的图书个性化推荐系统
毕业设计|基于SpringBoot+Vue的图书个性化推荐系统
251 0
|
安全 数据库
【Debian】配置aide入侵检测服务
基于debian系统。aide主要功能检测系统文件,当系统文件发生变化,如/etc/passwd文件出现差异,那么aide将会认为系统遭受入侵被增添用户
2409 0
|
资源调度 JavaScript
nuxt3目录及使用
# nuxt3 Nuxt 3 是一个开源框架,可以自动整合了很多东西,另外,nuxt3还是Vue3的同构ssr渲染的使用方案。csr,ssr,srg三种渲染方案三种渲染模式可以参考我的[都2023年了,还在用传统的前后端分离?来学习一下CSR,SSR与SRG吧!!! - 掘金 (juejin.cn)](https://juejin.cn/post/7195551013033164855 "https://juejin.cn/post/7195551013033164855")这篇文章。 今天我们来具体使用一下Nuxt,来体验一下这非常神奇的Nuxt开发。 # 初始化项目 我们的host