leetcode-48.旋转图像
leetcode-48.旋转图像
point: 数组
题意
给定一个 n × n 的二维矩阵表示一个图像。
将图像顺时针旋转 90 度。
说明:
你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。
示例 1:
给定 matrix =
[
[1,2,3],
[4,5,6],
[7,8,9]
], 原地旋转输入矩阵,使其变为:
[
[7,4,1],
[8,5,2],
[9,6,3]
]示例 2:
给定 matrix =
[
[ 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]
]
算法1
简单点就是,用一个栈来按照顺序存储每个元素,然后再依次循环更新数组。但是题目要求在原地实现。
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
vector<int> temp;
int i, j;
for(i=matrix[].size()-; i>=; i--)
{
for(int j=; j<matrix.size(); j++)
{
temp.push_back(matrix[j][i]);
}
} for(i=; i<matrix.size(); i++)
{
for(j=; j<matrix[].size(); j++)
{
matrix[i][j] = temp[temp.size()-];
temp.pop_back();
}
}
}
};
算法2
先转置矩阵,再反转(左右镜像)。左右交换可以使用reverse(begin, end)
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int i, j;
for(i=; i<matrix.size(); i++)
{
for(int j=; j<i; j++)
{
swap(matrix[i][j], matrix[j][i]);
}
} for(i=; i<matrix.size(); i++)
{
reverse(matrix[i].begin(), matrix[i].end());
}
}
};
leetcode-48.旋转图像的更多相关文章
- 前端与算法 leetcode 48. 旋转图像
目录 # 前端与算法 leetcode 48. 旋转图像 题目描述 概要 提示 解析 解法一:转置加翻转 解法二:在单次循环中旋转 4 个矩形 算法 传入测试用例的运行结果 执行结果 GitHub仓库 ...
- Java实现 LeetCode 48 旋转图像
48. 旋转图像 给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像. 示 ...
- [leetcode] 48. 旋转图像(Java)(模拟)
48. 旋转图像 模拟题,其实挺不喜欢做模拟题的... 其实这题一层一层的转就好了,外层转完里层再转,其实就是可重叠的子问题了. 转的时候呢,一个数一个数的转,一个数带动四个数.如图所示,2这个数应该 ...
- LeetCode——48. 旋转图像
给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像. 示例 1: 给定 m ...
- python(leetcode)-48旋转图像
给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像. 示例 1: 给定 m ...
- LeetCode 48. 旋转图像(Rotate Image)
题目描述 给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像. 示例 1: ...
- leetcode 48. 旋转图像 java
class Solution { public void rotate(int[][] matrix) { int n = matrix.length; for (int k = 0; k < ...
- LeetCode:旋转图像【48】
LeetCode:旋转图像[48] 题目描述 给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使 ...
- [array] leetcode - 48. Rotate Image - Medium
leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...
- [LeetCode] 48. Rotate Image 旋转图像
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
随机推荐
- 吴恩达机器学习笔记53-高斯分布的算法(Algorithm of Gaussian Distribution)
如何应用高斯分布开发异常检测算法呢? 异常检测算法: 对于给定的数据集
- [Swift]LeetCode648. 单词替换 | Replace Words
In English, we have a concept called root, which can be followed by some other words to form another ...
- [Swift]LeetCode684. 冗余连接 | Redundant Connection
In this problem, a tree is an undirected graph that is connected and has no cycles. The given input ...
- [Swift]LeetCode841. 钥匙和房间 | Keys and Rooms
There are N rooms and you start in room 0. Each room has a distinct number in 0, 1, 2, ..., N-1, an ...
- [Swift]LeetCode899. 有序队列 | Orderly Queue
A string S of lowercase letters is given. Then, we may make any number of moves. In each move, we c ...
- [Swift]LeetCode983. 最低票价 | Minimum Cost For Tickets
In a country popular for train travel, you have planned some train travelling one year in advance. ...
- 在admui中怎样上传本地文件?
1.首先需要引入一些插件 css: <link rel="stylesheet" href="/vendor/blueimp-file-upload/jquery. ...
- python获取当前运行程序的名字
import os filename = os.path.abspath(__file__) print filename 打印结果: E:\bluedon\test.py
- 【机器学习】--Kmeans从初识到应用
一.前述 Kmeans算法一般在数据分析前期使用,选取适当的k,将数据分类后,然后分类研究不同聚类下数据的特点. Kmeans算法是一种无监督的算法. 常用于分组,比如用户偏好. 二.概念及原理 Km ...
- 『没有上司的舞会 树形DP』
树形DP入门 有些时候,我们需要在树形结构上进行动态规划来求解最优解. 例如,给定一颗\(N\)个节点的树(通常是无根树,即有\(N-1\)条无向边),我们可以选择任意节点作为根节点从而定义出每一颗子 ...