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 的二维矩阵表示一个图像. 将 ...
随机推荐
- vbs操作txt文本文件常用方法(函数)
创建文件 dim fso, f set fso = server.CreateObject("Scripting.FileSystemObject") set f = fso.Cr ...
- MVC5 自定义表单错误信息
1.
- android开发,设置listview的高度无效
一般是在item的layout中设置高度 android:layout_height="100dp" 但是发现这样后无效,因此找到解决办法,如下: android:minHeigh ...
- 学习Linux第三天
1.常用的命令: reset 清屏 leave +hhmm 建立离开提醒 sudo apt-get yum 安装yum程序 sudo su 切换root身份 see test.c 可以直接查看文件,神 ...
- 802.11 wireless 二
802.11 wireless 2wireless spectrum(无线频谱)1.无线网络使用RF(射频)信号2.无线电也是电磁波3.频谱基于波长被划分,归为多个类型4.无线网络被归为微波段(mic ...
- 【Python】内置数据类型
参考资料: http://sebug.net/paper/books/dive-into-python3/native-datatypes.html http://blog.csdn.net/hazi ...
- E3: PS4/PC 莎木3 众筹200万美元 9小时内达成
这次E3任天堂没有新掌机(传说中的XDS呢.呵呵)和口袋的消息, 被喷得很严重, 索尼的FF7重制版和莎木3是亮点. 而莎木3的众筹速度据说创了记录, 玩家的情怀大胜. 笔者看到国内也有赞助几十刀的玩 ...
- p3p之讲解
P3P是一种被称为个人隐私安全平台项目(the Platform for Privacy Preferences)的标准,能够保护在线隐私权,使Internet冲浪者可以选择在浏览网页时,是否被第三方 ...
- AD转换后数字量的处理
假设模拟输入电压的最大值为5V,A/D转换器件为8位转换. [该转换器的分辨率为1/2n=0.3906%.] [能分辨输入模拟电压变化的最小值为5*0.3906%=19.5mv.] 则模拟电压与数字输 ...
- .NET设计模式(13):享元模式(Flyweight Pattern)(转)
摘要:面向对象的思想很好地解决了抽象性的问题,一般也不会出现性能上的问题.但是在某些情况下,对象的数量可能会太多,从而导致了运行时的代价.那么我们如何去避免大量细粒度的对象,同时又不影响客户程序使用面 ...