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 ...
随机推荐
- IOS 视频.图片上传服务器
//上传视频 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.requestSerializer. ...
- python线程——创建和启动
可以通过实例化一个threading.Thread()对象来创建子线程. import threading import time def listen_music(num): print(" ...
- Win10专业版激活
转载来自:http://www.zhuangjiba.com/bios/3432.html 如何激活win10正式版图文解说 打开开始菜单,找到设置,点开“更新和安全”,切换到“激活”选项卡,查看到当 ...
- EmEditor注册码
Emeditor 是一款非常好用的文本编辑器 Emeditor 注册码 姓 名:ttrar.com 序 列 号:DKAZQ-R9TYP-5SM2A-9Z8KD-3E2RK 姓 名:whyida 序 列 ...
- 与我们息息相关的internet服务(2)---WWW服务
在起步一个公司,从组建的技术上,可能要准备很多东西,其中一个就是我们熟悉的公司网站 网站,在初中,那时浏览一个网页可叫网上冲浪,听起来似乎比洗澡还爽快,可现在这词就是土鳖,网上冲浪火起来主要是应 ...
- 20145232韩文浩 《网络对抗技术》 Web基础
Apache 因为端口号80已经被占用(上次实验设置的),所以先修改/etc/apache2/ports.conf里的端口为5232后重新开启 可以在浏览器中输入localhost:5208来检查是否 ...
- JMS Cluster modules
是GeoServer实现集群还是在数据库实现集群? Hadoop.Spark.HBase与Redis的适用性见解:https://blog.csdn.net/cuiyaonan2000/article ...
- vue的指令
我之前学了学angular 发现angular和vue的指令有点类似 先说一下 new Vue({ el: "#box", // element(元素) 当前作 ...
- Linux-3.0.8中基于S5PV210的IRQ模块代码追踪和分析
init/main.c: asmlinkage void start_kernel(void) { ...... early_irq_init(); init_IRQ(); ...... } earl ...
- 快速入门Vue
前端技术发展很快,近日一个项目中想用Vue框架,对此对Vue基础进行了一些学习整理 何为Vue,官网 解释Vue.js(读音 /vjuː/,类似于 view) 是一套构建用户界面的渐进式框架 这里记录 ...