[Swift]LeetCode48. 旋转图像 | Rotate Image
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9907843.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
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.
Example 1:
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] ]
Example 2:
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] ]
给定一个 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] ]
8ms
1 class Solution 2 { 3 func rotate(_ matrix: inout [[Int]]) 4 { 5 let temp = matrix; 6 7 for i in 0..<matrix.count 8 { 9 for j in 0..<matrix[0].count 10 { 11 matrix[j][matrix[0].count - 1 - i] = temp[i][j]; 12 } 13 } 14 } 15 }
12ms
1 class Solution { 2 func rotate(_ matrix: inout [[Int]]) { 3 matrix.reverse() 4 5 for i in 1..<matrix.count { 6 for j in 0..<i { 7 (matrix[i][j], matrix[j][i]) = (matrix[j][i], matrix[i][j]) 8 } 9 } 10 } 11 }
16ms
1 class Solution { 2 func rotate(_ matrix: inout [[Int]]) { 3 let layers:Int = matrix.count / 2 - 1 4 let n = matrix.count - 1 5 var currentLayerWidth = n - 1 //layer的宽度,每一层转 currentLayerWidth次 6 if layers < 0 { 7 return 8 } 9 for i in 0...layers { 10 if currentLayerWidth < 0 { 11 break 12 } 13 for j in 0...currentLayerWidth { //每层旋转逻辑 14 let x = i + j //i 层级,x 移动点(相对整个矩阵) 15 let firstPoint = matrix[i][x] 16 matrix[i][x] = matrix[n - x][i] 17 matrix[n - x][i] = matrix[n - i][n - x] 18 matrix[n - i][n - x] = matrix[x][n - i] 19 matrix[x][n - i] = firstPoint 20 } 21 //向内层靠近 22 currentLayerWidth = currentLayerWidth - 2 23 } 24 } 25 }
16ms
1 class Solution { 2 func rotate(_ matrix: inout [[Int]]) { 3 let n = matrix.count 4 5 for layer in 0..<n/2 { 6 let start = layer 7 let end = n - layer - 1 8 9 for i in start..<end { 10 let offset = i - start 11 (matrix[start][i], matrix[i][end], matrix[end][end-offset], matrix[end-offset][start]) = ( matrix[end-offset][start], matrix[start][i], matrix[i][end], matrix[end][end-offset]) 12 } 13 } 14 } 15 }
20ms
1 class Solution { 2 func rotate(_ matrix: inout [[Int]]) { 3 let count = matrix[0].count 4 for i in (0 ..< count) { 5 for t in (i ..< count) { 6 let d = matrix[t][i] 7 matrix[t][i] = matrix[i][t] 8 matrix[i][t] = d 9 } 10 } 11 12 for i in (0 ..< count) { 13 for t in (0 ..< (count + 1)/2 ) { 14 let temp = matrix[i][t] 15 matrix[i][t] = matrix[i][count - t - 1] 16 matrix[i][count - t - 1] = temp 17 } 18 } 19 } 20 }
28ms
1 class Solution { 2 func rotate(_ matrix: inout [[Int]]) { 3 let count = matrix.count 4 for i in 0..<count { 5 for j in i+1..<count { 6 if i == j { return } 7 // swap 8 matrix[i][j] = matrix[i][j] ^ matrix[j][i] 9 matrix[j][i] = matrix[i][j] ^ matrix[j][i] 10 matrix[i][j] = matrix[i][j] ^ matrix[j][i] 11 } 12 matrix[i].reverse() 13 } 14 } 15 }
36ms
1 class Solution { 2 func rotate(_ matrix: inout [[Int]]) { 3 4 for item in 0...matrix.count-1 { 5 for i in item...matrix.count-1 { 6 let tmpItem = matrix[item][i] 7 matrix[item][i] = matrix[i][item] 8 matrix[i][item] = tmpItem 9 } 10 } 11 for item in 0...matrix.count-1 { 12 matrix[item].reverse() 13 } 14 15 } 16 }
转载于:https://www.cnblogs.com/strengthen/p/9907843.html
[Swift]LeetCode48. 旋转图像 | Rotate Image相关推荐
- leetcode48. 旋转图像
给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像. 示例 1: 给定 m ...
- LeetCode中,python一行代码能干啥?
导读 都说python语言简洁.集成高效,一行代码往往能实现很多复杂的操作,比如两变量交换.心形输出.打印乘法口诀等等.但这些总归还是不太实用.那么我们换做在LeetCode中,看看用python一行 ...
- 【Leetcode 专题五】数组和哈希表
目录 一.前言 二.解题思路和代码整理 2.1.数组重建 Leetcode283. 移动零 Leetcode27. 移除元素 Leetcode26. 删除有序数组中的重复项 2.2.数组双指针 Lee ...
- 深度学习必备---用Keras和直方图均衡化---数据增强
点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 一.俺.遇到了啥子问题撒~? 我现在写的文章都是因为遇到问题了,然 ...
- 【大总结3】leetcode解题总览(算法、剑指offer、SQL、多线程、shell)
3/22更新 剑指offer 题目链接 建议大部分题都会做,都能比较快速且准确的写出来.关于做题方式,我的建议是:一道一道刷即可,因为难度一般,不用系统的学习什么知识,遇到实在不会的就跳过即可. 我这 ...
- 【KERAS/直方图均衡化】图像数据集扩充
原网址: https://blog.csdn.net/sinat_36458870/article/details/78903092 一.我遇到了啥子问题撒~? 我现在写的文章都是因为遇到问题了,然后 ...
- android ndk之opencv+MediaCodec硬编解码来处理视频动态时间水印
android ndk之opencv+MediaCodec硬编解码来处理视频水印学习笔记 android视频处理学习笔记.以前android增加时间水印的需求,希望多了解视频编解码,直播,特效这一块, ...
- LeetCode算法题整理(200题左右)
目录 前言 一.树(17) 1.1.后序遍历 1.2.层次遍历 1.3.中序 1.4.前序 二.回溯(20) 2.1.普通回溯 2.2.线性回溯:组合.排列.子集.分割 2.3.矩阵回溯 三.二分查找 ...
- LeetCode 从零单刷个人笔记整理(持续更新)
更新至2020.2.23 github:https://github.com/ChopinXBP/LeetCode-Babel 本人博客用于个人对知识点的记录和巩固. 用几乎所有可行的方法进行了实现和 ...
最新文章
- SpringBoot第十四篇:在springboot中用redis实现消息队列
- Win7安装软件,界面上中文显示乱码的解决方案
- 安装NFS服务,并挂载到开发板
- 一起学nRF51xx 15 - spis
- 如何在virtualbox中对虚拟机截图
- 单片机断电后静态存储区里面还有数据吗_单片机启动流程和存储架构详解
- Java基础—集合2Set接口和Map接口
- mysql 编码种类_MySQL 编码
- index函数在python中的用法_index函数怎么在python中使用
- Chrome浏览器隐藏彩蛋
- _f5是c语言合法变量,C语言练习题1
- Google assisant 2018谷歌IO大会 谷歌助理背后的系统设计
- 转:企业最稀缺和最具价值的人力资源到底是什么?
- python取前三位_python的字符串截取||取字符串前三位
- 2021年美国大学生数学建模竞赛D题思路分析
- RxJava 3.x 使用总结
- Linux如何查看磁盘/分区的UUID
- mpvue使用vuex基本步骤以及如何使用
- 【Python项目】Python基于tkinter实现笔趣阁小说下载器(附源码)
- NUC-ACM/ICPC 寒假训练 简单DP A - G题