leetcode题目
class Solution:
def fp(m,n,N,i,j):
if i<0 or j<0 or i>=m or j>=n:return 0
if N==1:
if [i,j] in [[0,0],[0,n-1],[m-1,n-1],[m-1,0]]:
if m==1 or n==1:
return 3
return 2
elif i*j==0 or j==n-1 or i==m-1:
if m==1 or n==1:
return 2
return 1
else:return 0
else:
return Solution.fp(m, n, N-1, i-1, j)+Solution.fp(m, n, N-1, i+1, j)+Solution.fp(m, n, N-1, i, j-1)+Solution.fp(m, n, N-1, i, j+1)
def findPaths(self, m, n, N, i, j):
"""
:type m: int
:type n: int
:type N: int
:type i: int
:type j: int
:rtype: int
"""
res = 0
for x in range(1,N+1):
res+=Solution.fp(m,n,x,i,j)
return res
需要注意的是,在类里面写递归需要加上类名;使用递归的石斛不要用self参数。