【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 (clockwise).
Follow up: Could you do this in-place?
最简单的想法,两重循环。
第一重循环是对于矩阵的层次(从外到内)
第二重循环是对于每一层次的四元素轮换
举例:
1 2 3
4 5 6
7 8 9
最外层:
1-->3-->9-->7
2-->6-->8-->4
最内层:
5
class Solution {
public:
void rotate(vector<vector<int> > &matrix) {
if(matrix.empty())
return;
int n = matrix.size();
int temp;
for(int i = ; i < (n+)/; i ++)
{
for(int j = i; j < n--i; j ++)
{
temp = matrix[i][j];
matrix[i][j] = matrix[n--j][i];
matrix[n--j][i] = matrix[n--i][n--j];
matrix[n--i][n--j] = matrix[j][n--i];
matrix[j][n--i] = temp;
}
}
}
};

还有一种技巧性比较强的解法。
顺时针旋转90度可以分解为两步:
1、上下颠倒reverse
2、转置transpose
举例:
1 2 3
4 5 6
7 8 9
reverse之后
7 8 9
4 5 6
1 2 3
transpose之后
7 4 1
8 5 2
9 6 3
class Solution {
public:
void rotate(vector<vector<int> > &matrix) {
if(matrix.empty() || matrix[].empty())
return;
reverse(matrix.begin(), matrix.end());
int m = matrix.size();
int n = matrix[].size();
for(int i = ; i < m; i ++)
{
for(int j = i+; j < n; j ++)
{
swap(matrix[i][j], matrix[j][i]);
}
}
}
};

【LeetCode】48. Rotate Image (2 solutions)的更多相关文章
- 【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】61. Rotate List 解题报告(Python)
[LeetCode]61. Rotate List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- 【一天一道LeetCode】#48. Rotate Image
一天一道LeetCode系列 (一)题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 ...
- 【LeetCode】189. Rotate Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leet ...
- 【LeetCode】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】75. Sort Colors (3 solutions)
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- 【LeetCode】90. Subsets II (2 solutions)
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- 【leetcode】61. Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
随机推荐
- 卷积神经网络用于视觉识别Convolutional Neural Networks for Visual Recognition
Table of Contents: Architecture Overview ConvNet Layers Convolutional Layer Pooling Layer Normalizat ...
- LZO 使用和介绍
LZO说明 摘要 LZO 是一个用 ANSI C 语言编写的无损压缩库.他能够提供非常快速的压缩和解压功能.解压并不需要内存的支持.即使使用非常大的压缩比例进行缓慢压缩出的数据,依然能够非常快速的解压 ...
- UNIX域套接字——UNIX domain socket(DGRAM)
#define UNIX_PATH_MAX 108 #include <sys/types.h> #include <sys/socket.h> #include <sy ...
- Search in Rotated Sorted Array leetcode java
题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...
- Spring boot分层和基本概念
后端层次划分: 后端分包: 不同层级之间数据传输:推荐第二种 POJO与JavaBean: POJO就是简单的私有属性,加get/set方法, JavaBean,就是会做一些逻辑处理,包括接收事件,和 ...
- 【Nodejs】理想论坛帖子爬虫1.02
在1.01版本中,我发现各回调函数找到数据后再插入数据库有个竞争问题不好解决,如果等所有回调都完成也没有好的处理方法,因为启动不止一处启动了新的TopicSpider实例. 于是我决定把读数据和写DB ...
- Pinger
import java.io.IOException;import java.io.InputStreamReader;import java.io.LineNumberReader;import j ...
- (剑指Offer)面试题61:按之字形顺序打印二叉树
题目: 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推. 思路: 按照广度优先遍历来遍历二叉树,但是需要 ...
- mysql存储引擎简析
一.常见存储引擎特性 Innodb 具有提交.回滚和崩溃恢复能力的事务安全.支持外键.使用mvcc以及行锁来提供事务支持,因此支持高并发.适用于写频繁,并发率高的应用. Myisam 不支持事务和灾难 ...
- Android动态设置字体颜色
步骤: 1.在values目录下的strings.xml文件中加入颜色:比方 <color name="ccc">#ccc</color> 2.假设你直接这 ...