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的更多相关文章

  1. C++ STL@ list 应用 (leetcode: Rotate Array)

    STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...

  2. [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  ...

  3. [LeetCode] Rotate List 旋转链表

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  4. [LeetCode] Rotate Image 旋转图像

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  5. LeetCode——Rotate List

    Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given  ...

  6. [LeetCode]Rotate Image(矩阵旋转)

    48. Rotate Image     Total Accepted: 69437 Total Submissions: 198781 Difficulty: Medium You are give ...

  7. [leetcode]Rotate List @ Python

    原题地址:https://oj.leetcode.com/problems/rotate-list/ 题意: Given a list, rotate the list to the right by ...

  8. [leetcode]Rotate Image @ Python

    原题地址:https://oj.leetcode.com/problems/rotate-image/ 题意: You are given an n x n 2D matrix representin ...

  9. 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 = ...

  10. [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 ...

随机推荐

  1. rn下的弹性布局

    重点: 1]react native 下的弹性布局名字叫:flexDirection 2]flexDirection的默认值是column而不是row,而flex也只能指定一个数字值. 3]使用fle ...

  2. rem 原理与简介

    一.rem 原理与简介 字体单位:     值根据 html 根元素大小而定,同样可以作为宽度,高度等单位 适配原理:     将 px 替换为 rem,动态修改 HTML 根元素的 font-siz ...

  3. php7 三元运算 精简

    $abc = isset($_GET['name'])?$_GET['name']:'abc'; // 5.+ $abcd = $_GET[

  4. 去除最后一个li的样式

    推荐::::方法一,使用:first-child    纯css的:first-child伪类就可以胜任此任务,操作很方便,代码量忽略不计.支持IE7+,不支持IE6 :first-child /:l ...

  5. XSSearch 说明文档保存

    XSSearch All Packages | 属性 | 方法(函数) 包 XS 继承关系 class XSSearch » XSServer » XSComponent 版本 1.0.0 源代码 s ...

  6. javaean(web作业)

    javabean简介: javabean是java语言开发的可重用组件,在jsp中使用javabean可以减少代码的重复量,使代码简洁. 他的优点: 1.可将HTML和Java代码分离,这主要是为了日 ...

  7. python之路(四)-set集合

    set集合 set是一个无序且不重复的元素集合优点:访问速度快,解决重复问题 l1 = [1,2,34,5,6,7,4,3,3,] s2 = set(l1) #可以以列表元祖等作为参数传进来,set集 ...

  8. Excel把数据存入共享字符串文件中并返回该字符串的下标

    public static int InsertSharedStringItem(string text, pkg.SharedStringTablePart shareStringPart) { i ...

  9. 转 tomcat+nginx+redis实现均衡负载、session共享(二)

    http://www.cnblogs.com/zhrxidian/p/5491285.html http://www.cnblogs.com/zhrxidian/p/5432886.html

  10. 补发————grid布局

    CSS Grid布局是CSS中最强大的布局系统.与flexbox的一位布局不同的是CSS Grid布局是一个二维布局系统,即它可以同时处理列和行.通过将CSS规则应用于父元素和其子元素,就可以轻松使用 ...