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 ...
随机推荐
- RPA基础
RPA是什么 软件机器人 RPA是基于计算机操作系统的工作界面,自动识别UI,完成预先设定的工作流程的软件机器人 全自动 自动的操作整个工作流程,用软件的方式代替人力,完成大量的重复性的手工操作, ...
- 20175229张智敏 Arrays和String单元测试
Arrays和String单元测试 1.具体要求: 在IDEA中以TDD的方式对String类和Arrays类进行学习 测试相关方法的正常,错误和边界情况 String类 charAt split A ...
- expdp错误案例
转自:https://www.cnblogs.com/kerrycode/p/3960328.html Oracle数据泵(Data Dump)使用过程当中经常会遇到一些奇奇怪怪的错误案例,下面总结一 ...
- python3--迭代
判断一个对象是否能够进行迭代的方法 Iterable from collections import Iterable dict = {'name':'Joe','age':17} print (is ...
- 关于QList<T>的内存释放
当T为指针类型时,List.clear()不能释放其内存,需加上qDeleteAll()函数, //class Person ---> Person(int id_,QString name_) ...
- js的window.open()改写
说明:window.open(url,"_blank")方法替换如下: function openUrl(url) { try { if (/MSIE\s*(\d+\.\d+);/ ...
- Pip无法卸载某些包:Cannot uninstall 'PyYAML'.
查找了很多资料,最终还是手动删除吧: 注意如果你有火萤酱或everything等外部索引的,来搜索如图PyYAML的进行删除,可能删不干净 建议最后在你的anaconda路径下或者python路径下在 ...
- 03 of learning python
01 input输入的是str类型 如果输入的是数字的话,要记得强制转换一下! 02 isdigit() 这个方法是用来检测字符串是否全部由数字组成 str.isdigit() 如果字符串只包含数字则 ...
- iOS TouchID & FaceID
import UIKit import LocalAuthentication //指纹识别必须用真机测试,并且在iOS8以上系统,如果是FaceID至少IOS11以上. class Authenti ...
- java获取当前日期的前一天和后一天
/** * 获得指定日期的前一天 * @param specifiedDay * @return * @throws Exception */ public static String getSpec ...