日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

54. Spiral Matrix (Matrix)

發布時間:2024/4/15 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 54. Spiral Matrix (Matrix) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

設置toprow bottomrow leftcol rightcol來標記邊界,然后對每一條邊界進行循環 要是list的size等于matrix的size的話 就表明結束了

?

?

1 class Solution { 2 public List<Integer> spiralOrder(int[][] matrix) { 3 List<Integer> res = new ArrayList<>(); 4 if(matrix == null || matrix.length == 0 || matrix[0].length == 0) { 5 return res; 6 } 7 int row = matrix.length; 8 int col = matrix[0].length; 9 int n = row * col; 10 int toprow = 0; 11 int bottomrow = row - 1; 12 int leftcol = 0; 13 int rightcol = col - 1; 14 15 while(res.size() < n) { 16 for(int i = leftcol; i <= rightcol; i++) { 17 res.add(matrix[toprow][i]); 18 } 19 if(res.size() == n) break; 20 toprow++; 21 22 for(int i = toprow; i <= bottomrow; i++) { 23 res.add(matrix[i][rightcol]); 24 } 25 if(res.size() == n) break; 26 rightcol--; 27 28 for(int i = rightcol; i >= leftcol; i--) { 29 res.add(matrix[bottomrow][i]); 30 } 31 if(res.size() == n) break; 32 bottomrow--; 33 34 for(int i = bottomrow; i >= toprow; i--) { 35 res.add(matrix[i][leftcol]); 36 } 37 if(res.size() == n) break; 38 leftcol++; 39 40 } 41 return res; 42 43 } 44 }

?

轉載于:https://www.cnblogs.com/goPanama/p/9539922.html

總結

以上是生活随笔為你收集整理的54. Spiral Matrix (Matrix)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。