阿里IM技術分享(七):閒魚IM的線上、離線聊天資料同步機制優化實踐
阿新 • • 發佈:2022-03-15
劍指 Offer 29. 順時針列印矩陣
輸入一個矩陣,按照從外向裡以順時針的順序依次打印出每一個數字。
示例 1:
輸入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
輸出:[1,2,3,6,9,8,7,4,5]
示例 2:
輸入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
輸出:[1,2,3,4,8,12,11,10,9,5,6,7]
限制:
0 <= matrix.length <= 100
0 <= matrix[i].length <= 100
注意:本題與主站 54 題相同:https://leetcode-cn.com/problems/spiral-matrix/
通過次數177,812提交次數405,998
class Solution { public int[] spiralOrder(int[][] matrix) { if(matrix.length ==0 )return new int[]{}; int[][] visit = new int [105][105]; int n = matrix.length, m = matrix[0].length; int[] res = new int[n*m];//存放結果的arr int[][] dp={{0,1},{1,0},{0,-1},{-1,0}}; int cnt = 0; int row = 0 , col = 0; int i = 0 ;//director while(cnt<n*m) { res[cnt++] = matrix[row][col]; visit[row][col] = 1; int drow = row+dp[i][0],dcol = col + dp[i][1]; if(drow<0||drow>=n||dcol<0||dcol>=m||visit[drow][dcol]==1) { i = (i+1)%4; } row = row+dp[i][0]; col = col + dp[i][1]; } //123 //456 //789 return res; } }