leetcode — rotate-image
import java.util.Arrays;
/**
* Source : https://oj.leetcode.com/problems/rotate-image/
*
* Created by lverpeng on 2017/7/18.
*
* 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?
*
*/
public class RotateImage {
/**
* 将二维数组顺时针旋转90度
*
* 找到旋转前后的规律
*
* @param matrix
* @return
*/
public int[][] rotate (int[][] matrix) {
if (matrix.length == 0) {
return matrix;
}
int[][] rotatedMatrix = new int[matrix[0].length][matrix.length];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
rotatedMatrix[j][i] = matrix[matrix.length - 1 - i][j];
}
}
return rotatedMatrix;
}
/**
* 如果矩阵是行列数相等
* 旋转矩阵,,矩阵有四条边,相当于四条边上的元素互换位置,1号换到2号,2号换到3号,3号换到4号,4号换到1号
*
*
* @param matrix
*/
public void rotate1 (int[][] matrix) {
if (matrix.length == 0) {
return;
}
int length = matrix.length;
for (int i = 0; i < length / 2; i++) {
for (int j = i; j < length - i -1 ; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[length -1 -j][i];
matrix[length -1 -j][i] = matrix[length - 1 - i][length - 1 - j];
matrix[length - 1 - i][length - 1 - j] = matrix[j][length - 1 - i];
matrix[j][length - 1 - i] = temp;
}
}
}
public static void printMatrix (int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
System.out.println(Arrays.toString(matrix[i]));
}
System.out.println();
}
public static void main(String[] args) {
RotateImage rotateImage = new RotateImage();
int[][] matrix = new int[][]{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int[][] matrix1 = new int[][]{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{10, 11, 12}
};
int[][] matrix2 = new int[][]{
{1, 2, 3},
{4, 5, 6}
};
int[][] matrix3 = new int[][]{
{1, 2, 3}
};
int[][] matrix4 = new int[][]{
{1},
{4},
{5}
};
int[][] matrix5 = new int[][]{};
int[][] matrix6 = new int[][]{{},{}};
printMatrix(rotateImage.rotate(matrix));
printMatrix(rotateImage.rotate(matrix1));
printMatrix(rotateImage.rotate(matrix2));
printMatrix(rotateImage.rotate(matrix3));
printMatrix(rotateImage.rotate(matrix4));
printMatrix(rotateImage.rotate(matrix5));
printMatrix(rotateImage.rotate(matrix6));
System.out.println("========rotate1========");
int[][] matrix7 = new int[][]{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
rotateImage.rotate1(matrix7);
printMatrix(matrix7);
int[][] matrix8 = new int[][]{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}
};
rotateImage.rotate1(matrix8);
printMatrix(matrix8);
int[][] matrix9 = new int[][]{
{1}
};
rotateImage.rotate1(matrix9);
printMatrix(matrix9);
}
}
leetcode — rotate-image的更多相关文章
- C++ STL@ list 应用 (leetcode: Rotate Array)
STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...
- [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 ...
- 2016.5.16——leetcode:Rotate Array,Factorial Trailing Zeroe
Rotate Array 本题目收获: 题目: Rotate an array of n elements to the right by k steps. For example, with n = ...
- [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 ...
随机推荐
- mysql自定义时间段分组
说明:一下是自定义7天为一个时间段的分组统计 select datediff(ms_time, '开始时间') div 7 as time , count(*) count,sum(money) mo ...
- Flink 报错 "Could not find a suitable table factory for 'org.apache.flink.table.factories.StreamTableSourceFactory' in the classpath"
先上代码: table = tablexx.select('*).tablexx.groupBy('x).select('x, xx.count ) tableEnvironment // decla ...
- PEM证书加密方法(python)
1. 常见网络登录现在都使用了rsa加密,一般而言客户会提供模(mo)和指数(e). 2. 通过模和指数获取到证书PEM(方法见:https://www.cnblogs.com/luo30zhao/p ...
- springmvc 整合Controller出现实例化两次问题
启动项目的时候,发现初始化控制层的时候,初始化(使用构造方法打印日志的方式)了两次的情况. 后来检查配置: <context:component-scan base-package=" ...
- uboot中获取dts资源并操作gpio口
设备树(DTS)的引入减少了内核为支持新硬件而需要的改变,提高代码重用,使得单个内核镜像能支持多个系统. DTS作为U-Boot 和Linux 内核之间的动态接口,能够有效减少重复配置,共享于U ...
- gulp使用入门
介绍:Gulp 是基于node.js的一个前端自动化构建工具,可以使用它构建自动化工作流程(前端集成开发环境):不仅能对网站资源进行优化,而且在开发过程中很多重复的任务能够使用正确的工具自动完成,大大 ...
- 搭建node js的运行环境。
第一步:首先安装一个NVM,就是一个node的版本管理器. nvm的下载地址::https://github.com/coreybutler/nvm-windows/releases,我选择下载的是n ...
- CENTOS7常用的基础命令集合(一)
目录(?)[-] CentOS7 常用命令集合 常用命令 文件与目录操作 查看文件内容 文本内容处理 查询操作 压缩解压 yum安装器 网络相关 系统相关 系统服务启动相关 防火墙相关 RPM包管理 ...
- [转]Kaldi语音识别
转:http://ftli.farbox.com/post/kaldizhong-wen-shi-bie Kaldi语音识别 1.声学建模单元的选择 1.1对声学建模单元加入位置信息 2.输入特征 3 ...
- 利用Swashbuckle生成Web API Help Pages
利用Swashbuckle生成Web API Help Pages 本文将通过Swagger的.NET Core的实现封装工具Swashbuckle来生成上一篇-<创建ASP.NET Core ...