Rotate Image leetcode java
题目:
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?
题解:
这道题就是考察很直白的旋转坐标。要in place的。画个图自己算算就出来了。
代码如下:
1 /* public void rotate(int[][] matrix) {
2 int m = matrix.length;
3 int n = matrix[0].length;
4
5 int[][] result = new int[m][n];
6
7 for(int i = 0; i<m; i++){
8 for(int j = 0; j<n; j++){
9 result[j][m-1-i] = matrix[i][j];
}
}
for(int i=0;i<m;i++){
for(int j=0; j<n; j++){
matrix[i][j] = result[i][j];
}
}
}
*/
//in place
public void rotate(int[][] matrix) {
int n = matrix.length;
for (int i = 0; i < n / 2; i++) {
for (int j = 0; j < Math.ceil(((double) n) / 2.); j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[n-1-j][i];
matrix[n-1-j][i] = matrix[n-1-i][n-1-j];
matrix[n-1-i][n-1-j] = matrix[j][n-1-i];
matrix[j][n-1-i] = temp;
}
}
Rotate Image leetcode java的更多相关文章
- Rotate List leetcode java
题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example: Gi ...
- N-Queens II leetcode java
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- LeetCode算法题-Rotate String(Java实现)
这是悦乐书的第317次更新,第338篇原创 在开始今天的算法题前,说几句,今天是世界读书日,推荐两本书给大家,<终身成长>和<禅与摩托车维修艺术>,值得好好阅读和反复阅读. 0 ...
- LeetCode算法题-Rotate Array(Java实现)
这是悦乐书的第184次更新,第186篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第43题(顺位题号是189).给定一个数组,将数组向右旋转k步,其中k为非负数.例如: ...
- Regular Expression Matching leetcode java
题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...
- Sqrt(int x) leetcode java
Reference: http://blog.csdn.net/lbyxiafei/article/details/9375735 题目: Implement int sqrt(int x). Co ...
- 189. Rotate Array - LeetCode
Question 189. Rotate Array Solution 题目大意:数组中最后一个元素移到第一个,称动k次 思路:用笨方法,再复制一个数组 Java实现: public void rot ...
- 796. Rotate String - LeetCode
Question 796. Rotate String Solution 题目大意:两个字符串匹配 思路:Brute Force Java实现: public boolean rotateString ...
- 48. Rotate Image - LeetCode
Question 48. Rotate Image Solution 把这个二维数组(矩阵)看成一个一个环,循环每个环,循环每条边,每个边上的点进行旋转 public void rotate(int[ ...
随机推荐
- iOS 11开发教程(十六)iOS11应用视图之删除空白视图
iOS 11开发教程(十六)iOS11应用视图之删除空白视图 当开发者不再需要主视图的某一视图时,可以将该视图删除.实现此功能需要使用到removeFromSuperview()方法,其语法形式如下: ...
- BZOJ.3257.树的难题(树形DP)
题目链接 状态只与黑.白两点的颜色有关,于是用 \(f[x][i][j]\)表示当前以x为根节点,有\(i\)个黑点\(j\)个白点,使得x子树满足该条件的最小花费. 最后答案就是 \(min\{f[ ...
- jquery开发表格插件项目之知识点累积 二
$.addClass() 增加样式 $.removeClass() 去除样式 $.toggleClass() 单击增加样式,再单击取消样式 $.hasClass() 判断是否存在样式,存在返回true ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) E. Tree Folding 拓扑排序
E. Tree Folding 题目连接: http://codeforces.com/contest/765/problem/E Description Vanya wants to minimiz ...
- URAL 1966 Cycling Roads 计算几何
Cycling Roads 题目连接: http://acm.hust.edu.cn/vjudge/contest/123332#problem/F Description When Vova was ...
- [原创]SpotLight性能监控工具使用介绍
[原创]SpotLight性能监控工具使用介绍 1 Spotlight工具是什么? SpotLight 是由Quest公司出品的一款第三方性能监控的图形化工具.SpotLight有一些的产品诸如可以 ...
- LOG EXPLORER ApexSQL Log
http://www.minisoft.cn/ https://www.apexsql.com/sql_tools_log.aspx
- 委托、Lambda表达式、事件系列04,委托链是怎样形成的, 多播委托, 调用委托链方法,委托链异常处理
委托是多播委托,我们可以通过"+="把多个方法赋给委托变量,这样就形成了一个委托链.本篇的话题包括:委托链是怎样形成的,如何调用委托链方法,以及委托链异常处理. □ 调用返回类型为 ...
- lufylegend:图形变形2
下面来详细讲解一下drawtriangles函数的使用方法.并且使用drawtriangles函数实现下面这种处理效果 因为这个方法是从AS3移植而来,所以它的使用方法和AS3基本一致,这里是AS3的 ...
- javascript 原型继承
因为javascript没有专门的机制去实现类,所以这里只能是借助它的函数能够嵌套的机制来模拟实现类.在javascript中,一个函数,可以包含变量,也可以包含其它的函数,那么,这样子的话,我们就可 ...