LeetCode:Rotate Image
来源:互联网 发布:java 反射调用set方法 编辑:程序博客网 时间:2024/05/19 13:44
You are given an n*n 2D matrix representing an image.
Rotate the image by 90 degrees(clockwise).
Follow up: Could you do this in-place?
解题思路:
暴力法:从外到内一圈一圈地转,不过这个方法效率太低,比较慢。
分析法:首先沿着副对角线翻转一次,然后沿着水平中线翻转一次。
代码如下:
void rotate(vetor<vector<int>>& matrix) {const int n = matrix.size();for (itn i=0; i<n; ++i) {for (int j=0; j<n-i; ++j) { //沿着副对角线反转swap (matrix[i][j], matrix[n-1-j][n-1-i]);}}for (int i=0; i<n/2; ++i) { //沿着水平中线反转for (int j=0; i<n; ++j) {swap (matrix[i][j], matrix[n-1-i][j]);}}}
当然也可以先沿着水平中线翻转一次,然后沿着主对角线再翻转一次。
代码如下:
void rotate(vetor<vector<int>>& matrix) {const int n = matrix.size();for (int i=0; i<n/2; ++i) { //沿着水平中线反转for (int j=0; i<n; ++j) {swap (matrix[i][j], matrix[n-1-i][j]);}}for (itn i=0; i<n; ++i) {for (int j=i+1; j<n; ++j) { //沿着主对角线反转swap (matrix[i][j], matrix[j][i]);}}}
阅读全文
0 0
- LeetCode: Rotate Image
- [Leetcode] Rotate Image
- LeetCode : Rotate Image
- [LeetCode] Rotate Image
- leetcode 67: Rotate Image
- [leetcode] Rotate Image
- [Leetcode] Rotate Image
- [LeetCode]Rotate Image
- LeetCode-Rotate Image
- [leetcode] Rotate Image
- LeetCode - Rotate Image
- leetcode之Rotate Image
- LeetCode - Rotate Image
- Leetcode: Rotate Image
- LeetCode:Rotate Image
- Leetcode: Rotate Image
- Leetcode Rotate Image
- leetcode Rotate Image
- QQ登录.未能从程序集“RestSharp, Version=105.2.3.0, Culture=neutral, PublicKeyToken=null”中加载类型“RestSharp.OAuth
- 心有Java,细嗅Kotlin —— 细数 Kotlin 优雅的语法糖
- “玲珑杯”线上赛 Round #15 河南专场 C咸鱼魔法记
- 图像处理代码报错中断内存等问题解决策略 重点VS+opencv 兼顾其他 不定时更新
- 多线程-线程安全问题的产生原因分析以及同步代码块的方式解决线程安全问题
- LeetCode:Rotate Image
- 1012. 数字分类 (20)
- caffe matlab matcaffe 加载输入网络net时报错
- CentOS-7 常用配置命令
- 浙工大之江学院第0届校赛 持续补题
- R语言实战 聚类分析
- 观察者模式 —— 让你的对象知悉现况
- 单例模式
- 数据库的更新操作