2018.8.15 《剑指Offer》从零单刷个人笔记整理(66题全)目录传送门
这道题有两种思路:
思路1:以左上角(x,x)为起始点,每次循环打印矩阵最外围的一圈,每一圈分别四个循环打印四条边。循环结束判定方式也可以有两种:a.对打印次数进行计数,当计数数目==长*宽时截止;b.打印起始点横坐标的2倍<长,起始点纵坐标的2倍<宽,例如对于5*5或6*6的矩阵而言,5>2*2/6>2*2,均可以循环打印3圈。
思路2:每次打印矩阵的第一排,打印完成后删除第一排,对矩阵进行逆时针旋转,然后继续打印。思路比较清奇,但是效率偏低。
JAVA实现(思路1):
/**
*
* @author ChopinXBP
* 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字
* 例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
*
*/
import java.io.BufferedOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
public class printMatrix_18 {
public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] matrix = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};
//int[][] matrix2 = {{1}, {2}, {3}, {4}};
ArrayList<Integer> result = Print(matrix);
for(int i = 0;i < result.size();i++){
out.print(result.get(i));
}
out.close();
}
public static ArrayList<Integer> Print(int[][] matrix){
ArrayList<Integer> result = new ArrayList<Integer>();
int rowEdge = matrix.length;
int colEdge = matrix[0].length;
int num = rowEdge * colEdge;
int count = 0;
int row = 0;
int col = 0;
int beginpoint = 0;
//每次循环打印矩阵中一个圈
//循环条件可改为while(2 * beginpoint < rowEdge && 2 * beginpoint < colEdge)
while(count < num){
//顶边
while(col < colEdge){
result.add(matrix[row][col++]);
count++;
}
row++;col--;
//右边
while(row < rowEdge){
result.add(matrix[row++][col]);
count++;
}
col--;row--;
if(count >= num)break; //判断是否已经打印完(顶边/右边必存在)
//底边
while(col >= beginpoint){
result.add(matrix[row][col--]);
count++;
}
row--;col++;
//左边
while(row > beginpoint){
result.add(matrix[row--][col]);
count++;
}
//缩小一圈矩阵
beginpoint++;
rowEdge--;colEdge--;
row = beginpoint;col = beginpoint;
}
return result;
}
}
JAVA实现(思路2):
//解法二:矩阵转置,效率略低
/*
* 采用旋转魔方的方式 一次取一行,然后旋转
*/
public ArrayList<Integer> printMatrix_2(int[][] matrix) {
ArrayList<Integer> al = new ArrayList<>();
int row = matrix.length;
while (row != 0) {
for (int i = 0; i < matrix[0].length; i++) {
al.add(matrix[0][i]);
}
if (row == 1)
break;
matrix = turn(matrix);
row = matrix.length;
}
return al;
}
private int[][] turn(int[][] matrix) {
// TODO 自动生成的方法存根
int col = matrix[0].length;
int row = matrix.length;
int[][] newMatrix = new int[col][row - 1];
for (int j = col - 1; j >= 0; j--) {
for (int i = 1; i < row; i++) {
newMatrix[col - 1 - j][i - 1] = matrix[i][j];
}
}
return newMatrix;
}
C++实现(思路1):
void PrintMatrixInCircle(int** numbers, int columns, int rows, int start);
void printNumber(int number);
void PrintMatrixClockwisely(int** numbers, int columns, int rows)
{
if(numbers == NULL || columns <= 0 || rows <= 0)
return;
int start = 0;
while(columns > start * 2 && rows > start * 2)
{
PrintMatrixInCircle(numbers, columns, rows, start);
++start;
}
}
void PrintMatrixInCircle(int** numbers, int columns, int rows, int start)
{
int endX = columns - 1 - start;
int endY = rows - 1 - start;
// 从左到右打印一行
for(int i = start; i <= endX; ++i)
{
int number = numbers[start][i];
printNumber(number);
}
// 从上到下打印一列
if(start < endY)
{
for(int i = start + 1; i <= endY; ++i)
{
int number = numbers[i][endX];
printNumber(number);
}
}
// 从右到左打印一行
if(start < endX && start < endY)
{
for(int i = endX - 1; i >= start; --i)
{
int number = numbers[endY][i];
printNumber(number);
}
}
// 从下到上打印一行
if(start < endX && start < endY - 1)
{
for(int i = endY - 1; i >= start + 1; --i)
{
int number = numbers[i][start];
printNumber(number);
}
}
}
void printNumber(int number)
{
printf("%d\t", number);
}
测试代码:
// ====================测试代码====================
void Test(int columns, int rows)
{
printf("Test Begin: %d columns, %d rows.\n", columns, rows);
if(columns < 1 || rows < 1)
return;
int** numbers = new int*[rows];
for(int i = 0; i < rows; ++i)
{
numbers[i] = new int[columns];
for(int j = 0; j < columns; ++j)
{
numbers[i][j] = i * columns + j + 1;
}
}
PrintMatrixClockwisely(numbers, columns, rows);
printf("\n");
for(int i = 0; i < rows; ++i)
delete[] (int*)numbers[i];
delete[] numbers;
}
int _tmain(int argc, _TCHAR* argv[])
{
/*
1
*/
Test(1, 1);
/*
1 2
3 4
*/
Test(2, 2);
/*
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
*/
Test(4, 4);
/*
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
*/
Test(5, 5);
/*
1
2
3
4
5
*/
Test(1, 5);
/*
1 2
3 4
5 6
7 8
9 10
*/
Test(2, 5);
/*
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
*/
Test(3, 5);
/*
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
*/
Test(4, 5);
/*
1 2 3 4 5
*/
Test(5, 1);
/*
1 2 3 4 5
6 7 8 9 10
*/
Test(5, 2);
/*
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
*/
Test(5, 3);
/*
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
*/
Test(5, 4);
return 0;
}
#Coding一小时,Copying一秒钟。留个言点个赞呗,谢谢你#