BM98. 螺旋矩阵
描述
示例1
输入:
[[1,2,3],[4,5,6],[7,8,9]]
输出:
[1,2,3,6,9,8,7,4,5]
示例2
输入:
[]
输出:
[]
C++ 解法, 执行用时: 0ms, 内存消耗: 8556KB, 提交时间: 2014-11-03
class Solution { public: vector<int> spiralOrder(vector<vector<int> >& matrix) { vector<int> result; if (matrix.empty()) return result; int beginX = 0, endX = matrix[0].size() - 1; int beginY = 0, endY = matrix.size() - 1; while (true) { // From left to right for (int j = beginX; j <= endX; ++j) result.push_back(matrix[beginY][j]); if (++beginY > endY) break; // From top to bottom for (int i = beginY; i <= endY; ++i) result.push_back(matrix[i][endX]); if (beginX > --endX) break; // From right to left for (int j = endX; j >= beginX; --j) result.push_back(matrix[endY][j]); if (beginY > --endY) break; // From bottom to top for (int i = endY; i >= beginY; --i) result.push_back(matrix[i][beginX]); if (++beginX > endX) break; } return result; } };
C 解法, 执行用时: 2ms, 内存消耗: 348KB, 提交时间: 2021-03-03
/** * * @param matrix int整型二维数组 * @param matrixRowLen int matrix数组行数 * @param matrixColLen int* matrix数组列数 * @return int整型一维数组 * @return int* returnSize 返回数组行数 */ int* spiralOrder(int** matrix, int matrixRowLen, int *matrixColLen, int* returnSize ) { // write code here int row1,row2,col1,col2,idx=0,count,*array,tmp=0; count = matrixRowLen * (*matrixColLen); *returnSize = count; array = (int *) calloc(count, sizeof(int)); row1=0; row2=matrixRowLen-1; col1=0; col2=*matrixColLen-1; while(1) { for(idx=col1;idx<=col2;idx++) { array[tmp++]= matrix[row1][idx]; } if(tmp>=count) break; row1++; for(idx=row1;idx<=row2;idx++) { array[tmp++]= matrix[idx][col2]; } if(tmp>=count) break; col2--; for(idx=col2;idx>=col1;idx--) { array[tmp++]= matrix[row2][idx]; } if(tmp>=count) break; row2--; for(idx=row2;idx>=row1;idx--) { array[tmp++]= matrix[idx][col1]; } if(tmp>=count) break; col1++; } return array; }
C 解法, 执行用时: 2ms, 内存消耗: 352KB, 提交时间: 2021-01-17
/** * * @param matrix int整型二维数组 * @param matrixRowLen int matrix数组行数 * @param matrixColLen int* matrix数组列数 * @return int整型一维数组 * @return int* returnSize 返回数组行数 */ int* spiralOrder(int** matrix, int matrixRowLen, int *matrixColLen, int* returnSize ) { // write code here int row1,row2,col1,col2,idx=0,count,*array,tmp=0; count = matrixRowLen * (*matrixColLen); *returnSize = count; array = (int *) calloc(count, sizeof(int)); row1=0; row2=matrixRowLen-1; col1=0; col2=*matrixColLen-1; while(1) { for(idx=col1;idx<=col2;idx++) { array[tmp++]= matrix[row1][idx]; } if(tmp>=count) break; row1++; for(idx=row1;idx<=row2;idx++) { array[tmp++]= matrix[idx][col2]; } if(tmp>=count) break; col2--; for(idx=col2;idx>=col1;idx--) { array[tmp++]= matrix[row2][idx]; } if(tmp>=count) break; row2--; for(idx=row2;idx>=row1;idx--) { array[tmp++]= matrix[idx][col1]; } if(tmp>=count) break; col1++; } return array; }
C 解法, 执行用时: 2ms, 内存消耗: 352KB, 提交时间: 2020-11-29
/** * * @param matrix int整型二维数组 * @param matrixRowLen int matrix数组行数 * @param matrixColLen int* matrix数组列数 * @return int整型一维数组 * @return int* returnSize 返回数组行数 */ int* spiralOrder(int** matrix, int matrixRowLen, int *matrixColLen, int* returnSize ) { // write code here int row1,row2,col1,col2,idx=0,count,*array,tmp=0; count = matrixRowLen * (*matrixColLen); *returnSize = count; array = (int *) calloc(count, sizeof(int)); row1=0; row2=matrixRowLen-1; col1=0; col2=*matrixColLen-1; while(1) { for(idx=col1;idx<=col2;idx++) { array[tmp++]= matrix[row1][idx]; } if(tmp>=count) break; row1++; for(idx=row1;idx<=row2;idx++) { array[tmp++]= matrix[idx][col2]; } if(tmp>=count) break; col2--; for(idx=col2;idx>=col1;idx--) { array[tmp++]= matrix[row2][idx]; } if(tmp>=count) break; row2--; for(idx=row2;idx>=row1;idx--) { array[tmp++]= matrix[idx][col1]; } if(tmp>=count) break; col1++; } return array; }
C 解法, 执行用时: 2ms, 内存消耗: 352KB, 提交时间: 2020-10-05
/** * * @param matrix int整型二维数组 * @param matrixRowLen int matrix数组行数 * @param matrixColLen int* matrix数组列数 * @return int整型一维数组 * @return int* returnSize 返回数组行数 */ int* spiralOrder(int** matrix, int matrixRowLen, int *matrixColLen, int* returnSize ) { // write code here int row1,row2,col1,col2,idx=0,count,*array,tmp=0; count = matrixRowLen * (*matrixColLen); *returnSize = count; array = (int *) calloc(count, sizeof(int)); row1=0; row2=matrixRowLen-1; col1=0; col2=*matrixColLen-1; while(1) { for(idx=col1;idx<=col2;idx++) { array[tmp++]= matrix[row1][idx]; } if(tmp>=count) break; row1++; for(idx=row1;idx<=row2;idx++) { array[tmp++]= matrix[idx][col2]; } if(tmp>=count) break; col2--; for(idx=col2;idx>=col1;idx--) { array[tmp++]= matrix[row2][idx]; } if(tmp>=count) break; row2--; for(idx=row2;idx>=row1;idx--) { array[tmp++]= matrix[idx][col1]; } if(tmp>=count) break; col1++; } return array; }