【题解】【矩阵】【回溯】【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 ...
随机推荐
- centos 5.8 64位系统安装 mysql5.6
mysql5.5以上的版本编译需要 cmake 1 .安装cmake wget http://www.cmake.org/files/v2.8/cmake-2.8.10.2.tar.g ...
- Struts+Spring+Hibernate整合入门详解
Java 5.0 Struts 2.0.9 Spring 2.0.6 Hibernate 3.2.4 作者: Liu Liu 转载请注明出处 基本概念和典型实用例子. 一.基本概念 St ...
- OkHttp使用全解析(转)。
Android系统提供了两种HTTP通信类,HttpURLConnection和HttpClient.关于HttpURLConnection和HttpClient的选择>>官方博客尽管Go ...
- [Js]Ajax
一.什么是Ajax 不刷新的情况下读取数据或提交数据 (最早出现ajax:谷歌地图,拖动一下出现一片新的视野) 应用:用户注册.在线聊天.微博 特性:只能从服务器上去读取数据(所以我们需要配置自己的服 ...
- Windows下将程序打包为安装包(最为简易的方式)
一.准备工作:先下载一个Inno Setup编译器,这里我用到的是5.3.3中文版的. 软件介绍: Inno Setup 是一个免费的安装制作软件,小巧.简便.精美是其最大特点,支持pascal脚本, ...
- cmd的xcopy命令
C#项目的PostEvent里经常会用到xcopy命令,复制目录时容易出错,如下: xcopy sourceDir targetDir,其中的2个目录最后不能有反斜杠"",而目录类 ...
- K2 如何和 Java 做整合?
本文内容来自K2社区 问题:我们清楚K2 产品是基于.net 平台,我们有需求要将Java平台的表单和K2进行整合,使用K2.可以有什么方案建议? 专家解答: 这个需求也是比较常见的,以下是我的一些经 ...
- Xceed WPF 主题皮肤控件Xceed Professional Themes for WPF详细介绍
Xceed Professional Themes for WPF是一款为你的整个应用程序提供完美WPF主题风格的控件,包含Office 2007和Windows 7,可以应用到任何微软官方的WPF控 ...
- DotNetBar v12.6.0.4 Fully Cracked
更新信息: http://www.devcomponents.com/customeronly/releasenotes.asp?p=dnbwf&v=12.6.0.4 如果遇到破解问题可以与我 ...
- Ajax get方法 IE 下乱码
每个浏览器处理编码的格式不同. ajax使用utf-8来编码发送数据,ie在发送时并没加上charset=utf-8,从而导致乱码(IE默认使用iso-8859-1编码) JavaScript代码: ...