剑指offer:顺时针打印矩阵

题目

输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 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.

解题思路

  1. 往res中放入【左到右】的值;
  2. 往res中放入【上到下】的值;
  3. 往res中放入【右到左】的值;
  4. 往res中放入【下到上】的值;
  5. 需要设置好边界,这份代码有些案例会多出来一些值,但return res[:w*h] #len(matrix) 就可以。
# -*- coding:utf-8 -*-
class Solution:
    # matrix类型为二维列表,需要返回列表
    def printMatrix(self, matrix):
        # write code here
        res=[]
        if len(matrix)==0:
            return res
        elif len(matrix)==1:
            return matrix[0]
        elif len(matrix)==2:
            res.extend(matrix[0])
            res.extend(matrix[1][::-1])
            return res
        w = len(matrix[0])
        h = len(matrix)
        hs=0
        ws=0
        for i in range(int(h/2)):
            res.extend(matrix[hs][ws:w-ws])
            hs=hs+1
            for j in range(hs,h-hs):
                res.append(matrix[j][w-ws-1])
            res.extend(matrix[h-hs][ws:w-ws][::-1])
            #j=h-hs-1
            if ws!=w - ws - 1:
                for j in range(h - hs - 1, hs - 1, -1):
                    res.append(matrix[j][ws])
            if ws==w - ws - 1:
                break
            ws = ws + 1
        if h%2==1 and h<=w:
            if w-ws-1>ws+1:
                res.extend(matrix[int(h/2)][ws:w-ws])
            else:
                res.append(matrix[int(h/2)][ws])
        #if h%2!=0 and w%2!=0 and h>1 and w>1:
         #   res.append(matrix[h/2][w/2])
        return res[:w*h] #len(matrix)

剑指offer的思路:一个圈由四步组成,除了第一步从左到右,其他的三步都需要进行判断是否超过边界条件,超过则不进行。

  1. 依次放入的值顺序都一样,但它的边界条件设置得比较科学。
  2. 第一步的终止条件是起始列大于等于终止列;
  3. 第二步的终止条件是起始行大于等于终止行 和 起始列大于等于终止列;
  4. 第三步的终止条件是起始行大于等于终止行 和 起始列大于等于终止列。
# -*- coding:utf-8 -*-
class Solution:
    # matrix类型为二维列表,需要返回列表
    def printMatrix(self, matrix):
        # write code here
        res=[]
        if len(matrix)==0:
            return res
        elif len(matrix)==1:
            return matrix[0]
        elif len(matrix)==2:
            res.extend(matrix[0])
            res.extend(matrix[1][::-1])
            return res
        w = len(matrix[0])
        h = len(matrix)
        hs=0
        ws=0
        he, we = h,w
        while hs*2<h and ws*2<w:
            res.extend(matrix[hs][ws:we])
            hs=hs+1
            if hs<he:
                for j in range(hs,he):
                    res.append(matrix[j][we-1])
            we=we-1
            if ws<we and hs<he:
                res.extend(matrix[he-1][ws:we][::-1])
            he=he-1
            if hs < he and ws < we:
                for j in range(he-1, hs-1, -1):
                    res.append(matrix[j][ws])
            ws=ws+1
        
        return res#[:w*h] #len(matrix)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值