Created
December 7, 2016 06:31
-
-
Save jianminchen/91719129b81eefb19f662615b4e3ab95 to your computer and use it in GitHub Desktop.
Matrix Rotation code review - make modifications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Net.Mail; | |
class Solution | |
{ | |
static void Display(int[][] a) | |
{ | |
for (int row = 0; row < a.Length; row++) | |
{ | |
for (int column = 0; column < a[row].Length; column++) | |
{ | |
Console.Write(a[row][column]); | |
Console.Write(' '); | |
} | |
Console.WriteLine(); | |
} | |
} | |
/* | |
* Jianmin Chen | |
* Code review: | |
* count -> rings | |
*/ | |
static void Rotate(int[][] a, int amount) | |
{ | |
int rings = GetRingCount(a); | |
for (int i = 0; i < rings; i++) | |
{ | |
Rotate(a, i, amount); | |
} | |
} | |
/* | |
* Code review: | |
* a -> matrix - function argument | |
* amount -> steps | |
* k -> step, for loop statement | |
* buffer -> prev | |
* buffer1 -> cur | |
* | |
* Good ideas to study: | |
* 1. height, width variables are declared | |
* 2. go through the ring: start from second row, first column, anti-clockwise, one-by-one | |
* if/else if statement - 4 lines - very easy to follow | |
*/ | |
static void Rotate(int[][] matrix, int index, int steps) | |
{ | |
int minRow = index; | |
int maxRow = matrix.Length - 1 - index; | |
int minColumn = index; | |
int maxColumn = matrix[0].Length - 1 - index; | |
int height = maxRow - minRow + 1; | |
int width = maxColumn - minColumn + 1; | |
int actualSteps = steps % (2 * height + 2 * width - 4); | |
for (int step = 0; step < actualSteps; step++) | |
{ | |
int i = minRow, j = minColumn; | |
int prev = matrix[i][j]; | |
do | |
{ | |
// go through first column, last row, last column, first row | |
if ( i < maxRow && j == minColumn) i++; | |
else if (i == maxRow && j < maxColumn) j++; | |
else if (i > minRow && j == maxColumn) i--; | |
else if (i == minRow && j > minColumn) j--; | |
int current = matrix[i][j]; | |
matrix[i][j] = prev; | |
prev = current; | |
} while (!(i == minRow && j == minColumn)); | |
} | |
} | |
/* | |
* Calculate how many rings | |
* It is a good idea to calculate how many rings. | |
*/ | |
static int GetRingCount(int[][] a) | |
{ | |
int rows = a.Length; | |
int columns = a[0].Length; | |
return Math.Min(rows, columns) / 2; | |
} | |
static void Main(String[] args) | |
{ | |
var firstLine = Console.ReadLine().Split(' '); | |
var m = int.Parse(firstLine[0]); | |
var n = int.Parse(firstLine[1]); | |
var r = int.Parse(firstLine[2]); | |
var a = new int[m][]; | |
for (int i = 0; i < m; i++) | |
a[i] = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); | |
Rotate(a, r); | |
Display(a); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment