【leetcode】Rotate Image(middle)
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?
思路:我的思路,先沿对角线对称,再左右对称。
void rotate(int **matrix, int n) {
//先沿对角线对称
for(int i = ; i < n; i++)
{
for(int j = ; j < i; j++)
{
int tmp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = tmp;
}
}
//再左右对称
for(int c = ; c < (n + ) / ; c++)
{
for(int r = ; r < n; r++)
{
int tmp = matrix[r][c];
matrix[r][c] = matrix[r][n - c - ];
matrix[r][n - c - ] = tmp;
}
}
return;
}
大神一步到位的思路:找到每个圈要相互顺时针交换的4个位置,交换。
class Solution {
public:
void rotate(vector<vector<int> > &matrix) {
int n = matrix.size();
if(n<=) return;
for(int i = ; i!=n/;i++) //行 圈数
{
for(int j = i;j!=n--i;j++)
{
swap(matrix[i][j],matrix[j][n--i]);
swap(matrix[n--j][i],matrix[i][j]);
swap(matrix[n--i][n--j],matrix[n--j][i]);
}
}
}
inline void swap(int &a,int &b)
{
a = b+a;
b = a-b;
a = a-b;
}
};
【leetcode】Rotate Image(middle)的更多相关文章
- 【leetcode】Rotate List(middle)
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- 【leetcode】Reverse Integer(middle)☆
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...
- 【leetcode】Reorder List (middle)
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【leetcode】Partition List(middle)
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- 【leetcode】Spiral Matrix(middle)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 【leetcode】Next Permutation(middle)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 【leetcode】Reverse Bits(middle)
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- 【leetcode】Surrounded Regions(middle)☆
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
随机推荐
- python爬虫神器PyQuery的使用方法
你是否觉得 XPath 的用法多少有点晦涩难记呢? 你是否觉得 BeautifulSoup 的语法多少有些悭吝难懂呢? 你是否甚至还在苦苦研究正则表达式却因为少些了一个点而抓狂呢? 你是否已经有了一些 ...
- Win7与XP共享互相访问及共享注意事项!
win7共享方法和XP类似,主要需要检查以下操作: 1,首先将Guest账户打开 2,右击文件夹-属性-共享选项-高级共享 3,将共享文件√打上,应用-确定即可! 4,查看自己IP(开始-运行-cmd ...
- 浅谈checkpoint与内存缓存
事务日志存在检查点checkpoint,把内存中脏数据库写入磁盘,以减少故障恢复的时间,在此之前有必要提下SQL Server内存到底存放了哪些数据? SQL Server内存使用 对SQL Serv ...
- 【C++】递增递减操作符与指针的关系
可以将递增与递减操作符用于指针和基本变量,将递增操作符用于指针时,将把指针的值增加其指向的数据类型占用的字节数,这种规则适用于对指针递增和递减. int arr[5] = {21,32,23,45,3 ...
- springmvc 数据精准绑定
因为使用dwz 的lookup功能,回调的值通过name以 xxx.xxValue 来自动得到,而我还有些表单数据的name是没有前缀的, 到springmvc后台绑定的的话默认的绑定是有问题的.这是 ...
- hdu.1226.超级密码(bfs)
超级密码 Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 3.聚类–K-means的Java实现
K-means的步骤 输入: 含n 个样本的数据集,簇的数据K 输出: K 个簇 算法步骤: 1.初始化K个簇类中心C1,C2,---Ck (通常随机选择) 2.repeat 步骤3,4 3,将数据集 ...
- nginx反向代理、动静分离
环境:根据http://www.cnblogs.com/zzzhfo/p/6032095.html配置 方法一:根据目录实现动静分离 在web01创建image并上传一张图片作为静态页面 [root@ ...
- (一)css代码积累——自己经常忘记,但是总记不住的代码
1.透明度设置 90%透明:filter:alpha(opacity=90);-moz-opacity:0.90;-khtml-opacity: 0.90;opacity: 0.90; 80%透明:f ...
- iOS开发——高级篇——换肤、静态库
一.换肤 1.思路1> 解决方案1,使用颜色作为图片素材的命名关键字 问题1:要保证每套图片的文件名 颜色+ 名称.png的格式比较麻烦 问题2:如果要将某一个图片应用到其他皮肤不方便2> ...