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

54. Spiral Matrix
Medium
1349 453 Favorite Share

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 rightreturn 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
2373 227 Favorite Share

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
550 86 Favorite Share

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))

 

 

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