版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_36811967/article/details/87931916
注意是从0,0开始的:
# -*- coding:utf-8 -*-
class Solution:
def movingCount(self, threshold, rows, cols):
# write code here
locs = []
def sum_num(a, b):
return sum(list(map(int, list(str(a)))))+sum(list(map(int, list(str(b)))))
def helper(x, y):
if [x, y] in locs:
return
if x<0 or x>=rows or y<0 or y>=cols:
return
if sum_num(x, y) <= threshold:
locs.append([x, y])
helper(x-1, y)
helper(x+1, y)
helper(x, y-1)
helper(x, y+1)
helper(0, 0)
return len(locs)