# 0054. Spiral Matrix ###### tags: `Leetcode` `Microsoft` `Medium` Link: https://leetcode.com/problems/spiral-matrix/ ## 思路 一圈圈遍历 ## Code ```java= class Solution { public List<Integer> spiralOrder(int[][] matrix) { List<Integer> ans = new ArrayList<>(); if(matrix.length == 0 || matrix[0].length == 0){ return ans; } int top = 0, bottom = matrix.length-1, left = 0, right = matrix[0].length-1; while(top<=bottom && left<= right){ for(int i = left;i <= right;i++){ ans.add(matrix[top][i]); } for(int i = top+1;i <= bottom;i++){ ans.add(matrix[i][right]); } if(top<bottom && left<right){ for(int i = right-1; i >= left;i--){ ans.add(matrix[bottom][i]); } for(int i = bottom-1; i > top;i--){ ans.add(matrix[i][left]); } } top++; bottom--; left++; right--; } return ans; } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up