轉圈列印矩陣

2021-09-17 03:42:10 字數 1666 閱讀 4419

題目:

給定乙個整形矩陣matrix,請按照轉圈的方式列印它。

例如:1    2    3    4

5    6    7    8

9   10  11  12

13  14  15  16

列印結果為:1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10

要求:額外空間複雜度為o(1).

分析:如果把思路限制在區域性座標怎麼變換上面,這個題就很難寫了。

例如:如果遇到乙個矩陣(陣列)列印或者處理的題目,應該先形成一種巨集觀排程的思維,這牽涉到程式的設計了。那麼對於這道題,給四個變數來標記左上角的點(col1,row1)和右下角的點(col2,row2),這兩個點可以壓中乙個矩陣,如果要列印這兩個點壓中的矩陣的邊界(最外圍一圈),這個函式比較好寫,col1++,加到col2的位置停下來,然後row1++,加到row2的位置停下來。然後row2--,減到row1的位置停下來,然後col2--,減到col1的位置停下來。基本函式是只列印最外圍的一圈,**如下:

public class printmatrixspiralorder 

} public static void printedge(int m, int row1, int col1, int row2, int col2)

} else if (col1 == col2)

} else

while (curr != row2)

while (curc != col1)

while (curr != row1)

} }public static void main(string args) , , ,

};spiralorderprint(matrix);

}}

再例如,如果要z字形列印矩陣,也不要侷限在座標變換裡面,會瘋的。**如下:

public class zigzagprintmatrix 

system.out.println();

} public static void printlevel(int m, int row1, int col1, int row2, int col2,

boolean f)

} else

} }public static void main(string args) , , };

printmatrixzigzag(matrix);

}}

下面來說轉圈列印這道題

public class rotatematrix 

} public static void rotateedge(int m, int tr, int tc, int dr, int dc)

} public static void printmatrix(int matrix)

system.out.println();

} }public static void main(string args) , , ,

};printmatrix(matrix);

rotate(matrix);

system.out.println("*****====");

printmatrix(matrix);

}}

轉圈列印矩陣

題目 給定乙個整型矩陣matrix,請按照轉圈的方式列印它。例如 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 列印結果為 1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10 要求 額外空間複雜度為o 1 難度 1星 coding utf 8...

轉圈列印矩陣

輸入乙個矩陣,按照從外向裡以順時針的順序一次列印出每乙個數字。例如 輸入如下矩陣 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 輸出結果 1,2 3 4 8 12 16 15 14 13 9 5 6 7 11 10這個題的解法很簡單,就是矩陣分圈處理,每次都要列印最外...

轉圈列印矩陣

題目 給定乙個整型矩陣matrix,請按照轉圈的方式列印它。例如 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 列印結果為 1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10 要求 額外空間複雜度為o 1 思路 巨集觀代替微觀的思想 1 先列...