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 的二维矩阵表示一个图像. 将 ...
随机推荐
- MVC中使用SignalR打造酷炫实用的即时通讯功能附源码
前言,现在这世道写篇帖子没个前言真不好意思发出来.本贴的主要内容来自于本人在之前项目中所开发的一个小功能,用于OA中的即时通讯.由于当时走的太急,忘记把代码拿出来.想想这已经是大半年前的事情了,时间过 ...
- 【Valid Palindrome】cpp
题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...
- Taxes
Taxes can be one of the largest cash outflows that a firm experiences.The size of the tax bill(税单) i ...
- Window.document对象(1)
1.Window.document对象 一.找到元素: docunment.getElementById("id"):根据id找,最多找一个: var a =docunme ...
- 输出string vector到file
#include <fstream> #include <iterator> #include <string> #include <vector> i ...
- 无法解决 equal to 运算中 "Chinese_PRC_CI_AS" 和 "SQL_Latin1_General_CP1_CI_AS" 之间的排序规则冲突。
什么是排序规则(collation) 关于SQL Server的排序规则,估计大家都不陌生,在创建数据库时我们经常要选择一种排序规则(conllation),一般我们会留意到每一种语言的排序规则都有许 ...
- c# 简单文件流读写CSV文件
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...
- HDU1013Digital Roots
G - Digital Roots Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- vi编辑
保存命令 按ESC键 跳到命令模式,然后: :w 保存文件但不退出vi :w file 将修改另外保存到file中,不退出vi :w! 强制保存,不推出vi :wq 保存文件并退出vi :wq! 强制 ...
- POJ 2081
#include <iostream> #define MAXN 500005 using namespace std; //unsigned _m[MAXN]; ]; int main( ...