//螺旋算法遍历
spiralOrder = (matrix) => {
if (matrix.length === 0) return [];
let top = 0,
bottom = matrix.length - 1,
left = 0,
right = matrix[0].length - 1,
result = [];
while (top <= bottom && left <= right) {
for (let j = left; j <= right; j++) {
result.push(matrix[top][j]);
}
if (++top > bottom) break;
for (let i = top; i <= bottom; i++) {
result.push(matrix[i][right]);
}
if (--right < left) break;
for (let j = right; j >= left; j--) {
result.push(matrix[bottom][j]);
}
if (--bottom < top) break;
for (let i = bottom; i >= top; i--) {
result.push(matrix[i][left]);
}
if (++left > right) break;
}
return result;
};
//一维数组转二维
toDimensionArray = (baseArray, n) => {
let len = baseArray.length;
let lineNum = len % 4 === 0 ? len / 4 : Math.floor(len / 4 + 1);
let result = [];
for (let i = 0; i < lineNum; i++) {
let temp = baseArray.slice(i * n, i * n + n);
result.push(temp);
}
return result;
};