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) ...
随机推荐
- git分支间切换注意点和bug分支的处理
目录 备注: 知识点 记一次分支合并问题状况 从分支点开始,不同分支修改工作区的内容(不添加到暂存区和提交),切换分支,工作区的内容是一样的. 必须在提交或者暂存当前暂存区的状态后,再切换或合并分支 ...
- RabbitMQ 入门之基础概念
什么是消息队列(MQ) 消息是在不同应用间传递的数据.这里的消息可以非常简单,比如只包含字符串,也可以非常复杂,包含多个嵌套的对象.消息队列(Message Queue)简单来说就是一种应用程序间的通 ...
- fastjson将json字符串转化为java对象
目录 一.导入一个fastjson的jar包 二.Json字符串格式 三.根据json的格式创建Java类 四.给java类的所有属性添加setter方法 五.转换为java对象 一.导入一个fast ...
- 设计模式:proxy模式
目的:为其他对象提供一种代理以控制对这个对象的访问 理解:尽管Decorator的实现部分与代理相似,但Decorator的目的不一样.Decorator为对象添加一个或多个功能,而代理则控制对对象的 ...
- 【翻译】.NET 5 Preview7发布
今天,发布了.NET 5.0 Preview7.这是倒数第二个预览版本(在转移到RC之前).此时,大多数功能应该已经非常接近完成了.Single file和ARM64 intrinsics是两个花费了 ...
- 水题----根据O出现次数判断分数
There is an objective test result such as \OOXXOXXOOO". An `O' means a correct answer of a prob ...
- 2020数字中国创新大赛虎符网络安全赛道-pwn count
比赛结束前半个小时才看的题,等我做出来比赛已经结束了.难受Orz 本地文件无法执行,远程调试. 题目大概意思就是让你计算200道四则运算.(实际上格式是固定的.先乘一次然后再加两次).200道题都正确 ...
- php提取xml配置参数
demo1.php <?php class AddressManager{ private $addresses = array("ip地址1","ip地址2&qu ...
- PHP is_int() 、is_integer()、is_long() 函数
is_int() 函数用于检测变量是否是整数.高佣联盟 www.cgewang.com 注意: 若想测试一个变量是否是数字或数字字符串(如表单输入,它们通常为字符串),必须使用 is_numeric( ...
- PHP soundex() 函数
实例 计算 "Hello" 的 soundex 键: <?php高佣联盟 www.cgewang.com$str = "Hello";echo sound ...