-
Notifications
You must be signed in to change notification settings - Fork 639
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
字节&剑指 Offer 29:顺时针打印矩阵(螺旋矩阵) #119
Comments
用四个变量up, down, left, right分别记录当前上下左右范围,每遍历一行或一列就修改相应的值。 var spiralOrder = function(matrix) {
if (matrix === undefined || matrix.length === 0) return [];
let res = [];
let left = 0, right = matrix[0].length-1, up = 0, down = matrix.length-1;
while (true) {
for (let i = left;i <= right;i++) {
res.push(matrix[up][i]);
}
if (++up > down) break;
for (let i = up;i <= down;i++) {
res.push(matrix[i][right]);
}
if (--right < left) break;
for (let i = right;i >= left;i--) {
res.push(matrix[down][i]);
}
if (--down < up) break;
for (let i = down;i >= up;i--) {
res.push(matrix[i][left]);
}
if (++left > right) break;
}
return res;
}; |
核心是四种情况的判断:
var PrintMatrixClockwise = function (matrix) {
let border = { // 存储边界
top: 0,
left: 0,
bottom: matrix.length - 1,
right: matrix[0].length - 1
}
let direction = 'right' // top、right、bottom、left
let result = []
let location = [0, 0] // 当前坐标
function forword() { // 向前移动
switch (direction) {
case 'top':
if (location[1] >= border['top']) {
// 继续向前走
result.push(matrix[location[1]][location[0]])
location[1]--
} else if (location[0] + 1 <= border['right']) {
// 此方向到头了,向右
location[0]++
location[1]++
direction = 'right'
border['left']++
} else return
break;
case 'right':
if (location[0] <= border['right']) {
// 继续向前走
result.push(matrix[location[1]][location[0]])
location[0]++
} else if (location[1] + 1 <= border['bottom']) {
// 此方向到头了,向下
location[0]--
location[1]++
direction = 'bottom'
border['top']++
} else return
break;
case 'bottom':
if (location[1] <= border['bottom']) {
// 继续向前走
result.push(matrix[location[1]][location[0]])
location[1]++
} else if (location[0] - 1 >= border['left']) {
// 此方向到头了,向左
location[0]--
location[1]--
direction = 'left'
border['right']--
} else return
break;
case 'left':
if (location[0] >= border['left']) {
// 继续向前走
result.push(matrix[location[1]][location[0]])
location[0]--
} else if (location[1] - 1 >= border['top']) {
// 此方向到头了,向上
location[0]++
location[1]--
direction = 'top'
border['bottom']--
} else return
break;
}
// 递归
forword()
}
forword()
return result
} |
一圈一圈删除 function fn(arr) {
const res = []
while (arr.length) {
res.push(...arr.shift())
if (arr.length) {
const last = arr.pop()
if (arr.length) {
arr.forEach(val => {
res.push(val.pop())
})
}
res.push(...last.reverse())
if (arr.length) {
const temp = []
arr.forEach(val => {
temp.push(val.shift())
})
res.push(...temp.reverse())
}
}
}
return res
} |
function echoValueClockWise(matrix) {
} |
/**
* @param {number[][]} matrix
* @return {number[]}
*/
var spiralOrder = function(matrix) {
if(matrix.length===0) {
return []
}
const res = [];
//1. 定义左边界 右边界 上边界 下边界
let m = matrix.length;
let n = matrix[0].length;
let l = 0, r = n - 1, t = 0, b = m - 1;
let matrixSize = m * n;
while (res.length < matrixSize) {
for (let i = l; i <= r; i++) {
res.push(matrix[t][i]);
}
t++;
for (let i = t; i <= b; i++) {
res.push(matrix[i][r]);
}
r--;
if(res.length === matrixSize) {
break;
}
for (let i = r; i >= l; i--) {
res.push(matrix[b][i]);
}
b--;
for (let i = b; i >= t; i--) {
res.push(matrix[i][l]);
}
l++;
}
return res;
}; |
var matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
var queryFn = function (matrix) {
if(matrix.length===0) return []
let res = []
let left = 0,
right = matrix[0].length-1,
top = 0,
bottom = matrix.length-1
let n = matrix[0].length * matrix.length
while(true) {
if(left>right) break
for(let i = left; i <= right; i++) {
res.push(matrix[top][i])
}
top++
if(top>bottom) break
for(let i=top; i <= bottom; i++) {
res.push(matrix[i][right])
}
right--
if(left>right) break
for(let i=right; i >= left; i--) {
res.push(matrix[bottom][i])
}
bottom--
if(top>bottom) break
for(let i=bottom; i >= top; i--) {
res.push(matrix[i][left])
}
left++
}
return res
}
queryFn(matrix)
// [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。
示例 1:
示例 2:
限制:
0 <= matrix.length <= 100
0 <= matrix[i].length <= 100
leetcode
The text was updated successfully, but these errors were encountered: