问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)的更多相关文章

  1. LeetCode刷题总结-数组篇(中)

    本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...

  2. C#LeetCode刷题-数组

    数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...

  3. leetcode刷题目录

    leetcode刷题目录 1. 两数之和 2. 两数相加 3. 无重复字符的最长子串 4. 寻找两个有序数组的中位数 5. 最长回文子串 6. Z 字形变换 7. 整数反转 8. 字符串转换整数 (a ...

  4. LeetCode刷题总结-数组篇(下)

    本期讲O(n)类型问题,共14题.3道简单题,9道中等题,2道困难题.数组篇共归纳总结了50题,本篇是数组篇的最后一篇.其他三个篇章可参考: LeetCode刷题总结-数组篇(上),子数组问题(共17 ...

  5. C#LeetCode刷题-字典树

    字典树篇 # 题名 刷题 通过率 难度 208 实现 Trie (前缀树)   48.6% 中等 211 添加与搜索单词 - 数据结构设计   39.9% 中等 212 单词搜索 II   27.9% ...

  6. C#LeetCode刷题-设计

    设计篇 # 题名 刷题 通过率 难度 146 LRU缓存机制   33.1% 困难 155 最小栈 C#LeetCode刷题之#155-最小栈(Min Stack) 44.9% 简单 173 二叉搜索 ...

  7. C#LeetCode刷题-树

    树篇 # 题名 刷题 通过率 难度 94 二叉树的中序遍历   61.6% 中等 95 不同的二叉搜索树 II   43.4% 中等 96 不同的二叉搜索树   51.6% 中等 98 验证二叉搜索树 ...

  8. C#LeetCode刷题-排序

    排序篇 # 题名 刷题 通过率 难度 56 合并区间   31.2% 中等 57 插入区间   30.4% 困难 75 颜色分类   48.6% 中等 147 对链表进行插入排序   50.7% 中等 ...

  9. C#LeetCode刷题-贪心算法

    贪心算法篇 # 题名 刷题 通过率 难度 44 通配符匹配   17.8% 困难 45 跳跃游戏 II   25.5% 困难 55 跳跃游戏   30.6% 中等 122 买卖股票的最佳时机 II C ...

  10. C#LeetCode刷题-字符串

    字符串篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串   24.6% 中等 5 最长回文子串   22.4% 中等 6 Z字形变换   35.8% 中等 8 字符串转整数 (atoi)   ...

随机推荐

  1. 计算机网络学习socket--day3

    1.REUSEADDR(地址重复利用) 1.REUSEADDR解决服务器关闭后重新绑定地址,在day3中知道服务器端必须绑定地址 2.服务器端尽可能使用REUSEADDR 3.在绑定之前尽可能调用se ...

  2. GPO - Folder Mapping via GPO

    Create a Group Policy on AD DC Server. The GPO policy will come into effect on the next login, or us ...

  3. Ethical Hacking - GAINING ACCESS(4)

    SERVER SIDE ATTACKS - METASPLOIT Metasploit is an exploit development and execution tool. It can als ...

  4. mybatis自动生成代码插件mybatis-generator使用流程(亲测可用)

    mybatis-generator是一款在使用mybatis框架时,自动生成model,dao和mapper的工具,很大程度上减少了业务开发人员的手动编码时间 坐着在idea上用maven构建spri ...

  5. Onedrive分享型网盘搭建 - OneManager

    注册账号 部署OneManager 注册完账号后打开网址:https://heroku.com/deploy?template=https://github.com/qkqpttgf/OneManag ...

  6. 高效C++:继承和实现

    如何正确的使用继承和实现是本章说明的重点. 确定public继承的关系是is-a public继承等同于is-a 对public继承,所有base的特性,在derived上都适用 避免遮掩继承而来的名 ...

  7. [spring cloud] -- 服务注册与服务发现篇

    eureka 服务发现客户端 DiscoveryClinet职责(核心) 注册服务无试了到Eureka Server中; 发送新塘更新与Eureka Server的租约: 在服务关闭时从Eureka ...

  8. Nginx配置中文参数说明

    #定义Nginx运行的用户和用户组 user www www; # #nginx进程数,建议设置为等于CPU总核心数. worker_processes 8; # #全局错误日志定义类型,[ debu ...

  9. 一文了解JDK12 13 14 GC调优秘籍-附PDF下载

    目录 简介 那些好用的VM参数 G1的变化 配置FlightRecorder RAM参数 JDK13中的ZGC RTM支持 总结 简介 想了解JDK12,13,14中的GC调优秘籍吗?想知道这三个版本 ...

  10. 第四课 OOP封装继承多态解析,接口抽象类选择 2019-04-21

    父类 xx = new 子类(); xx.method(); 1 普通方法由编译时决定(左边) --- 提高效率 2 虚方法(virtual)  由运行时决定-- -多态,灵活 3 抽象方法由运行时决 ...