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[ ...
随机推荐
- 最短路径:(Dijkstra & Floyd)
Dijkstra算法 1.定义概览 Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止.Di ...
- Xtreme8.0 - Magic Square 水题
Xtreme8.0 - Magic Square 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/ ...
- Linux下查看哪些IP登陆过系统/var/log/wtmp
last -f /var/log/wtmp
- 使用Docker中国官方镜像的加速地址
vi /etc/docker/daemon.json # 添加如下内容 { "registry-mirrors": ["https://registry.docker-c ...
- stm32f103串口实现映射功能
在实际开发中,常常遇到串口的默认输出IO口被其它模块占用了,所以我们要用到串口IO口映射功能.是指将原来实现功能的IO口映射到其它指定IO口,其它不变.详细操作例如以下: 先贴出默认下的串口初始化设置 ...
- MyEclipse使用总结——MyEclipse中配置WebLogic12c服务器
MyEclipse中配置WebLogic12c服务器的步骤如下: [Window]→[Preferences],如下图所示: 找到WebLogic的配置,如下图所示:
- C#获取文件夹及文件的大小与占用空间的方法
本文详细介绍了利用C#实现根据路径,计算这个路径所占用的磁盘空间的方法 . 网上有很多资料都是获取文件夹/文件的大小的.对于占用空间的很少有完整的代码.这里介绍实现这一功能的完整代码,供大家参考一下. ...
- ibatis.net:惯用法
使用<![CDATA[]]>保持SQL格式 IN 查询
- ExtJS 4.2 教程-04:数据模型
转载自起飞网,原文地址:http://www.qeefee.com/extjs-course-4-data-model ExtJS 4.2 教程-01:Hello ExtJS ExtJS 4.2 教程 ...
- DES、MD5、RSA加密算法
本篇主要是实现标题中的三个加密算法,至于机制大家自行百度吧. 一.DES 实现类:DES.java package com.kale.des; import java.security.SecureR ...