54. Spiral Matrix 与59. Spiral Matrix II python

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.

Example 1:

Input:
[
 [ 1, 2, 3 ],
 [ 4, 5, 6 ],
 [ 7, 8, 9 ]
]
Output: [1,2,3,6,9,8,7,4,5]

Example 2:

Input:
[
  [1, 2, 3, 4],
  [5, 6, 7, 8],
  [9,10,11,12]
]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]

大概题意:顺时针打印二维数组

每次打印的开始都是从正对角线的位置,(start_x,start_y)(0,0)(1,1),(2,2)等,每次是否需要打印的条件是row>start_x*2,column>start_y*2.

每个方向打印之前需要判断是否满足打印条件

class Solution(object):
    def spiralOrder(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: List[int]
        """
        if(matrix==[]):
            return []
        start=0
        result=[]
        row=len(matrix)
        column=len(matrix[0])
        while(row>start*2 and column>start*2):
            result+=self.manycolumns(matrix,start,row,column)
            print(result)
            start+=1
            
        return result
        
        
        
    def manycolumns(self,matrix,start,row,column):
        result=[]
        endx=row-start-1
        endy=column-start-1
        for i in range(start,endy+1):
            result.append(matrix[start][i])
        print('1',result)
            
        if(start<endx):
            for i in range(start+1,endx+1):
                result.append(matrix[i][endy])
            print('2',result)
        if(start<endy and start <endx):
            for i in range(endy-1,start-1,-1):
                result.append(matrix[endx][i])
            print('3',result)
        if(start<endy and start<endx-1):
            for i in range(endx-1,start,-1):
                result.append(matrix[i][start])
            print('4',result)
        return result
    

Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.

Example:

Input: 3
Output:
[
 [ 1, 2, 3 ],
 [ 8, 9, 4 ],
 [ 7, 6, 5 ]
]

大概题意:循环写入数据

思路和上提一样

class Solution(object):
    def generateMatrix(self, n):
        """
        :type n: int
        :rtype: List[List[int]]
        """
        
        result=[[0 for i in range(n)] for j in range(n)]
        start=0
        count=1
        while(n>start*2):
            result,count=self.get_result(n,start,result,count)
            start+=1
        return result
    def get_result(self,n,start,result,count):
        end=n-start-1
        for i in range(start,end+1):
            result[start][i]=count
            count+=1
        if(start<end):
            for i in range(start+1,end+1):
                result[i][end]=count
                count+=1
        if(start<end):
            for i in range(end-1,start-1,-1):
                result[end][i]=count
                count+=1
        if(start<end-1):
            for i in range(end-1,start,-1):
                result[i][start]=count
                count+=1
        return result,count
        
        
        
        

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值