48. Rotate Image
来源:互联网 发布:linux ftp server 编辑:程序博客网 时间:2024/05/28 04:54
原题
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?
代码实现
算法思想:
时间复杂度O(n^2),空间复杂度O(1)
public class Solution { //This is in-place algorithm by setting a cache cell // first dividing layers(n>>1) // second each layer element moves forward each(n-2*i-1) steps public void Rotate(int[,] matrix) { int n = matrix.GetUpperBound(0) + 1; int loop = n >> 1; for (int i = 0; i < loop; i++){ int each = n - 2 * i - 1; for (int k = 0; k < each; k++) { for (int j = k + each; j < each * 4; j += each){ int cache; if (j / each == 0) {//top cache = matrix[i, i + j % each]; matrix[i, i + j % each] = matrix[i, i + k]; } else if (j / each == 1) {//right cache = matrix[i + j % each, n - i - 1]; matrix[i + j % each, n - i - 1] = matrix[i, i + k]; } else if (j / each == 2) {//bottom cache = matrix[n - i - 1, n - j % each - i - 1]; matrix[n - i - 1, n - j % each - i - 1] = matrix[i, i + k]; } else {//left cache = matrix[n - j % each - i - 1, i]; matrix[n - j % each - i - 1, i] = matrix[i, i + k]; } //matrix[i,i+k]作为缓存区 matrix[i, i + k] = cache; } } } } }
阅读全文
2 1
- [LeetCode]48.Rotate Image
- LeetCode 48. Rotate Image
- LeetCode 48.Rotate Image
- LeetCode --- 48. Rotate Image
- [Leetcode] 48. Rotate Image
- 48. Rotate Image
- 48. Rotate Image
- 48. Rotate Image
- 48. Rotate Image
- [leetcode] 48. Rotate Image
- Leetcode 48. Rotate Image
- 48. Rotate Image LeetCode
- [LeetCode]48. Rotate Image
- 48. Rotate Image
- 48. Rotate Image
- leetcode 48. Rotate Image
- Leetcode 48. Rotate Image
- LeetCode 48. Rotate Image
- 代码托管:GitLab与GitHub的比较
- Windows下MySQL 5.7.18免安装版的安装配置教程
- Android Studio 创建/打开项目时一直处于Building“project name”Gradle project info 的解决
- Redis的数据安全
- Android Studio轻松配置自动版本号和版本名
- 48. Rotate Image
- Java后台之路(15)-MVC和数据库连接池
- 使用jenkins持续构建、部署SpringBoot项目
- 基于Transformation Generation的单张图像视频生成
- 关于路径前面加"/"与不加"/"的区别小记
- 深度学习防止过拟合的方法
- webview 去掉滚动条和上下黑灰色背景
- 面向过程 VS 面向对象
- 无法进入ubuntu怎么清理\boot旧版系统内核