标签:顺时针 nbsp .sh ret row log == describe style
function printMatrix(matrix) { // write code here var rows; var cols; var i,j; var result = []; if(matrix.length === 0 ){ return result; }else{
//读取矩阵第一行的元素 cols = matrix[0].length; if(cols > 0){ i = 0; while(i < cols){ result.push(matrix[0][i]); i++; } matrix.shift(); } rows = matrix.length; //读取矩阵最后一列元素 if(rows > 0){ cols = matrix[0].length; j = 0; while(j < rows && cols != 0){ result.push(matrix[j][cols-1]); matrix[j].pop(); j++; } }else{ return result; } rows = matrix.length; //读取最后一行的元素 if(rows > 0){ cols = matrix[0].length; j = cols-1; while(j >= 0 && cols != 0){ result.push(matrix[rows-1][j]); j--; } matrix.pop(); }else{ return result; } rows = matrix.length; //读取第一列的元素 if(rows > 0){ cols = matrix[0].length; i = rows - 1; while(i >= 0 && cols != 0){ result.push(matrix[i][0]); matrix[i].shift(); i--; } }else{ return result; } return result.concat(printMatrix(matrix)); } }
标签:顺时针 nbsp .sh ret row log == describe style
原文地址:http://www.cnblogs.com/deerfig/p/6878948.html