54,题目:
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
For example,
Given the following matrix:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
You should return [1,2,3,6,9,8,7,4,5].
本题是按照一种螺旋状的顺序读取一个矩阵的元素。一种简单的思路就是将每一圈提取出来,这是有规律可偱的,所以我们只需要写出一圈的程序就能得到整体的代码。先读上面的行,在读右面的行,下面的行,左面的行。代码入下:
public static List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res = new ArrayList<>();
if(matrix.length ==0 || matrix[0].length==0)
return res;
int row=matrix.length, col=matrix[0].length, n=Math.min(row/2, col/2), i;
for(i=0; i<n; i++){
for(int j=i; j<col-i; j++)
res.add(matrix[i][j]);
for(int j=i+1; j<row-i-1; j++)
res.add(matrix[j][col-i-1]);
for(int j=i; j<col-i; j++)
res.add(matrix[row-i-1][col-j-1]);
for(int j=i+1; j<row-i-1; j++)
res.add(matrix[row-j-1][i]);
}
if(row >= col && col%2 != 0)
for(int j=i; j<row-i; j++)
res.add(matrix[j][col/2]);
if(col>row && row%2 != 0)
for(int j=i; j<col-i; j++)
res.add(matrix[row/2][j]);
return res;
}
另外一种方法思路是相同的,只不过这里用四个角标来表示上下左右四个边,每进行一次遍历之后都将相应的角标更新,直到遍历完整个矩阵结束。代码如下所示:
public List<Integer> spiralOrder1(int[][] matrix) {
List<Integer> spiralList = new ArrayList<>();
if(matrix == null || matrix.length == 0) return spiralList;
// declare indices
int top = 0;
int bottom = matrix.length - 1;
int left = 0;
int right = matrix[0].length - 1;
while(true){
// 1. print top row
for(int j=left; j <=right;j++){
spiralList.add(matrix[top][j]);
}
top++;
if(boundriesCrossed(left,right,bottom,top))
break;
// 2. print rightmost column
for(int i=top; i <= bottom; i++){
spiralList.add(matrix[i][right]);
}
right--;
if(boundriesCrossed(left,right,bottom,top))
break;
// 3. print bottom row
for(int j=right; j >=left; j--){
spiralList.add(matrix[bottom][j]);
}
bottom--;
if(boundriesCrossed(left,right,bottom,top))
break;
// 4. print leftmost column
for(int i=bottom; i >= top; i--){
spiralList.add(matrix[i][left]);
}
left++;
if(boundriesCrossed(left,right,bottom,top))
break;
}// end while true
return spiralList;
}
private boolean boundriesCrossed(int left,int right,int bottom,int top){
if(left>right || bottom<top)
return true;
else
return false;
}
59,题目:
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
For example,
Given n = 3,
You should return the following matrix:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
与上道题目思路一样,只不过一个是读取,另一个是写入。两种方法的代码如下所示:
public int[][] generateMatrix(int n) {
int [][] res = new int[n][n];
int count=1;
for(int i=0; i<n/2; i++){
for(int j=i; j<n-i; j++)
res[i][j] = count++;
for(int j=i+1; j<n-i-1; j++)
res[j][n-i-1] = count++;
for(int j=i; j<n-i; j++)
res[n-i-1][n-j-1] = count++;
for(int j=i+1; j<n-i-1; j++)
res[n-j-1][i] = count++;
}
if(n%2 != 0)
res[n/2][n/2] = n*n;
return res;
}
public static int[][] generateMatrix1(int n) {
int[][] ret = new int[n][n];
int left = 0, top = 0;
int right = n - 1, down = n - 1;
int count = 1;
while (left <= right) {
for (int j = left; j <= right; j++) {
ret[top][j] = count++;
}
top++;
for (int i = top; i <= down; i++) {
ret[i][right] = count++;
}
right--;
for (int j = right; j >= left; j--) {
ret[down][j] = count++;
}
down--;
for (int i = down; i >= top; i--) {
ret[i][left] = count++;
}
left++;
}
return ret;
}