C#LeetCode刷题之#48-旋转图像(Rotate Image)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3668 访问。
给定一个 n × n 的二维矩阵表示一个图像。
将图像顺时针旋转 90 度。
说明:
你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。
给定 matrix =
[
[1,2,3],
[4,5,6],
[7,8,9]
],原地旋转输入矩阵,使其变为:
[
[7,4,1],
[8,5,2],
[9,6,3]
]
给定 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]
]
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.
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]
]
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]
]
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3668 访问
public class Program {
public static void Main(string[] args) {
var matrix = new int[,] {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
Rotate(matrix);
ShowArray(matrix);
matrix = new int[,] {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 }
};
Rotate2(matrix);
ShowArray(matrix);
Console.ReadKey();
}
private static void ShowArray(int[,] matrix) {
var length = matrix.GetLength(0);
for(var i = 0; i < length; i++) {
for(var j = 0; j < length; j++) {
Console.Write($"{ matrix[i, j]} ");
}
Console.WriteLine();
}
Console.WriteLine();
}
public static void Rotate(int[,] matrix) {
var length = matrix.GetLength(0);
for(var i = 0; i < length; i++) {
for(var j = i; j < length; j++) {
var swap = matrix[i, j];
matrix[i, j] = matrix[j, i];
matrix[j, i] = swap;
}
}
for(var i = 0; i < length; i++) {
for(var j = 0; j < length / 2; j++) {
var swap = matrix[i, j];
matrix[i, j] = matrix[i, length - j - 1];
matrix[i, length - j - 1] = swap;
}
}
}
public static void Rotate2(int[,] matrix) {
var length = matrix.GetLength(0);
for(var i = 0; i < length / 2; i++) {
for(var j = i + 1; j < length - i; j++) {
var swap = matrix[i, j];
matrix[i, j] = matrix[length - 1 - j, i];
matrix[length - 1 - j, i] = matrix[length - 1 - i, length - 1 - j];
matrix[length - 1 - i, length - 1 - j] = matrix[j, length - 1 - i];
matrix[j, length - 1 - i] = swap;
}
}
}
}
以上给出2种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3668 访问
7 4 1
8 5 2
9 6 3
13 9 5 1
14 10 6 2
15 11 7 3
16 12 8 4
分析:
显而易见,Rotate 和 Rotate2 的时间复杂度均为: 。
C#LeetCode刷题之#48-旋转图像(Rotate Image)的更多相关文章
- LeetCode刷题总结-数组篇(中)
本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...
- C#LeetCode刷题-数组
数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...
- leetcode刷题目录
leetcode刷题目录 1. 两数之和 2. 两数相加 3. 无重复字符的最长子串 4. 寻找两个有序数组的中位数 5. 最长回文子串 6. Z 字形变换 7. 整数反转 8. 字符串转换整数 (a ...
- LeetCode刷题总结-数组篇(下)
本期讲O(n)类型问题,共14题.3道简单题,9道中等题,2道困难题.数组篇共归纳总结了50题,本篇是数组篇的最后一篇.其他三个篇章可参考: LeetCode刷题总结-数组篇(上),子数组问题(共17 ...
- C#LeetCode刷题-字典树
字典树篇 # 题名 刷题 通过率 难度 208 实现 Trie (前缀树) 48.6% 中等 211 添加与搜索单词 - 数据结构设计 39.9% 中等 212 单词搜索 II 27.9% ...
- C#LeetCode刷题-设计
设计篇 # 题名 刷题 通过率 难度 146 LRU缓存机制 33.1% 困难 155 最小栈 C#LeetCode刷题之#155-最小栈(Min Stack) 44.9% 简单 173 二叉搜索 ...
- C#LeetCode刷题-树
树篇 # 题名 刷题 通过率 难度 94 二叉树的中序遍历 61.6% 中等 95 不同的二叉搜索树 II 43.4% 中等 96 不同的二叉搜索树 51.6% 中等 98 验证二叉搜索树 ...
- C#LeetCode刷题-排序
排序篇 # 题名 刷题 通过率 难度 56 合并区间 31.2% 中等 57 插入区间 30.4% 困难 75 颜色分类 48.6% 中等 147 对链表进行插入排序 50.7% 中等 ...
- C#LeetCode刷题-贪心算法
贪心算法篇 # 题名 刷题 通过率 难度 44 通配符匹配 17.8% 困难 45 跳跃游戏 II 25.5% 困难 55 跳跃游戏 30.6% 中等 122 买卖股票的最佳时机 II C ...
- C#LeetCode刷题-字符串
字符串篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串 24.6% 中等 5 最长回文子串 22.4% 中等 6 Z字形变换 35.8% 中等 8 字符串转整数 (atoi) ...
随机推荐
- js 自定义阻止事件冒泡函数
// 以下改方法全兼容Chrome function stopBubble(event){ if(event.stopPropagation){ // 兼容火狐(firebox) event.st ...
- Ethical Hacking - Web Penetration Testing(6)
REMOTE FILE INCLUSION Similar to local file inclusion. But allows an attacker to read ANY file from ...
- centos 构建dns服务 dnsmasq
1 安装yum -y install dnsmasq开放udp tcp 53 端口2,修改配置文件 dnsmasq.conf# grep -Ev "^$|^[#;]" /etc/d ...
- LESS实战::not与:hover混合使用
举个例子,有个HTML是这样的. <div class="item light">A</div> <div class="item" ...
- HDU - 1520 Anniversary party (树的最大独立集)
Time limit :1000 ms :Memory limit :32768 kB: OS :Windows There is going to be a party to celebrate t ...
- Docker 入门教程(3)——Dockerfile
Dockerfile Dockerfile是一个文本文件,用来定制镜像. 镜像是分层存储的,前一层会是下一层的基础.而镜像的定制就是定制每一层镜像在上一层做了什么改变. Dockerfile其内包含一 ...
- ZYNQ PS端IIC接口使用-笔记
ZYNQ7000系列FPGA的PS自带两个IIC接口,接口PIN IO可扩展为EMIO形式即将IO约束到PL端符合电平标准的IO(BANK12.BANK13.BANK34.BANK35): SDK中需 ...
- 【Django组件】WebSocket的简单实现
1:HTML: <!DOCTYPE html><html lang="en"><head> <meta charset="UTF ...
- .NET CORE HttpClient使用
自从HttpClient诞生依赖,它的使用方式一直备受争议,framework版本时代产生过相当多经典的错误使用案例,包括Tcp链接耗尽.DNS更改无感知等问题.有兴趣的同学自行查找研究.在.NETC ...
- Python自动化运维:技术与最佳实践 PDF高清完整版|网盘下载内附地址提取码|
内容简介: <Python自动化运维:技术与最佳实践>一书在中国运维领域将有“划时代”的重要意义:一方面,这是国内第一本从纵.深和实践角度探讨Python在运维领域应用的著作:一方面本书的 ...