-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathspiralMatrix.js
More file actions
70 lines (65 loc) · 1.6 KB
/
spiralMatrix.js
File metadata and controls
70 lines (65 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* @param {number[][]} matrix
* @return {number[]}
*/
var spiralOrder = function(matrix) {
let n = matrix.length;
let m = matrix[0].length;
let top = 0, left = 0;
let right = m -1, bottom = n - 1;
let ans = []
while(top <= bottom && left<=right){
for(let i= left; i<= right; i++){
ans.push(matrix[top][i])
}
top++;
for(let i=top; i<=bottom; i++){
ans.push(matrix[i][right])
}
right--;
for(let i=right; i>=left; i--){
ans.push(matrix[bottom][i])
}
bottom--;
for(let i= bottom; i>=top; i--){
ans.push(matrix[i][left])
}
left++
}
return ans
};
let matrix = [[1,2,3],[4,5,6],[7,8,9]]
console.log(spiralOrder(matrix))
/**
* @param {number[][]} matrix
* @return {number[]}
*/
var spiralOrder = function(matrix) {
if (matrix.length === 0) return [];
const result = [];
let top = 0, bottom = matrix.length - 1;
let left = 0, right = matrix[0].length - 1;
while (top <= bottom && left <= right) {
for (let i = left; i <= right; i++) {
result.push(matrix[top][i]);
}
top++;
for (let i = top; i <= bottom; i++) {
result.push(matrix[i][right]);
}
right--;
if (top <= bottom) {
for (let i = right; i >= left; i--) {
result.push(matrix[bottom][i]);
}
bottom--;
}
if (left <= right) {
for (let i = bottom; i >= top; i--) {
result.push(matrix[i][left]);
}
left++;
}
}
return result;
}