順時針列印矩陣
輸入一個矩陣,按照從外向裡以順時針的順序依次列印出每一個數字。
解題思路:我們把列印一圈分為四步:第一步從左到右列印一行,第二步從上到下列印一列,第三步從右到左列印一行,第四步從下到上列印一列。值得注意的是,最後一圈有可能退化成只有一行、只有一列,甚至只有一個數字。
因此要仔細分析列印時每一步的前提條件。第一步總是需要的,因為列印一圈至少有一步。如果只有一行,就不用第二步了。也就是需要第二步的前提條件是終止行號大於起始行號。需要第三部列印的前提條件是圈內至少有兩行兩列,也就是說除了要求終止行號大於起始行號之外,還要求終止列號大於起始列號。需要列印第四步的前提條件是至少有三行兩列,因此要求終止行號比起始行號至少大2,終止列號大於起始列號。
C#實現:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
#region 順時針列印矩陣 /// 輸入一個矩陣,按照從外向裡以順時針的順序依次列印出每一個數字。
///
public static void PrintMatrixClockwisely( int [,] numbers, int columns, int rows)
{
if (numbers == null || columns <= 0 || rows <= 0)
return ;
int start = 0;
while (columns > start * 2 && rows > start * 2)
{
PrintMatrixInCircle(numbers, columns, rows, start);
start++;
}
}
private static void PrintMatrixInCircle( int [,] numbers, int columns, int rows, int start)
{
int endX = columns - 1 - start;
int endY = rows - 1 - start;
// 從左到右列印一行
for ( int i = start; i <= endX; i++)
Console.Write(numbers[start, i] + "," );
// 從上到下列印一列
if (start < endY)
for ( int i = start + 1; i <= endY; i++)
Console.Write(numbers[i, endX] + "," );
// 從右到左列印一行
if (start < endX && start < endY)
for ( int i = endX - 1; i >= start; i--)
Console.Write(numbers[endY, i] + "," );
// 從下到上列印一行
if (start < endX && start < endY - 1)
for ( int i = endY - 1; i >= start + 1; i--)
Console.Write(numbers[i, start] + "," );
}
#endregion
|
Java實現:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
/** 順時針列印矩陣 * 輸入一個矩陣,按照從外向裡以順時針的順序依次列印出每一個數字。
*/
public static void printMatrixClockwisely( int [][] numbers, int columns, int rows){
if (numbers == null || columns <= 0 || rows <= 0 )
return ;
int start = 0 ;
while (columns > start * 2 && rows > start * 2 ){
printMatrixInCircle(numbers, columns, rows, start);
start++;
}
}
private static void printMatrixInCircle( int [][] numbers, int columns, int rows, int start)
{
int endX = columns - 1 - start;
int endY = rows - 1 - start;
// 從左到右列印一行
for ( int i = start; i <= endX; i++)
System.out.print(numbers[start][i] + "," );
// 從上到下列印一列
if (start < endY)
for ( int i = start + 1 ; i <= endY; i++)
System.out.print(numbers[i][endX] + "," );
// 從右到左列印一行
if (start < endX && start < endY)
for ( int i = endX - 1 ; i >= start; i--)
System.out.print(numbers[endY][i] + "," );
// 從下到上列印一行
if (start < endX && start < endY - 1 )
for ( int i = endY - 1 ; i >= start + 1 ; i--)
System.out.print(numbers[i][start] + "," );
}
|
Python實現:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
def printMatrixClockWisely(numbers, columns, rows):
"""
順時針列印矩陣
輸入一個矩陣,按照從外向裡以順時針的順序依次列印出每一個數字。
:param numbers:
:param columns:
:param rows:
:return:
"""
if numbers = = None or columns < = 0 or rows < = 0 :
return
start = 0
while columns > start * 2 and rows > start * 2 :
printMatrixInCircle(numbers, columns, rows, start)
start + = 1
def printMatrixInCircle(numbers, columns, rows, start):
endX = columns - 1 - start
endY = rows - 1 - start
# 從左到右列印一行
for i in range (start, endX + 1 ):
print (numbers[start][i], end = ', ' )
# 從上到下列印一列
if start < endY:
for i in range (start + 1 , endY + 1 ):
print (numbers[i][endX], end = ', ' )
# 從右到左列印一行
if start < endX and start < endY:
for i in range (endX - 1 , start - 1 , - 1 ):
print (numbers[endY][i], end = ', ' )
# 從下到上列印一行
if start < endX and start < endY - 1 :
for i in range (endY - 1 , start, - 1 ):
print (numbers[i][start], end = ', ' )
|
本文轉自 許大樹 51CTO部落格,原文連結:http://blog.51cto.com/abelxu/1973293,如需轉載請自行聯絡原作者
相關文章
- JZ-019-順時針列印矩陣矩陣
- 劍指offer-19:順時針列印矩陣矩陣
- 劍指offer面試題29:順時針列印矩陣面試題矩陣
- Python練習-LeetCode 第1篇 順時針列印矩陣PythonLeetCode矩陣
- 力扣 - 劍指 Offer 29. 順時針列印矩陣力扣矩陣
- 劍指 Offer 29-順時針列印矩陣c++矩陣C++
- [每日一題] 第二十一題:順時針列印矩陣每日一題矩陣
- 第四章:多維陣列和矩陣 --------------- 4.1 基礎題:順時針列印二維陣列陣列矩陣
- 劍指offer之順序列印陣列陣列
- 巨大的矩陣(矩陣加速)矩陣
- 鄰接矩陣、度矩陣矩陣
- 愛奇藝矩陣管理工具,多個矩陣同時管理矩陣
- 奇異矩陣,非奇異矩陣,偽逆矩陣矩陣
- 資料結構:陣列,稀疏矩陣,矩陣的壓縮。應用:矩陣的轉置,矩陣相乘資料結構陣列矩陣
- 矩陣矩陣
- 求任意矩陣的伴隨矩陣矩陣
- 矩陣和陣列矩陣陣列
- 理解矩陣矩陣
- 海浪矩陣矩陣
- 矩陣相乘矩陣
- 稀疏矩陣矩陣
- 螺旋矩陣矩陣
- 矩陣乘法矩陣
- 8.6 矩陣?矩陣
- 找矩陣矩陣
- 矩陣分解矩陣
- 快手矩陣管理平臺,矩陣管理有方法矩陣
- 第四章:多維陣列和矩陣 ------------- 4.3 基礎題:Z形列印二位陣列陣列矩陣
- 機器學習中的矩陣向量求導(五) 矩陣對矩陣的求導機器學習矩陣求導
- 矩陣:如何使用矩陣操作進行 PageRank 計算?矩陣
- 演算法學習:矩陣快速冪/矩陣加速演算法矩陣
- 每日一題/005/矩陣/數學歸納法/設A的順序主子式均不為0.則有下三角矩陣B,使得BA是上三角矩陣,每日一題矩陣
- 高斯消除矩陣矩陣
- 矩陣求導矩陣求導
- 置換矩陣矩陣
- 視訊矩陣矩陣
- 矩陣樹定理矩陣
- leetcode:螺旋矩陣LeetCode矩陣
- 矩陣置0矩陣