Wins the Offer (XIX): Clockwise Matrix Print
Search micro-channel public number: 'AI-ming3526' or 'this small computer vision' for more algorithms, machine learning, dry
CSDN: https://blog.csdn.net/baidu_31657889/
GitHub: https://github.com/ aimi-cn / AILearners
First, the primer
This series is my brush "to prove safety Offer" brush off the cattle in question notes online, it aims to enhance the ability under its own algorithm.
View the complete algorithm to prove safety issues resolved Offer Click: to prove safety Offer complete analytical exercises
Second, the title
Enter a matrix, in order from outside to inside in a clockwise order to print out sequentially for each number, for example, if you enter the following 4 X 4 matrix: 1,234,567,891,011,121,314 15 sequentially printed out 16 digital 1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
1, ideas
This question first of all we must first figure out what he meant was, we can draw a map on their first know what he meant, that is, as shown clockwise print out ~
Simulation can cube rotates counterclockwise, removing operation has been done in the first row -
E.g:
1 2 3
4 5 6
7 8 9
After the output and deletes the first line, then counterclockwise rotation, it becomes:
. 9. 6
. 5. 8
. 4. 7
continues to repeat the operation. Done, the code implements a thin ~
2, programming
python2.7
Code implementation:
# -*- coding:utf-8 -*-
class Solution:
# matrix类型为二维列表,需要返回列表
def printMatrix(self, matrix):
# write code here
result = []
while(matrix):
# list.pop详细介绍请看:https://www.runoob.com/python/att-list-pop.html
result+=matrix.pop(0)
if not matrix or not matrix[0]:
break
matrix = self.turn(matrix)
return result
def turn(self,matrix):
num_r = len(matrix)
num_c = len(matrix[0])
newmat = []
for i in range(num_c):
newmat2 = []
for j in range(num_r):
newmat2.append(matrix[j][i])
newmat.append(newmat2)
newmat.reverse()
return newmat
AIMI-CN AI learning exchange group [1015286623] For more information on AI
scan code plus group:
Sharing technology, fun in life: our number of public computer vision this small push "AI" series News articles per week, welcome your interest!