Rotate Matrix by One
记得有道Amazon的OA题目,好像是给定一个矩阵,让把矩阵的每个元素向右shift一个位置。这道题之前没有好好自己想过。今天正好刷到了rotate matrix,所以正好一块想了。
思路是类似LeetCode Spiral Matrix:
- 假设矩阵为方阵
- 设置top, left, bot, right四个边界变量,然后从最外圈到最内圈一圈一圈的shift。
- 设定一个count,当count < total elements in matrix的时候进行shift
- 在每一圈开始的时候记录下来matrix[top][left],然后开始shift
- 从top到bot
- 从left到right
- 从bot到top
- 从right到left
- 在最后一条边的结果尝试更新新的matrix[top][left],这时候的top为旧的,而left已经更新过一次了, 我们要分为两种情况考虑
- count != totalElements - 1, 这时候我们要:
- matrix[top][left] 更新为tmp
- count++
- top++
- 进入下一圈
- 否则 count == totalElements - 1,也要分为两种情况
- totalElements为奇数,我们不改变matrix[top][left]
- totalElements为偶数,这时我们依然要更新一次matrix[top][left] = tmp
- 然后count++结束循环返回结果
- count != totalElements - 1, 这时候我们要:
- 在最后一条边的结果尝试更新新的matrix[top][left],这时候的top为旧的,而left已经更新过一次了, 我们要分为两种情况考虑
- 假如给定矩阵不为方阵,则我们还要加入更多判断,比如剩下最后一行或者最后一列的时候不更新之类的。最后一行或者最后一列可以由bot - top 或者 right - left分别求出
Time Complexity - O(mn),Space Complexity - O(1), in place。
public class Solution {
public void rotateMatrixByOne(int[][] matrix) {
if (matrix == null || matrix[0] == null) {
return;
}
int m = matrix.length;
int n = matrix[0].length;
int left = 0, top = 0, right = n - 1, bot = m - 1;
int count = 0;
int totalElements = m * n;
while (count < totalElements) {
int tmp = matrix[top][left];
if (count < totalElements) {
for (int i = top; i < bot; i++) {
matrix[i][left] = matrix[i + 1][left];
count++;
}
left++;
}
if (count < totalElements) {
for (int i = left - 1; i < right; i++) {
matrix[bot][i] = matrix[bot][i + 1];
count++;
}
bot--;
}
if (count < totalElements) {
for (int i = bot + 1; i > top; i--) {
matrix[i][right] = matrix[i - 1][right];
count++;
}
right--;
}
if (count < totalElements) {
for (int i = right + 1; i > left; i--) {
matrix[top][i] = matrix[top][i - 1];
count++;
}
if (count != totalElements - 1) {
matrix[top][left] = tmp;
} else if (totalElements % 2 == 0) {
matrix[top][left] = tmp;
}
count++;
top++;
}
}
}
}
Rotate Matrix by One的更多相关文章
- [Leetcode] Template to rotate matrix
Rotate the image by 90 degrees (clockwise). Given input matrix = [ [1,2,3,4], [5,6,7,8], [9,10,11,12 ...
- 旋转矩阵(Rotate Matrix)的性质分析
博客转载自:http://www.cnblogs.com/caster99/p/4703033.html 学过矩阵理论或者线性代数的肯定知道正交矩阵(orthogonal matrix)是一个非常好的 ...
- 48. Rotate Image
题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...
- [Leetcode][Python]48: Rotate Image
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 48: Rotate Imagehttps://leetcode.com/pr ...
- [LeetCode]Rotate Image(矩阵旋转)
48. Rotate Image Total Accepted: 69437 Total Submissions: 198781 Difficulty: Medium You are give ...
- LeetCode 48 Rotate Image(2D图像旋转问题)
题目链接: https://leetcode.com/problems/rotate-image/?tab=Description Problem:给定一个n*n的二维图片,将这个二维图片按照顺时 ...
- LeetCode解题报告—— Permutations & Permutations II & Rotate Image
1. Permutations Given a collection of distinct numbers, return all possible permutations. For exampl ...
- [LeetCode] Rotate Image n-by-n矩阵顺时针旋转
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- C#LeetCode刷题之#48-旋转图像(Rotate Image)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3668 访问. 给定一个 n × n 的二维矩阵表示一个图像. 将 ...
随机推荐
- oracle查看最大长度
select s.ids from Student s where length(s.ids)=311 select max(length(s.ids)) from Student s
- 软件工程随堂小作业—— 寻找“水王”(C++)
一.设计思路 (1)输入发帖ID记录表 (2)从第一个ID开始,与后续的发帖ID进行比较,若相同计数器则加一,否则减一.若计数器的数值被减为零,则重新选取当前ID开始记录比较. (3)输出结果 二.源 ...
- android手机配置hosts文件
Android设备测试服务器时,可能需要修改 hosts 文件指定域名到对应的 IP 地址.Android 是基于 Linux 的系统,与 Linux 类似,通过 hosts 文件来设置. 在 And ...
- 异步FIFO为什么用格雷码
异步FIFO通过比较读写地址进行满空判断,但是读写地址属于不同的时钟域,所以在比较之前需要先将读写地址进行同步处理,将写地址同步到读时钟域再和读地址比较进行FIFO空状态判断(同步后的写地址一定是小于 ...
- 1565: [NOI2009]植物大战僵尸 - BZOJ
Description Input Output仅包含一个整数,表示可以获得的最大能源收入.注意,你也可以选择不进行任何攻击,这样能源收入为0.Sample Input3 210 020 0-10 0 ...
- 发送Http Get和Post请求
发送Get请求 HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "GET"; r ...
- winform 按顺序连续打印多个PDF文件
关于PDF打印的问题,前面有篇文章(点这里查看)也叙述过,今天来谈谈另外一种方法 其实方法很简单,因为需要把多个PDF文档按顺序连续打印,为此我们为什么不把要打印的pdf文档按顺序合并成一个PDF打印 ...
- mongo 1067错误
对mongo进行错误的操作导致mongo服务异常关闭,当重启mongo服务时出现1067错误此时在data目录下产生mongod.lock文件,可以讲此文件删除,然后重启就可以了 Please mak ...
- SQL Server 监控 使用sp_trace_create
监控前言 上一节我们提到了MSSQL的基于SQL Event的监控,但是有些时候我们需要更加详细.适用于调优排错的监控.SQL Server内部运行的可见性是的查询调整.优化和综合排查成为可能!这一节 ...
- 安装成功的nginx如何添加未编译安装模块
原已经安装好的nginx,现在需要添加一个未被编译安装的模块举例说明:安装第三方的ngx_cache_purge模块(用于清除指定URL的缓存)nginx的模块是需要重新编译nginx,而不是像apa ...