[LeetCode] Rotate Image [26]
题目
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?
解题思路
顺时针方向旋转数组90°。这个题也是个没啥意思的题,自己画绘图,找找规律。就出来了。我举一个n=4的样例还说明下规律:
通过图能够看出A[0][0] = A[3][0],....。从这些中我们能够找到例如以下规律:
A[i][j] = A[n-1-j][i];
A[n-1-j][i] = A[n-1-i][n-1-j];
A[n-1-i][n-1-j] = A[j][n-1-i];
A[j][n-1-i] = A[i][j];(原来的A[i][j]).
规律出来了,代码也就好写了,只是的考虑边界的情况。这个自己还得举个n为奇数的样例看看。
代码实现
class Solution {
public:
void rotate(vector<vector<int> > &A) {
int m = A.size();
if(m<=0) return ;
int n = A[0].size();
for(int i=0; i<m/2;++i){
for(int j=i; j<n-1-i; ++j){
int temp = A[i][j];
A[i][j] = A[n-1-j][i];
A[n-1-j][i] = A[n-1-i][n-1-j];
A[n-1-i][n-1-j] = A[j][n-1-i];
A[j][n-1-i] = temp;
}
}
}
};
另外,我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.
)
[LeetCode] Rotate Image [26]的更多相关文章
- C++ STL@ list 应用 (leetcode: Rotate Array)
STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...
- [LeetCode] Rotate Function 旋转函数
Given an array of integers A and let n to be its length. Assume Bk to be an array obtained by rotati ...
- [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 List
Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given ...
- [LeetCode]Rotate Image(矩阵旋转)
48. Rotate Image Total Accepted: 69437 Total Submissions: 198781 Difficulty: Medium You are give ...
- [leetcode]Rotate List @ Python
原题地址:https://oj.leetcode.com/problems/rotate-list/ 题意: Given a list, rotate the list to the right by ...
- [leetcode]Rotate Image @ Python
原题地址:https://oj.leetcode.com/problems/rotate-image/ 题意: You are given an n x n 2D matrix representin ...
随机推荐
- SQL Server 2008 对XML 数据类型操作
原文 http://www.cnblogs.com/qinjian123/p/3240702.html 一.前言 从 SQL Server 2005 开始,就增加了 xml 字段类型,也就是说可以直接 ...
- AOP概念
在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的 ...
- c语言_常见图片格式判断
c语言_常见图片格式判断 我想尽各种思路.今天,终于把图片判断搞定了. 在此,我写一下我的思路.希望对那些不想看代码的朋友们有帮助. 常风的的图片格式有:bmp,png,jpg,gif等图片格式. 我 ...
- uva10820 send a table (nlogn求1-n欧拉函数值模版
//重点就是求1-n的欧拉函数啦,重点是nlogn求法的版 //大概过程类似于筛选法求素数 #include<cstdio> #include<iostream> #inclu ...
- [Leetcode][Python]43: Multiply Strings
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 43: Multiply Stringshttps://leetcode.co ...
- js函数设计原则
一般认为函数指具有返回值的子程序,过程指没有返回值的子程序.C++中把所有子程序成为函数,其实那些返回值为void的 函数在语义上也是过程.函数与过程的区别更多是语义上的区别,而不是语法的区别. 语言 ...
- css系列教程--overflow min/maxheight content
outline:这只轮廓样式,与border类似.写法参考border. overflow/overflow-x/overflow-y:visible/hidden/scroll/auto/no-di ...
- eclipse自动提示类型的作用
eclipse的自动提示功能确实十分好用,但是只是笼统的都勾上了,所有会有好多重复项,看着很眼疼. 今天就稍微研究了下,略微知道了几个类型的作用: 序号 类型 大体的中文意思 作用 1 Java Ty ...
- 蜗牛爱课 -- iOS 设计模式之模板模式
1 前言 模板方法模式是面向对象软件设计中一种非常简单的设计模式.其基本思想是在抽象类的一个方法定义“标准”算法.在这个方法中调用的基本操作由子类重载予以实现.这个方法成为“模板”.因为方法定义的算法 ...
- C++ Primer之 十二章 类
1.关于类的const对象 const对象只能调用声明为const的成员函数,在这篇csdn博客中也讨论了这个问题. 究其原因是因为 const 对象A 调用了非const函数F, F因为没有cons ...