leetcode解题方案--048--Rotate Image
来源:互联网 发布:淘宝高仿男鞋店铺 编辑:程序博客网 时间:2024/05/17 21:53
题目
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Note:
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
分析
旋转操作很简单,找到规律就可以啦。
别的。。。突然发现题目中不让申请其他数组空间。而我的思路是dfs。就是利用dfs进行遍历,申请了marked数组来标记已经旋转过的位置,应该是不对的。
另外就是用数学方法计算到底怎样才能不多旋转,也非常简单。按框框来算,每一个框框只需要旋转(n-1)*4次就可以啦
class Solution { public static void rotate(int[][] xx) { int n = xx.length; boolean[][] marked = new boolean[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (marked[i][j] == true) { continue; } int newValue = xx[i][j]; dfs(xx, j, n -1 - i, newValue, marked); } } } public static void dfs(int[][] xx, int x, int y, int value, boolean[][] marked) { int newValue = xx[x][y]; xx[x][y] = value; marked[x][y] = true; if (!marked[y][xx.length - 1-x]) { dfs(xx, y, xx.length - x-1, newValue, marked); } }}
阅读全文
0 0
- leetcode解题方案--048--Rotate Image
- [LeetCode] Rotate Image 解题报告
- LeetCode 048 Rotate Image
- LeetCode(048) Rotate Image
- LeetCode 048 Rotate Image
- leetcode 048 Rotate Image
- [LeetCode]048-Rotate Image
- LeetCode 048 Rotate Image
- [leetcode] 48. Rotate Image 解题报告
- Leetcode解题报告:48. Rotate Image
- [Leetcode] 48. Rotate Image 解题报告
- LeetCode 48. Rotate Image 解题报告
- leetCode 48.Rotate Image (旋转图像) 解题思路和方法
- Leetcode #48. Rotate Image 图像旋转 解题报告
- leetcode 048 —— Rotate Image
- leetcode之路048 Rotate Image
- LeetCode: Rotate Image
- [Leetcode] Rotate Image
- 怎么利用qq来获取好友ip地址
- 页面引入dialog
- 15个学习习惯,受益一生
- 从零开始:在树莓派上构建tensorflow——详细至极
- 我的学习记录47
- leetcode解题方案--048--Rotate Image
- Mycat 安装
- 纯干货,Spring-data-jpa详解,全方位介绍。
- (三十五)沉浸式设计以及兼容
- java多线程(5)死锁
- Jdbc连接数据库(JDBC读取propertites属性文件)
- myeclipse2017CI的SVNE170001错误
- 3D打印技术与应用
- ML--SVM学习小结(一)