LeetCode开心刷题四十一天——54. Spiral Matrix(目前实现的时空都太慢,麻烦)55. Jump Game(时空不错,判断条件是点睛之笔)59. Spiral Matrix II(...

本文深入探讨了螺旋矩阵的生成与遍历算法,通过具体示例展示了如何按螺旋顺序填充和读取矩阵元素。同时,解析了一个跳跃游戏问题,讨论了如何判断在给定步长条件下能否到达数组末尾的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

54. Spiral Matrix
Medium
1349453FavoriteShare

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]
class Solution(object):
    def spiralOrder(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: List[int]
        """
        if matrix==[]: return []
        up,left,right,down=0,0,len(matrix[0])-1,len(matrix)-1
        direct=0
        res=[]
        while True:
            if direct==0:
                for i in range(left,right+1):
                    res.append(matrix[up][i])
                up+=1
                # 不能在这改变direct值,否则会导致跳进下面的循环
            if direct==1:
                for i in range(up,down+1):
                    res.append(matrix[i][right])
                right-=1
            if direct==2:
                for i in range(right,left-1,-1):
                    res.append(matrix[down][i])
                down-=1
            if direct==3:
                for i in range(down,up-1,-1):
                    res.append(matrix[i][left])
                left+=1
            # 如果不满足上下左右基本规则直接退出
            if up>down or right<left: return res
            # 方向需要对4取余
            direct=(direct+1)%4

solu=Solution()
matrix=[
  [1, 2, 3, 4],
  [5, 6, 7, 8],
  [9,10,11,12]
]
print(solu.spiralOrder(matrix))

 

 

 
Medium
2373227FavoriteShare

Given an array of non-negative integers, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Determine if you are able to reach the last index.

Example 1:

Input: [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.

Example 2:

Input: [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum
             jump length is 0, which makes it impossible to reach the last index.
class Solution(object):
    def canJump(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        # 因为最少也可以走一步。除非值为0,所以在每一步都应该尽可能多的走,看能不能走到终点
        # nums[0]比起0才是真正能走的最远距离
        maxV=nums[0]
        n=len(nums)
        for i in range(n):
            # 如果i比maxV大,说明maxV更新的速度慢,走不到i这一步,因为正常情况应该是比它小的
            if i>maxV:
                break
            maxV=max(i+nums[i],maxV)
        return maxV>=n-1



solu=Solution()
nums=[3,2,1,0,4]
print(solu.canJump(nums))

 

59. Spiral Matrix II
Medium
55086FavoriteShare

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 ]
]
思路还是四个方向。辅助判断方向while True决定,加了count函数,代表实际存储的值
class Solution(object):
    def generateMatrix(self, n):
        """
        :type n: int
        :rtype: List[List[int]]
        """
        if n==0:return []
        matrix=[[0 for i in range(n)]for j in range(n)]
        up,down,left,right=0,n-1,0,n-1
        # 代表方向和个数计数
        direct,count=0,0
        while True:
            if direct==0:
                for i in range(left,right+1):
                    count+=1
                    matrix[up][i]=count
                up+=1
            if direct==1:
                for i in range(up,down+1):
                    count+=1
                    # 注意前后顺序,容易被上面的带跑偏了
                    matrix[i][right]=count
                # 方位不同加减不同
                right-=1
            if direct==2:
                for i in range(right,left-1,-1):
                    count+=1
                    matrix[down][i]=count
                down-=1
            if direct==3:
                for i in range(down,up-1,-1):
                    count+=1
                    matrix[i][left]=count
                left+=1
            if count==n*n:
                return matrix
            direct=(direct+1)%4


solu=Solution()
matrix=[1,2,3,4,5,6,7,8,9]
print(solu.generateMatrix(3))

 

 

转载于:https://www.cnblogs.com/Marigolci/p/11524824.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值