今天的题目 每天的题目见github(看最新的日期): https://github.com/gzc426 具体的题目可以去牛客网对应专题去找。
每天一道剑指offer-顺时针打印矩阵 来源:牛客网对应专题
输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下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.util.ArrayList;
public class Solution {
public ArrayList<Integer> printMatrix(int [][] matrix) {
ArrayList<Integer> resultList = new ArrayList<>();
int cols = matrix[0].length;
int rows = matrix.length;
int left=0,top=0,bottom=rows-1,right=cols-1;
int count = 0;//计数,count如果达到数组的全部个数,那么结束。
while(count < cols*rows)
{
for(int i=left;i<=right;i++)//从左往右进行遍历,第一层
{//left是目前最左边的那个边界,right是目前最右边的边界
resultList.add(matrix[top][i]);
count++;
if(count >= cols*rows)
return resultList;
}
top++;//遍历完目前的最顶层,那么top就到下一层
for(int i=top;i<=bottom;i++)
{//从上往下进行遍历,top是目前最上的边界,bottom是目前最下的边界
resultList.add(matrix[i][right]);
count++;
if(count >= cols*rows)
return resultList;
}
right--;//遍历完最右边的边界,那么right就减一,到下一个最右边边界
for(int i=right;i>=left;i--)
{//从右到左,和上面同理
resultList.add(matrix[bottom][i]);
count++;
if(count >= cols*rows)
return resultList;
}
bottom--;
for(int i=bottom;i>=top;i--)
{//从下到上,和上面同理。
resultList.add(matrix[i][left]);
count++;
if(count >= cols*rows)
return resultList;
}
left++;
}
return resultList;
}
}
代码截图(避免乱码)