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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

[剑指offer][JAVA]面试题第[29]题[顺时针打印矩阵][数组]

發布時間:2023/12/10 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [剑指offer][JAVA]面试题第[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

【解答思路】

1. 模擬


時間復雜度:O(NM) 空間復雜度:O(NM)

class Solution {public int[] spiralOrder(int[][] matrix) {if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {return new int[0];}int rows = matrix.length, columns = matrix[0].length;boolean[][] visited = new boolean[rows][columns];int total = rows * columns;int[] order = new int[total];int row = 0, column = 0;int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};int directionIndex = 0;//total二維數組的所有個數for (int i = 0; i < total; i++) {order[i] = matrix[row][column];visited[row][column] = true;int nextRow = row + directions[directionIndex][0], nextColumn = column + directions[directionIndex][1];if (nextRow < 0 || nextRow >= rows || nextColumn < 0 || nextColumn >= columns || visited[nextRow][nextColumn]) {directionIndex = (directionIndex + 1) % 4;}row += directions[directionIndex][0];column += directions[directionIndex][1];}return order;} }
2. 按層模擬(圓圓圈圈圓圓)

解釋一


時間復雜度:O(NM) 空間復雜度:O(NM)

class Solution {public int[] spiralOrder(int[][] matrix) {if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {return new int[0];}int rows = matrix.length, columns = matrix[0].length;int[] order = new int[rows * columns];int index = 0;int left = 0, right = columns - 1, top = 0, bottom = rows - 1;while (left <= right && top <= bottom) {for (int column = left; column <= right; column++) {order[index++] = matrix[top][column];}for (int row = top + 1; row <= bottom; row++) {order[index++] = matrix[row][right];}//判斷必不可少 if (left < right && top < bottom) {for (int column = right - 1; column > left; column--) {order[index++] = matrix[bottom][column];}for (int row = bottom; row > top; row--) {order[index++] = matrix[row][left];}}left++;right--;top++;bottom--;}return order;} }

解釋二

時間復雜度:O(NM) 空間復雜度:O(NM)

class Solution {public int[] spiralOrder(int[][] matrix) {if(matrix.length == 0) return new int[0];int l = 0, r = matrix[0].length - 1, t = 0, b = matrix.length - 1, x = 0;int[] res = new int[(r + 1) * (b + 1)];while(true) {for(int i = l; i <= r; i++) res[x++] = matrix[t][i]; // left to right.if(++t > b) break;for(int i = t; i <= b; i++) res[x++] = matrix[i][r]; // top to bottom.if(l > --r) break;for(int i = r; i >= l; i--) res[x++] = matrix[b][i]; // right to left.if(t > --b) break;for(int i = b; i >= t; i--) res[x++] = matrix[i][l]; // bottom to top.if(++l > r) break;}return res;} }

【總結】

1.++i 和 i++

++在前下自加后運算;++在后先運算后自加

2.畫圖有助于理解,定點切分
3.細節學習

3.1 while break結合

while(true) { if(條件) break;}

3.2 二維矩陣遍歷初始化

int rows = matrix.length, columns = matrix[0].length;boolean[][] visited = new boolean[rows][columns];int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

轉載鏈接:https://leetcode-cn.com/problems/shun-shi-zhen-da-yin-ju-zhen-lcof/solution/shun-shi-zhen-da-yin-ju-zhen-by-leetcode-solution/

參考鏈接:https://leetcode-cn.com/problems/shun-shi-zhen-da-yin-ju-zhen-lcof/solution/mian-shi-ti-29-shun-shi-zhen-da-yin-ju-zhen-she-di/

總結

以上是生活随笔為你收集整理的[剑指offer][JAVA]面试题第[29]题[顺时针打印矩阵][数组]的全部內容,希望文章能夠幫你解決所遇到的問題。

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