JZ19 顺时针打印矩阵 python

JZ19 顺时针打印矩阵

在这里插入图片描述
可以将矩阵看成若干层,首先打印最外层的元素,其次打印次外层的元素,直到打印最内层的元素。
对于每层,从左上方开始以顺时针的顺序遍历所有元素。假设当前层的左上角位于 (top,left),右下角位于(bottom,right),按照如下顺序遍历当前层的元素。
1、从左到右遍历上侧元素,依次为(top,left) 到(top,right)。
2、从上到下遍历右侧元素,依次为(top+1,right) 到 (bottom,right)。
3、如果 left<right 且top<bottom,则从右到左遍历下侧元素,依次为(bottom,right−1) 到 (bottom,left+1),以及从下到上遍历左侧元素,依次为(bottom,left) 到(top+1,left)。
遍历完当前层的元素之后,将left 和top 分别增加 1,将 right 和 bottom 分别减少 11,进入下一层继续遍历,直到遍历完所有元素为止。

class Solution:
    # matrix类型为二维列表,需要返回列表
    def printMatrix(self, matrix):
        if not matrix:
            return 
        top = 0
        left = 0
        bottom = len(matrix)-1
        right = len(matrix[0])-1
        count = len(matrix)*len(matrix[0])
        ret = []
        while count:
            # 左->右
            for i in range(left, right+1):
                ret.append(matrix[top][i])
                count -= 1
            if not count:
                break
            # 上->下
            for i in range(top+1, bottom+1):
                ret.append(matrix[i][right])
                count -= 1
            if not count:
                break
            # 右->左
            for i in range(right-1, left-1, -1):
                ret.append(matrix[bottom][i])
                count -= 1
            if not count:
                break
            # 下->上
            for i in range(bottom-1, top, -1):
                ret.append(matrix[i][left])
                count -= 1
            if not count:
                break
            top += 1
            right -= 1
            bottom -= 1
            left += 1
        return ret
class Solution:
    # matrix类型为二维列表,需要返回列表
    def printMatrix(self, matrix):
        if not matrix:
            return 
        top = 0
        left = 0
        bottom = len(matrix)-1
        right = len(matrix[0])-1
        ret = []
        while True:
            # 左->右
            for i in range(left, right+1):
                ret.append(matrix[top][i])
            top += 1
            if top > bottom:
                break
            # 上->下
            for i in range(top, bottom+1):
                ret.append(matrix[i][right])
            right -= 1
            if right < left:
                break
            # 右->左
            for i in range(right, left-1, -1):
                ret.append(matrix[bottom][i])
            bottom -= 1
            if bottom < top:
                break
            # 下->上
            for i in range(bottom, top-1, -1):
                ret.append(matrix[i][left])
            left += 1
            if left > right:
                break
        return ret

矩阵可由左上和右下两个坐标确定

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,问题是关于在一个二维数组中查找一个整数的。这个二维数组的特点是每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。给定一个目标整数,要判断数组中是否存在该整数。 根据引用中的代码,可以使用两个指针i和j来遍历二维数组,从右上角开始。如果当前元素等于目标整数,则返回True;如果当前元素大于目标整数,则向左移动j;如果当前元素小于目标整数,则向下移动i。重复这个过程直到找到目标整数或者遍历完整个数组。如果遍历完整个数组都没有找到目标整数,则返回False。 根据引用中的思路1,可以先判断数组是否为空。如果数组为空,则直接返回False。然后遍历每一行,如果当前行的第一个数大于目标整数,则说明该行及其之后的行都不存在目标整数,可以直接退出循环。如果当前行的最后一个数小于目标整数,则说明该行的所有数都比目标整数小,继续查找下一行。否则,在当前行中比对查找,如果找到目标整数,则返回True。如果遍历完整个数组都没有找到目标整数,则返回False。 所以,根据以上的方法和思路,可以用以下的代码来实现: ``` class Solution: def Find(self, target, array): if len(array) == 0: return False elif len(array > target: break if array[i][n-1 < target: continue else: for item in array[i]: if item == target: return True return False ``` 这样,就可以通过调用`Solution`类中的`Find`方法来判断二维数组中是否存在目标整数。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [JZ1 二维数组中的查找 python](https://blog.csdn.net/qq_42187809/article/details/119544063)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [python JZ4 二维数组中的查找(剑指offer)](https://blog.csdn.net/qq_45894553/article/details/121388712)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值