LeetCode OJ 48. Rotate Image
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?
【题目解析】
这个题目的要求是把一个二维数组向右旋转九十度,就地实现。
【思路1】
相比较矩阵的转置,矩阵的旋转有什么不同呢?看下面的例子:
1,2,3 1,3,2 1,2,3 2,3,1
3,2,1 ----转置---> 2,2,3 3,2,1 ----旋转---> 3,2,2
2,3,1 3,1,1 2,3,1 1,1,3

观察上面的结果我们发现这两个不同操作的结果是不同的,但是又有一定的关系,即转置后的每一行是旋转后每一行的逆序。如果就地实现旋转的话,我们发现这是有一定难度的——最后一列变为第一行,倒数第二行变为第二列······,如果我们先转置再逆序的话是很容易实现的。
【思路2】

【java代码1】
public class Solution {
public void rotate(int[][] matrix) {
int row = matrix.length;
if(row == 0) return;
int col = matrix[0].length;
if(col < row) return;
for(int i = 0; i < row; i++){
for(int j = i + 1; j < row; j++){
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
for(int k = 0, m = row - 1; k < m; k++, m--){
int temp = matrix[i][k];
matrix[i][k] = matrix[i][m];
matrix[i][m] = temp;
}
}
}
}
【代码2】
class Solution {
public:
void rotate(vector<vector<int> > &matrix) {
int i,j,temp;
int n=matrix.size();
for(i = ;i < n/;++i) {
for (j = i;j < n--i;++j) {
temp = matrix[j][n-i-];
matrix[j][n-i-] = matrix[i][j];
matrix[i][j] = matrix[n-j-][i];
matrix[n-j-][i] = matrix[n-i-][n-j-];
matrix[n-i-][n-j-] = temp;
}//for
}//for
}
};
LeetCode OJ 48. Rotate Image的更多相关文章
- [Leetcode][Python]48: Rotate Image
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 48: Rotate Imagehttps://leetcode.com/pr ...
- 【一天一道LeetCode】#48. Rotate Image
一天一道LeetCode系列 (一)题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 ...
- 【LeetCode】48. Rotate Image
Difficulty:medium More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/rotate-image/ ...
- 【LeetCode】48. Rotate Image 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode OJ 189. 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 OJ 61. Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- 【LeetCode】48. Rotate Image (2 solutions)
Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...
- LeetCode OJ:Rotate List(旋转链表)
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- LeetCode OJ:Rotate Image(旋转图片)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
随机推荐
- sort vector - leetcode 新用法
179. Largest Number sort(num.begin(), num.end(), [](int a, int b){ return to_string(a)+to_string(b) ...
- HTML5学习之Web存储
在客户端存储数据 HTML5 提供了两种在客户端存储数据的新方法: localStorage - 没有时间限制的数据存储 sessionStorage - 针对一个 session 的数据存储 之前, ...
- iOS 消息推送证书生成方法的简单说明
openssl x509 -in idp.flowtreasure.cer -inform der -out PushChatCert.pem openssl pkcs12 -nocerts -out ...
- 认识css
(一)认识css: CSS全称为"层叠样式表 (Cascading Style Sheets)",它主要是用于定义HTML内容在浏览器内的显示样式,如文字大小.颜色.字体加粗等. ...
- C/C++-style输入输出函数
C风格的输入输出 (1) int getchar() 与 int putchar(int c) getchar从stdin输入流中读取字符,每次只能读取一个字符.若想一次性读取多个字符,则可将其放入循 ...
- 用ftplib爆破FTP口令
#coding:utf-8 #author:jwong import ftplib def bruteLogin(hostname,passwordFile): with open(passwordF ...
- [ An Ac a Day ^_^ ] POJ 3254 Corn Fields 状压dp
题意: 有一块n*m的土地 0代表不肥沃不可以放牛 1代表肥沃可以放牛 且相邻的草地不能同时放牛 问最多有多少种放牛的方法并对1e8取模 思路: 典型的状压dp 能状态压缩 能状态转移 能状态压缩的题 ...
- Pelican搭建静态博客
前言 一直以来都希望拥有属于自己的个人博客,随性发点信息,写点技术感想,记录自己的生活,重要的是不受广告的影响.不被河蟹.不会担心有一天被莫名其妙地消失. 之前看过一篇文章:"像黑客一样写博 ...
- java 的对象拷贝(有深浅拷贝两种方式,深拷贝实现的两种方式(逐层实现cloneable接口,序列化的方式来实现))
Java提高篇--对象克隆(复制)(转自:http://www.cnblogs.com/Qian123/p/5710533.html#_label0) 阅读目录 为什么要克隆? 如何实现克隆 浅克 ...
- oracle查询排序后的前几条记录
select * from (select * from table order by 字段名 desc) where rownum<你要查的记录条数,这样才能符合条件.