-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.py
29 lines (23 loc) · 931 Bytes
/
11.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# -*- coding: utf-8 -*-
class Solution(object):
def movingCount(self, threshold, rows, cols):
flag = [[False] * cols for i in range(rows)]
return self.helper(0, 0, threshold, rows, cols, flag)
def helper(self, x, y, threshold, rows, cols, flag):
if x < 0 or x >= rows or \
y < 0 or y >= cols or + \
self.numSum(x) + self.numSum(y) > threshold or \
flag[x][y]:
return 0
flag[x][y] = True
return self.helper(x, y + 1, threshold, rows, cols, flag) + \
self.helper(x, y - 1, threshold, rows, cols, flag) + \
self.helper(x - 1, y, threshold, rows, cols, flag) + \
self.helper(x + 1, y, threshold, rows, cols, flag) + 1
def numSum(self, num):
sum = 0
while num != 0:
sum += num % 10
num /= 10
return sum
# *注意Python中如何创建二维数组*