leetcode - 48. Rotate Image - Medium

descrition

You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Note:

You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.


Example 1 : Given input matrix =
[
[1,2,3],
[4,5,6],
[7,8,9]
], rotate the input matrix in-place such that it becomes:
[
[7,4,1],
[8,5,2],
[9,6,3]
] Example 2: Given input matrix =
[
[ 5, 1, 9,11],
[ 2, 4, 8,10],
[13, 3, 6, 7],
[15,14,12,16]
], rotate the input matrix in-place such that it becomes:
[
[15,13, 2, 5],
[14, 3, 4, 1],
[12, 6, 8, 9],
[16, 7,10,11]
]

解析

参见代码。小技巧:矩阵的对角线可以唯一确定一个矩阵。

code

#include <iostream>
#include <vector>
#include <algorithm> using namespace std; class Solution{
public:
void rotate(vector<vector<int> >& matrix){
//rotateNonInplace(matrix);
rotateNonInplace(matrix);
}
/*
(si,sj) ** (si,sj+k) *** (si,ej)
* *
* *
* (si+k,ej)
(ei-k,sj) *
* *
* *
(ei,sj)*** (ei,ej-k) ** (ei,ej)
*/
// time-O(n^2), space-O(1)
void rotateInplace(vector<vector<int> >& matrix){
int n = matrix.size();
int si = 0, sj = 0; // the top-left corner
int ei = n-1, ej = n-1; // the down-right corner
// (si, sj), (ei, ej) while(si <= ei && sj<=ej){
for(int k=0; sj+k<=ej; k++){
int temp = matrix[si][sj+k];
matrix[si][sj+k] = matrix[ei-k][sj];
matrix[ei-k][sj] = matrix[ei][ej-k];
matrix[ei][ej-k] = matrix[si+k][ej];
matrix[si+k][ej] = temp;
}
si++;
sj++;
ei--;
ej--;
}
} // time-O(n^2), space-O(n^2)
void rotateNonInplace(vector<vector<int> >& matrix){
int n = matrix.size();
vector<vector<int> > assit(n, vector<int>(n, 0)); // put the i-row in matrix to (n-1-i)-column in assit
for(int i=0; i<n; i++){
for(int k=0; k<n; k++){
assit[k][n-1-i] = matrix[i][k];
}
} // copy assit to matrix
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
matrix[i][j] = assit[i][j];
}
}
}
}; int main()
{
return 0;
}

[array] leetcode - 48. Rotate Image - Medium的更多相关文章

  1. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  2. [array] leetcode - 31. Next Permutation - Medium

    leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...

  3. LeetCode 48. 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 旋转图像

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

  5. LeetCode 48 Rotate Image(2D图像旋转问题)

    题目链接: https://leetcode.com/problems/rotate-image/?tab=Description   Problem:给定一个n*n的二维图片,将这个二维图片按照顺时 ...

  6. [leetcode 48] rotate image

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

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

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

  8. [array] leetcode - 54. Spiral Matrix - Medium

    leetcode-54. Spiral Matrix - Medium descrition GGiven a matrix of m x n elements (m rows, n columns) ...

  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. input[type='file']样式美化及实现图片预览

    前言 上传图片是常见的需求,多使用input标签.本文主要介绍 input标签的样式美化 和 实现图片预览. 用到的知识点有: 1.input标签的使用 2.filelist对象 和 file对象 3 ...

  2. 书写规范的javaScript

    书写可维护的代码 代码的维护,修改以及扩展都是需要时间和人力成本的,所以为了减少浪费不必要的成本,从一开始就要书写可维护的代码,这样给自己也给项目其他人提供便利. 书写可维护的代码意味着你的代码是: ...

  3. java equals 和 == 的区别

    )1. == 可以用来比较基本类型以及引用类型.在基本类型中 == 比较的是两个变量的值是否相等.在引用类型中,== 比较的是两个对象的内存地址是否相等. )2. equals 只能用来比较引用类型. ...

  4. 《java.util.concurrent 包源码阅读》23 Fork/Join框架之Fork的冰山一角

    上篇文章一直追踪到了ForkJoinWorkerThread的pushTask方法,仍然没有办法解释Fork的原理,那么不妨来看看ForkJoinWorkerThread的run方法: public ...

  5. Android 异步消息处理机制前篇(二):深入理解Message消息池

    版权声明:本文出自汪磊的博客,转载请务必注明出处. 上一篇中共同探讨了ThreadLocal,这篇我们一起看下常提到的Message消息池到底是怎么回事,废话少说吧,进入正题. 对于稍有经验的开发人员 ...

  6. 防止SSH自动断线

    在连接远程SSH服务的时候,经常会发生长时间后的断线,或者无响应(无法再键盘输入). 总体来说有两个方法: 1.依赖ssh客户端定时发送心跳. putty.SecureCRT.XShell都有这个功能 ...

  7. 局域网下的html注入及DNS劫持

    之前研究Dsploit的部分功能实现原理,现在了解的差不多了,简要记录下Dsploit的断网攻击,html注入,图片替换,cookie劫持的原理.本篇blog需要有一定的网络知识基础在看. 假设现有三 ...

  8. ElasticSearch 学习记录之集群分片内部原理

    分片内部原理 分片是如何工作的 为什么ES搜索是近实时性的 为什么CRUD 操作也是实时性 ES 是怎么保证更新被持久化时断电也不丢失数据 为什么删除文档不会立即释放空间 refresh, flush ...

  9. java 类加载机制总结

    Java程序的运行,离不开class的加载,所谓类加载就是将编译好的class文件加载的jvm并形成class对象,只有class正确加载才能被实例化和调用. public class test { ...

  10. [睡前灵感and发散思维]由一个简单的数组比较问题而想到的

    前言 据说,一只优秀的程序猿往往会有这样的经历,白天遇到一个绞尽脑汁也无法解决的问题,晚上睡了后,半夜在梦中会灵感涌现,立马起床,打开电脑,一气呵成.第二天如果不看注释,完全不知道自己找到了如此巧妙地 ...