【题解】【矩阵】【回溯】【Leetcode】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?
思路:
每次转着圈挪四个元素,这步只需要一个int的额外空间,想象一个方阵

哦不好意思,这个太不专业了,咱换个大一点6×6的

草图上比划比划第二层的元素怎么移动,一次Accept的感觉好爽
代码:
void rotate(vector<vector<int> > &matrix) {
int n = matrix.size();
if(n < ) return;
for(int layer = ; layer < n/; layer++){
for(int i = layer; i < n--layer; i++){//避免重复处理n-1-layer
int leftTop = matrix[layer][i];//注意二维矩阵的(i,j)index与数学坐标(x,y)中是相反的
matrix[layer][i] = matrix[n--i][layer]; //先在纸上想清楚赋值的先后循序
matrix[n--i][layer] = matrix[n--layer][n--i];
matrix[n--layer][n--i] = matrix[i][n--layer];
matrix[i][n--layer] = leftTop;
}
}
}
【题解】【矩阵】【回溯】【Leetcode】Rotate Image的更多相关文章
- C++ STL@ list 应用 (leetcode: Rotate Array)
STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...
- [LeetCode]Rotate Image(矩阵旋转)
48. Rotate Image Total Accepted: 69437 Total Submissions: 198781 Difficulty: Medium You are give ...
- 【LeetCode】【矩阵旋转】Rotate Image
描述 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise ...
- [LeetCode] Rotate Image n-by-n矩阵顺时针旋转
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- [leetcode]Rotate Image @ Python
原题地址:https://oj.leetcode.com/problems/rotate-image/ 题意: You are given an n x n 2D matrix representin ...
- [LeetCode] Rotate Array 旋转数组
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- [LeetCode] Rotate List 旋转链表
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- [LeetCode] Rotate Image 旋转图像
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- LeetCode Rotate Image (模拟)
题意: 将一个n*n的矩阵顺时针旋转90度. 思路: 都是差不多的思路,交换3次也行,反转再交换也是行的. class Solution { public: void rotate(vector< ...
- LeetCode——Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given ...
随机推荐
- 在vim中执行外部命令
11.7.5 在Vim编辑器中执行Shell命令 有时需要在Vim编辑器中执行Shell命令,例如需要验证一个Shell命令是否正确,以便写入脚本中:需要在文件中引用某个Shell命令的输入等.本小 ...
- tomcat项目发布 更改小猫图标 及自定义错误404界面
tomcat发布项目的时候遇到些小问题 不过解决了 问题1. 整个服务器的404自定义界面问题 解决方法: 在tomcat安装目录下conf中web.xml中修改配置文件 <error-page ...
- 碰到sshd连接不上linux时的解决办法
1,首先更改ssh配置,可以是ssh端口连接不上服务器 cd /etc/ssh/sshd_config vi !$ Port 52113 //ssh默认的连接端口, 改为别人不知道的端口 Permit ...
- Flickr 网站架构分析
Flickr 网站架构分析 Flickr.com 是网上最受欢迎的照片共享网站之一,还记得那位给Windows Vista拍摄壁纸的Hamad Darwish吗?他就是将照片上传到Flickr,后而被 ...
- js基础之弹性运动(四)
一.滑动菜单.图片 var iSpeed=0;var left=0;function startMove(obj,iTarg){ clearInterval(obj.timer);//记得先关定时器 ...
- Android打开新的Activity并同时关闭当前Activity
Intent it = new Intent(); it.setClass(EditActivity.this, MainActivity.class); it.setFlags(Intent.FLA ...
- 前端CSS编程之道-LESS
由于前端css编写繁琐,最近开始学习LESS,用LESS编写文件.less文件可以直接编译成我们要的.css文件 学习Less 我下面是我练习时的截图,希望小伙伴也能动手自己写一下,而不是复制粘贴模式 ...
- iOS多线程之NSThread使用
iOS中的多线程技术 我们在iOS开发项目过程中,为了解决UI界面操作不被耗时操作阻塞,我们会使用到多线程技术.在iOS开发中,我们主要会用到三种多线程操作技术:NSThread,NSOperatio ...
- Julia中文教程资源.txt
Julia中文教程资源.txt 2016年3月28日 05:18:32 codegay 本文更新在这里: https://github.com/FGFW/julia-science-and-techn ...
- 指针属性直接赋值 最好先retain 否则内存释放导致crash
//先释放之前的 YK_RELEASE_SAFELY(_selectedDate); //retain新的 _selectedDate = [aDate retain]; NSString 属性的好像 ...