import java.util.*;
// 1 2 3
// 4 5 6
// 7 8 9
// 螺旋 1 2 3 6 9 8 7 4 5
public class Solution {
public int[] printMatrix (int[][] matrix) {
// 判断空
if (matrix.length == 0 || matrix[0].length == 0) return null;
// left 从1--2--3
// right 从3--2--1
// top 从1--4--7
// bottom 从7--4--1
int left = 0, right = matrix[0].length - 1;
int top = 0, bottom = matrix.length - 1;
// res[]的指针索引index
int index = 0;
// res[]的长度count==计算m*n
int count = matrix.length * matrix[0].length;
// 创建数组存放
int[] res = new int[count];
// 遍历
while (top < (matrix.length+1)/2 && left < (matrix[0].length+1)/2) {
// 1.上面1--2--3左往右
for (int i = left; i <= right; i++) {
res[index++] = matrix[top][i]; // 添加matrix[0][0],matrix[0][1],matrix[0][2]
}
// 2.右边3--6--9上往下,3(即matrix[0][2])已经添加,则top+1
for (int i = top+1; i <= bottom; i++) {
res[index++] = matrix[i][right]; // 添加matrix[1][2],matrix[2][2]
}
// 3.底面9--8--7右往左,9(即matrix[2][2])已经添加,则right-1
// 上述条件top < (matrix.length+1)/2,以防矩阵过于大的时候重复
for (int i = right-1; top != bottom && i >= left; i--) {
res[index++] = matrix[bottom][i]; // 添加matrix[2][1],matrix[2][0]
}
// 4.左边7--4--1下往上,7(即matrix[2][0])已经添加,则bottom-1
// 添加matrix[0][0]重复,冲突 top+1
// 上述条件left < (matrix[0].length+1)/2,以防矩阵过于大的时候重复
for (int i = bottom-1; left != right && i >= top+1; i--) {
res[index++] = matrix[i][left]; // 添加matrix[1][0]
}
// 一圈后,top+1; bottom -1; left+1; right-1
// 缩小范围
top++;
bottom--;
left++;
right--;
}
return res;
}
} class Solution {
public:
/**
*
* @param matrix int整型vector<vector<>> the matrix
* @return int整型vector
*/
/*
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
*/
vector<int> printMatrix(vector<vector<int> >& matrix) {
// write code here
if (matrix .size() == 0) return {};
int width = matrix[0].size();
int heigh = matrix.size();
int layer = (min(heigh,width) + 1) >> 1;
// 层数 0 ~
int i = 0;
vector<int> res;
while ( i < layer ){
// 横着: layer ~ width - layer
// 举例: 0,0 0,5 | 1,1,1,4
for (int j = i ; j < width - i ; j ++) {
res.push_back( matrix[i][j]);
}
//竖着: j确定: i递减
for (int row = i + 1; row < heigh - i ; row ++) {
res.push_back(matrix[row][width - 1 - i]) ;
}
//反向横着
for (int lay = width - 1 - i - 1; lay >= i && (heigh - 1 - i) != i ; lay --) {
res.push_back(matrix[heigh - 1 - i][ lay]);
}
// 反向竖着
for (int row = heigh - 1 - i - 1;row >= i + 1 &&(width - 1 - i) != i ; row --) {
res.push_back( matrix[row][i] );
}
i ++;
}
return res;
}
}; function printMatrix( matrix ) {
// write code here
const res = []
if(matrix.length==0) return []
let top = 0,bottom = matrix.length-1,right= matrix[0].length-1,left=0
while(left<right&&top<bottom){
for(let i= left;i<right;i++) res.push(matrix[top][i])//上层
for(let i = top;i<bottom;i++) res.push(matrix[i][right])//右层
for(let i = right;i>left;i--) res.push(matrix[bottom][i])//下层
for(let i = bottom;i>top;i--) res.push(matrix[i][left])//左层
top ++
bottom--
left++
right--
}
if(left==right){
for(let i = top;i<=bottom;i++) res.push(matrix[i][right])//右层
} else if(top == bottom){
for(let i= left;i<=right;i++) res.push(matrix[top][i])//上层
}
return res
} # # # @param matrix int整型二维数组 the matrix # @return int整型一维数组 # class Solution: def printMatrix(self , matrix ): # write code here ans = [] line,row=len(matrix),len(matrix[0]) count= row*line start,end=0,0 i,j= 0,0 while True: if count==0:return ans if i==start: for j in range(end,row): ans.append(matrix[i][j]) count-=1 start+=1 if count==0:return ans if j==row-1: for i in range(start,line): ans.append(matrix[i][j]) count-=1 row-=1 if count==0:return ans if i==line-1: for j in range(row-1,end-1,-1): ans.append(matrix[i][j]) count-=1 line-=1 if count==0:return ans if j==end: for i in range(line-1,start-1,-1): ans.append(matrix[i][j]) count-=1 end+=1
class Solution {
public int[] printMatrix(int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return new int[0];
}
int m = matrix.length, n = matrix[0].length;
int[] res = new int[m * n];
int rowBegin = 0, rowEnd = m - 1, colBegin = 0, colEnd = n - 1;
int index = 0;
while (rowBegin <= rowEnd && colBegin <= colEnd) {
for (int j = colBegin; j <= colEnd; j++) {
res[index++] = matrix[rowBegin][j];
}
rowBegin++;
for (int i = rowBegin; i <= rowEnd; i++) {
res[index++] = matrix[i][colEnd];
}
colEnd--;
if (rowBegin <= rowEnd) {
for (int j = colEnd; j >= colBegin; j--) {
res[index++] = matrix[rowEnd][j];
}
}
rowEnd--;
if (colBegin <= colEnd) {
for (int i = rowEnd; i >= rowBegin; i--) {
res[index++] = matrix[i][colBegin];
}
}
colBegin++;
}
return res;
}
} class Solution {
public:
/**
*
* @param matrix int整型vector<vector<>> the matrix
* @return int整型vector
*/
vector<int> printMatrix(vector<vector<int> >& matrix) {
// write code here
if (matrix.empty() || matrix[0].empty()) return {};
int m = matrix.size(), n = matrix[0].size();
int up = 0, down = m - 1, left = 0, right = n - 1;
vector<int> res;
while (true) {
for (int j = left; j <= right; ++j) res.push_back(matrix[up][j]);
if (++up > down) break;
for (int i = up; i <= down; ++i) res.push_back(matrix[i][right]);
if (--right < left) break;
for (int j = right; j >= left; --j) res.push_back(matrix[down][j]);
if (--down < up) break;
for (int i = down; i >= up; --i) res.push_back(matrix[i][left]);
if (++left > right) break;
}
return res;
}
};