博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode 48. Rotate Image
阅读量:4565 次
发布时间:2019-06-08

本文共 1277 字,大约阅读时间需要 4 分钟。

You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Note:

You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.

Example 1:

Given input matrix = [  [1,2,3],  [4,5,6],  [7,8,9]],rotate the input matrix in-place such that it becomes:[  [7,4,1],  [8,5,2],  [9,6,3]]

Example 2:

Given input matrix =[  [ 5, 1, 9,11],  [ 2, 4, 8,10],  [13, 3, 6, 7],  [15,14,12,16]], rotate the input matrix in-place such that it becomes:[  [15,13, 2, 5],  [14, 3, 4, 1],  [12, 6, 8, 9],  [16, 7,10,11]]

模拟一下,先看四个角,每次交换两个,交换三次就可以了。对于左上角向下走一个单位找到a,左下角向右移动一个单位b,右下角向上移动c,右上角向左移动d。交换(a,b)(b,c),(c,d)....

具体见代码:

class Solution {public:    void rotate(vector
>& matrix) { int n = matrix.size(); if (n == 0) return ; int x = n/2; int y = n; for (int p = 0; p < x; ++p) { for (int i = 0; i <= y - 2; ++i) { swap(matrix[p+i][p], matrix[n-p-1][p+i]); swap(matrix[n-p-1][p+i], matrix[n-p-1-i][n-p-1]); swap(matrix[n-p-1-i][n-p-1], matrix[p][n-p-1-i]); } y -= 2; } }};

转载于:https://www.cnblogs.com/pk28/p/7714637.html

你可能感兴趣的文章
JDBC连接ORACLE无法登陆java.sql.SQLException: ORA-01017: invalid username/password; logon denied...
查看>>
团队-学生成绩管理-设计文档
查看>>
MySQL可供选择的存储引擎
查看>>
UITableView
查看>>
【SSM 7】Mybatis底层封装思路
查看>>
mysql默认字符集问题
查看>>
springmvc,mybatis,freemarker,maven-基于注解的整合
查看>>
Java--String 和StringBuilder、StringBuffer 的区别?
查看>>
js 获取浏览器可视窗口大小,滚动条高度
查看>>
通过LVM给Linux扩容
查看>>
audio进度条
查看>>
软件测试第四次作业
查看>>
翻译-现代浏览器的架构与发展
查看>>
OSGI框架的功能和设计思想
查看>>
c语言字符串操作,及常用函数
查看>>
【例4-4】最小花费
查看>>
平衡树之splay BZOJ3224 普通平衡树
查看>>
精妙的SQL语句
查看>>
关于git的submodule
查看>>
冒泡排序
查看>>