【题解】【矩阵】【回溯】【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 ...
随机推荐
- svn resolve/merge
svn merge http://svn.a.com/branches/20150129_168954_sales-impr_1 svn resolve --accept working web/sr ...
- 使用AIDL将接口暴露给客户端(远程绑定Service)
import java.util.Timer;import java.util.TimerTask; import jww.mediaprovidertest.ICat.Stub;import and ...
- [Js]面向对象基础
一.什么是对象 对象是一个整体,对对外提供一些操作 二.什么是面向对象 使用对象时,只关注对象提供的功能,不关注其内部细节,比如Jquery 三.Js中面向对象的特点 1.抽象:抓住核心问题 2.封装 ...
- C#获取指定日期为一年中的第几周
/// <summary> /// 获取指定日期,在为一年中为第几周 /// </summary> /// <param name="dt">指 ...
- 继承自NSObject的类不能用CGRect
我用的是Xcode6.2. 系统默认没有pch文件. 所以没有自动导入UIKit包. 我在继承NSObject类里也不能用CGRect或者UI开头的控件,原因也是Xcode6.2以后版本 缺少UIKi ...
- PowerShell工具脚本---按行数切割大文本文件
我编写的PowerShell工具脚本,[按行数切割大(文本)文件],生成n个小文件. 主要目的是为了能够让excel快速处理.或用脚本并发处理文本. 注意: 1 如果有必要,你可以先用其他工具,把大文 ...
- Linux Lab and project latest
samba : start your samba service netlogon syslog vi /usr/local/samba vi /usr/samba/etc/smb.conf smbc ...
- Apache Ant运行时Unable to locate tools.jar解决方法
下载Apache Ant 一.解压ant安装包在D:\ant下 二.环境变量配置 ANT_HOME D:\ant\apache-ant-1.9.0 CLASSPATH ;%ANT_HOME%lib; ...
- RPI学习--环境搭建_刷卡+wiringPi库安装
1,镜像地址 http://www.raspberrypi.org/downloads/ 2,Windows下刷写工具 Win32 Disk Imager 3,安装wiringPi库 (这里在连网状态 ...
- SharePoint 2013 Nintex Workflow 工作流帮助(一)
博客地址 http://blog.csdn.net/foxdave 接下来一段时间的内容中,我们基于SharePoint 2013来了解一下Nintex Workflow的具体内容. 之前的几篇由于之 ...