给定一个 n × n 的二维矩阵表示一个图像。
将图像旋转 90 度(顺时针)。
注意:
你必须在原矩阵中旋转图像,请不要使用另一个矩阵来旋转图像。
例 1:
给出的输入矩阵 =
[
  [1,2,3],
  [4,5,6],
  [7,8,9]
],
旋转输入矩阵,使其变为 :
[
  [7,4,1],
  [8,5,2],
  [9,6,3]
]
例 2:
给出的输入矩阵 =
[
  [ 5, 1, 9,11],
  [ 2, 4, 8,10],
  [13, 3, 6, 7],
  [15,14,12,16]
],
旋转输入矩阵,使其变为 :
[
  [15,13, 2, 5],
  [14, 3, 4, 1],
  [12, 6, 8, 9],
  [16, 7,10,11]
]
详见:https://leetcode.com/problems/rotate-image/description/

Java实现:

class Solution {
public void rotate(int[][] matrix) {
int n=matrix.length;
//沿左上至右下对角线,交换对称对
for(int i=0;i<n;++i){
for(int j=i+1;j<n;++j){
int tmp=matrix[i][j];
matrix[i][j]=matrix[j][i];
matrix[j][i]=tmp;
}
}
//水平翻转每一行
for(int i=0;i<n;++i){
for(int j=0;j<n/2;++j){
int tmp=matrix[i][j];
matrix[i][j]=matrix[i][n-1-j];
matrix[i][n-1-j]=tmp;
}
}
}
}

参考:http://www.cnblogs.com/grandyang/p/4389572.html

https://www.cnblogs.com/lightwindy/p/8564385.html

048 Rotate Image 旋转图像的更多相关文章

  1. Leetcode48. Rotate Image旋转图像

    给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像. 示例 1: 给定 m ...

  2. [LeetCode] Rotate Image 旋转图像

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

  3. Java for LeetCode 048 Rotate Image

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

  4. leetCode 48.Rotate Image (旋转图像) 解题思路和方法

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

  5. [Leetcode][048] Rotate Image 略详细 (Java)

    题目在这里 https://leetcode.com/problems/rotate-image/ [个人分析] 这个题目,我觉得就是考察基本功.考察细心的,算法方面没有太多东西,但是对于坐标的使用有 ...

  6. [leetcode]48. Rotate Image旋转图像

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

  7. Rotate Image 旋转图像

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

  8. 【LeetCode】048. Rotate Image

    题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...

  9. [LeetCode] 48. Rotate Image 旋转图像

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

随机推荐

  1. listen 66

    Frog Species Found in Big Apple Scientists discover new species all the time—on the order of 15,000 ...

  2. hdu-5804 Price List(水题)

    题目链接: Price List Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 262144/131072 K (Java/Othe ...

  3. 机器学习:朴素贝叶斯--python

    今天介绍机器学习中一种基于概率的常见的分类方法,朴素贝叶斯,之前介绍的KNN, decision tree 等方法是一种 hard decision,因为这些分类器的输出只有0 或者 1,朴素贝叶斯方 ...

  4. POI2014

    ...一个shabi和一堆神题的故事 今天只写了两道 之后随缘更吧 啊 顺便 snake我是不会更的 bzoj3829 POI2014 Farmcraft mhy住在一棵有n个点的树的1号结点上,每个 ...

  5. 【C++基础】重载,覆盖,隐藏

    函数签名的概念 函数签名主要包括1.函数名:2.参数列表(参数的个数.数据类型和顺序):但是注意,C++官方定义中函数签名不包括返回值!! 1.重载 函数重载是指在同一作用域内,可以有一组具有相同函数 ...

  6. 【LeetCode】063. Unique Paths II

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  7. C++ 创建文件的方法

    CString getPath(){ CTime time = CTime::GetCurrentTime(); CString t = time.Format(_T("%Y%m%d%H%M ...

  8. Python3解leetcode Maximum SubarrayClimbing Stairs

    问题: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cl ...

  9. 三 lambda表达式有什么用

    (转载: https://mp.weixin.qq.com/s/-PHOc6p-qKJBktle28AUgA) 一: 直接把代码块赋值给变量 我们知道,对于一个Java变量,我们可以赋给其一个“值”. ...

  10. Linux下使用doxygen+vim生成c语言源程序文档的方法

    1.安装 doxygen 有两种获得 doxygen 的方法.可以下载预编译的可执行文件,也可以从 SVN 存储库下载源代码并自己编译.清单 1 演示的是后一种方法. 清单 1. 安装和构建 doxy ...