# 1. 二维数组中的查找

## 题目描述

``````class Solution:
# array 二维列表
def Find(self, target, array):
rowNum = len(array)
columnNum = len(array[0])
for p in range(rowNum):
for q in range(columnNum):
if target == array[p][q]:
return True
return False
``````

# 2. 替换空格

## 题目描述

``````class Solution:
# s 源字符串
def replaceSpace(self, s):
return s.replace(" ", "%20")
``````

# 3. 从头到尾打印链表

## 题目描述

``````# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
# 返回从尾部到头部的列表值序列，例如[1,2,3]
# write code here
``````

# 4. 重建二叉树

## 题目描述

``````# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
# 返回构造的TreeNode根节点
def reConstructBinaryTree(self, pre, tin):
if len(pre)==0 or len(tin)==0 :
return None
elif len(pre)==1 and len(tin)==1 :
return TreeNode(pre[0])
else:
root = TreeNode(pre[0])
root.left = self.reConstructBinaryTree(pre[1:tin.index(pre[0])+1],tin[0:tin.index(pre[0])])
root.right = self.reConstructBinaryTree(pre[tin.index(pre[0])+1:],tin[tin.index(pre[0])+1:])
return root
``````

# 5. 用两个栈实现队列

## 题目描述

``````class Solution:
def __init__(self):
self.stack1 = []
self.stack2 = []
def push(self, node):
self.stack1.append(node)
def pop(self):
if self.stack2:
return self.stack2.pop()
else:
while self.stack1:
self.stack2.append(self.stack1.pop())
return self.stack2.pop()
``````

# 6. 旋转数组的最小数字

## 题目描述

``````class Solution:
def minNumberInRotateArray(self, rotateArray):
if len(rotateArray) == 0:
return 0
for i in range(len(rotateArray)):
if rotateArray[i] > rotateArray[i+1]:
return rotateArray[i+1]
``````

# 7. 斐波那契数列

## 题目描述

``````class Solution:
def Fibonacci(self, n):
a, b = 0, 1
for i in range(n):
a, b = b, a+b
return a
``````

# 8. 跳台阶

## 题目描述

``````class Solution:
def jumpFloor(self, number):
a, b = 0, 1
for i in range(number+1):
a, b = b, a+b
return a
``````

# 9. 变态跳台阶

## 题目描述

``````class Solution:
def jumpFloorII(self, number):
return 2**(number - 1)
``````

# 10. 矩形覆盖

## 题目描述

``````class Solution:
def rectCover(self, number):
if number == 0:
return 0
a, b = 0, 1
for i in range(number+1):
a, b = b, a+b
return a
``````

# 11. 二进制中1的个数

## 题目描述

``````class Solution:
def NumberOf1(self, n):
count = 0
if n >= 0:
string = str(bin(n).replace('0b',''))
for i in range(len(string)):
if '1' == string[i]:
count += 1
return count
else:
string = str(bin(((1 << 32) - 1) & n)[2:].zfill(32).replace('0b',''))
for i in range(len(string)):
if '1' == string[i]:
count += 1
return count
``````

# 12. 数值的整数次方

## 题目描述

``````class Solution:
def Power(self, base, exponent):
return base ** exponent
``````

# 13. 调整数组顺序使奇数位于偶数前面

## 题目描述

``````class Solution:
def reOrderArray(self, array):
even, odd = [], []
for i in range(len(array)):
if array[i] % 2 == 0:
even.append(array[i])
else:
odd.append(array[i])
return odd + even
``````

# 14. 链表中倒数第k个节点

## 题目描述

``````class Solution:
for i in range(k):
if pre == None:
return None
pre = pre.next
while pre != None:
pre = pre.next
post = post.next
return post
``````

# 15. 反转链表

## 题目描述

``````class Solution:
return None
last = None
return last
``````

# 16. 合并两个排序的链表

## 题目描述

``````class Solution:
else:
``````

# 17. 树的子结构

## 题目描述

``````class Solution:
def Tree1HasTree2(self, tree1, tree2):
if tree2 == None:
return True
if tree1 == None:
return False
if tree1.val != tree2.val:
return False
return self.Tree1HasTree2(tree1.left, tree2.left) and self.Tree1HasTree2(tree1.right, tree2.right)

def HasSubtree(self, pRoot1, pRoot2):
result = False
if pRoot1 != None and pRoot2 != None:
if pRoot1.val == pRoot2.val:
result = self.Tree1HasTree2(pRoot1, pRoot2)
if not result:
result = self.HasSubtree(pRoot1.left, pRoot2)
if not result:
result = self.HasSubtree(pRoot1.right, pRoot2)
return result
``````

# 18. 二叉树的镜像

## 题目描述

``````class Solution:
# 返回镜像树的根节点
def Mirror(self, root):
if root == None:
return None
root.left, root.right = root.right, root.left
if root.left != None:
self.Mirror(root.left)
if root.right != None:
self.Mirror(root.right)
``````

# 19. 顺时针打印矩阵

## 题目描述

``````class Solution:
# matrix类型为二维列表，需要返回列表
def printMatrix(self, matrix):
return matrix and list(matrix.pop(0)) + self.printMatrix(list(zip(*matrix))[::-1])

``````

# 20. 包含min函数的栈

## 题目描述

``````class Solution:
def __init__(self):
self.stack=[]
self.min_stack=[]
self.min_number = float('inf')

def push(self, node):
if node < self.min_number:
self.min_number = node
self.min_stack.append(self.min_number)
self.stack.append(node)

def pop(self):
if self.stack != []:
if self.stack[-1] == self.min_number:
self.min_stack.pop()
self.stack.pop(-1)

def top(self):
if self.stack != []:
return self.stack[-1]
else:
return None

def min(self):
return self.min_stack[-1]

``````

# 21. 栈的压入、弹出序列

## 题目描述

``````class Solution:
def IsPopOrder(self, pushV, popV):
stack = []
for each in pushV:
stack.append(each)
while stack and stack[-1] == popV[0]:
stack.pop()
popV.pop(0)
if stack == []:
return True
else:
return False

``````

# 22. 从上往下打印二叉树

## 题目描述

``````class Solution:
# 返回从上到下每个节点值列表，例：[1,2,3]
def PrintFromTopToBottom(self, root):
node_list = [root]
result = []
if not root:
return result
while node_list:
current_root = node_list.pop(0)
result.append(current_root.val)
if current_root.left:
node_list.append(current_root.left)
if current_root.right:
node_list.append(current_root.right)
return result

``````

# 23. 二叉搜索树的后序遍历序列

## 题目描述

• 若任意节点的左子树不空，则左子树上所有节点的值均小于它的根节点的值；
• 若任意节点的右子树不空，则右子树上所有节点的值均大于它的根节点的值；
• 任意节点的左、右子树也分别为二叉查找树；'

``````class Solution:
def VerifySquenceOfBST(self, sequence):
if len(sequence) == 0:
return False
root = sequence[-1]
for split_index in range(len(sequence)):
if sequence[split_index] > root:
break
for temp in range(split_index, len(sequence)):
if sequence[temp] < root:
return False
left, right = True, True
left = self.VerifySquenceOfBST(sequence[0:split_index])
if left and split_index < len(sequence) - 1:
right = self.VerifySquenceOfBST(sequence[split_index:-1])
return right

``````

# 24. 二叉树中和为某一值的路径

## 题目描述

1. 如果只有根节点或者找到叶子节点，我们就把其对应的val值返回
2. 如果不是叶子节点，我们分别对根节点的左子树、右子树进行递归，直到找到叶子结点。然后遍历把叶子结点和父节点对应的val组成的序列返回上一层；如果没找到路径，其实也返回了序列，只不过是[]
``````class Solution:
# 返回二维列表，内部每个列表表示找到的路径
def FindPath(self, root, expectNumber):
result = []
if not root:
return result
if not root.left and not root.right and root.val == expectNumber:
return [[root.val]]
else:
left = self.FindPath(root.left, expectNumber - root.val)
right = self.FindPath(root.right, expectNumber - root.val)
for item in left + right:
result.append([root.val] + item)
return result
``````

# 25. 复杂链表的复制

## 题目描述

``````class Solution:
return None
# Step 1
while pCur:
node = RandomListNode(pCur.label)
node.next = pCur.next
pCur.next = node
pCur = node.next
# Step 2
while pCur:
if pCur.random != None:
pCur.next.random = pCur.random.next
pCur = pCur.next.next
# Step 3
while pCur:
pCur.next = pCur.next.next
if cur.next != None:
cur.next = cur.next.next
cur = cur.next
pCur = pCur.next

``````

# 26. 二叉搜索树与双向链表

## 题目描述

Illustration

``````class Solution:
def __init__(self):
self.listTail = None

def Convert(self, pRootOfTree):
if pRootOfTree == None:
return
self.Convert(pRootOfTree.left)
self.listTail = pRootOfTree
else:
self.listTail.right = pRootOfTree
pRootOfTree.left = self.listTail
self.listTail = pRootOfTree
self.Convert(pRootOfTree.right)

``````

# 27. 字符串的排列

## 输入描述:

``````class Solution:
def Permutation(self, ss):
result = []
if len(ss) <= 1:
return ss
for i in range(len(ss)):
for j in map(lambda x: ss[i] + x, self.Permutation(ss[:i]+ss[i+1:])):
if j not in result:
result.append(j)
return result

``````

# 28. 数组中出现次数超过一半的数字

## 题目描述

``````class Solution:
def MoreThanHalfNum_Solution(self, numbers):
numbers = sorted(numbers)
count = 0
for each in range(len(numbers)):
if numbers[each] == numbers[len(numbers) // 2]:
count = count + 1
if count > len(numbers)/2:
return numbers[len(numbers) // 2]
else:
return 0
``````

# 29. 最小的k个数

## 题目描述

``````class Solution:
def GetLeastNumbers_Solution(self, tinput, k):
return sorted(tinput)[:k] if len(tinput) >= k else []
``````

# 30. 连续子数组的最大和

## 题目描述

HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学。今天测试组开完会后，他又发话了：在古老的一维模式识别中，常常需要计算连续子向量的最大和，当向量全为正数的时候，问题很好解决。但是，如果向量中包含负数，是否应该包含某个负数，并期望旁边的正数会弥补它呢？例如：{6,-3,-2,7,-15,1,2,2}，连续子向量的最大和为8(从第0个开始，到第3个为止)。给一个数组，返回它的最大连续子序列的和，你会不会被他忽悠住？(子向量的长度至少是1)

``````class Solution:
def FindGreatestSumOfSubArray(self, array):
res = max(array)
temp = 0
for each in array:
temp = max(each, temp + each)
res = max(temp, res)
return res
``````

# 31. 整数中1出现的次数（从1到n整数中1出现的次数）

## 题目描述

``````class Solution:
def NumberOf1Between1AndN_Solution(self, n):
number_string = ''
for each in range(1, n+1):
number_string = number_string + str(each)
return len(number_string) - len(number_string.replace('1', ''))

``````

# 32. 把数组排成最小的数

## 题目描述

``````class Solution:
def PrintMinNumber(self, numbers):
if numbers is None or len(numbers) == 0:
return ''
numbers = map(str, numbers)
numbers.sort(cmp = lambda x, y : cmp(x + y, y + x))
return ''.join(numbers).lstrip()
``````

# 33. 丑数

## 题目描述

``````class Solution:
def GetUglyNumber_Solution(self, index):
if index < 1:
return 0
if index == 1:
return 1
uglyNumberList = [1]
t2, t3, t5 = 0, 0, 0
for i in range(1, index):
if uglyNumberList[t2] * 2 <= uglyNumberList[i-1]:
t2 += 1
if uglyNumberList[t3] * 3 <= uglyNumberList[i-1]:
t3 += 1
if uglyNumberList[t5] * 5 <= uglyNumberList[i-1]:
t5 += 1
uglyNumber = min(uglyNumberList[t2]*2,uglyNumberList[t3]*3,uglyNumberList[t5]*5)
uglyNumberList.append(uglyNumber)
return uglyNumberList[index - 1]

``````

# 34. 第一个只出现一次的字符

## 题目描述

``````class Solution:
def FirstNotRepeatingChar(self, s):
if not s or len(s)>10000:
return -1
for each in s:
if s.count(each) == 1:
return s.index(each)
``````

# 35. 数组中的逆序对

## 示例1

``````class Solution:
def InversePairs(self, data):
count = 0
while len(data)>1:
min_data_index = data.index(min(data))
count += min_data_index
data.pop(min_data_index)
return count%1000000007
``````

# 36. 两个链表的第一个公共结点

## 题目描述

• 如果两个链表长度一样，则正常遍历，找到相同的或者不存在。
• 如果两个链表长度不同，则首先短的遍历结束后会从另一个链表开头开始遍历，而当另一个节点遍历结束后从另一个链表头开始遍历时，这两个链表的差则会消除。
``````class Solution:
while p1 != p2:
p1 = pHead2 if p1 is None else p1.next
p2 = pHead1 if p2 is None else p2.next
return p1

``````

# 37. 数字在排序数组中出现的次数

## 题目描述

``````class Solution:
def GetNumberOfK(self, data, k):
return data.count(k)
``````

# 38. 二叉树的深度

## 题目描述

``````class Solution:
def TreeDepth(self, pRoot):
if pRoot == None:
return 0
left = self.TreeDepth(pRoot.left)
right = self.TreeDepth(pRoot.right)
return max(left, right) + 1

``````

# 39. 平衡二叉树

## 题目描述

``````class Solution:
def IsBalanced_Solution(self, pRoot):
if pRoot == None:
return True
left_depth = self.TreeDepth(pRoot.left)
right_depth = self.TreeDepth(pRoot.right)
if abs(left_depth - right_depth) > 1:
return False
return True

def TreeDepth(self, pRoot):
if pRoot == None:
return 0
left = self.TreeDepth(pRoot.left)
right = self.TreeDepth(pRoot.right)
return max(left, right) + 1

``````

# 40. 数组中只出现一次的数字

## 题目描述

``````class Solution:
# 返回[a,b] 其中ab是出现一次的两个数字
def FindNumsAppearOnce(self, array):
result = []
for each in array:
if array.count(each) == 1:
result.append(each)
return result
``````

# 41. 和为s的连续正数序列

## 输出描述

``````class Solution:
def FindContinuousSequence(self, tsum):
result = []
for i in range(1, tsum // 2 + 1):
t_sum = 0
for j in range(i, tsum // 2 + 2):
t_sum += j
if t_sum == tsum:
result.append(list(range(i,j+1)))
return result
``````

# 42. 和为S的两个数字

## 输出描述

``````class Solution:
def FindNumbersWithSum(self, array, tsum):
result = []
for i in range(len(array)):
for j in range(len(array)-1, i-1, -1):
if array[i] + array[j] == tsum:
result.append(array[i])
result.append(array[j])
return result
return result
``````

# 43. 左旋转字符串

## 题目描述

``````class Solution:
def LeftRotateString(self, s, n):
if s == '':
return ''
s_list = list(s)
for i in range(n):
temp = s_list.pop(0)
s_list.append(temp)
return ''.join(str(i) for i in s_list)
``````

# 44. 翻转单词顺序列

## 题目描述

``````class Solution:
def ReverseSentence(self, s):
s_list = s.split(' ')
return ' '.join(str(i) for i in s_list[::-1])
``````

# 45. 扑克牌顺子

## 题目描述

LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张_)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定去买体育彩票,嘿嘿！！“红心A,黑桃3,小王,大王,方片5”,“Oh My God!”不是顺子.....LL不高兴了,他想了想,决定大\小 王可以看成任何数字,并且A看作1,J为11,Q为12,K为13。上面的5张牌就可以变成“1,2,3,4,5”(大小王分别看作2和4),“So Lucky!”。LL决定去买体育彩票啦。 现在,要求你使用这幅牌模拟上面的过程,然后告诉我们LL的运气如何， 如果牌能组成顺子就输出true，否则就输出false。为了方便起见,你可以认为大小王是0。

``````class Solution:
def IsContinuous(self, numbers):
if len(numbers) != 5:
return False
cards = {}
for each in numbers:
if each == 0:
continue
if each in cards.keys():
return False
else:
cards[each] = 1
if max(cards.keys()) - min(cards.keys()) < 5:
return True
else:
return False

``````

# 46. 孩子们的游戏（圆圈中最后剩下的数）

## 题目描述

``````class Solution:
def LastRemaining_Solution(self, n, m):
if n < 1 or m < 1:
return -1
result = 0
for i in range(2, n+1):
result = (result + m) % i
return result

``````

# 47. 求1+2+3+...+n

## 题目描述

``````class Solution:
def Sum_Solution(self, n):
result = n
temp = (n > 1 and self.Sum_Solution(n - 1))
result = result + temp
return result
``````

# 48. 不用加减乘除做加法

## 题目描述

``````class Solution:
while True:
# 不进位加法
s = num1 ^ num2
# 计算进位
carry = num1 & num2

# 手动把高于 32 位的部分变成 0
num1 = s & 0xFFFFFFFF
num2 = carry << 1

if carry == 0:
break
# 如果是正数和 0 ，就直接返回这个正数
if num1 >> 31 == 0:
return num1
# 如果是负数
return num1 - (1 << 32)
``````

+2147483647
1a33

2147483647
0

# 50. 数组中重复的数字

## 题目描述

``````class Solution:
# 这里要特别注意~找到任意重复的一个值并赋值到duplication[0]
# 函数返回True/False
def duplicate(self, numbers, duplication):
n = {}
for each in numbers:
if each in n.keys():
duplication[0] = each
return True
else:
n[each] = 1
return False

``````

# 51. 构建乘机数组

## 题目描述

``````class Solution:
def multiply(self, A):
if not A:
return []
B = [1 for _ in range(len(A))]
for i in range(1, len(A)):
B[i] = B[i-1] * A[i-1]
temp = 1
for i in range(len(A)-2, -1, -1):
temp *= A[i+1]
B[i] *= temp
return B

``````

# 52. 正则表达式匹配

## 题目描述

``````class Solution:
# s, pattern都是字符串
def match(self, s, pattern):
if s == pattern:
return True
if len(pattern) > 1 and pattern[1] == '*':
if s and (s[0] == pattern[0] or pattern[0] == '.'):
return self.match(s, pattern[2:]) or self.match(s[1:], pattern)
else:
return self.match(s, pattern[2:])
elif s and pattern and (s[0] == pattern[0] or pattern[0] == '.'):
return self.match(s[1:], pattern[1:])
return False
``````

# 54. 字符流中第一个不重复的字符

## 输出描述:

``````class Solution:
def __init__(self):
self.char_dic = {}
self.char_s = ''

def FirstAppearingOnce(self):
for each in self.char_s:
if self.char_dic[each] == 1:
return each
return '#'

def Insert(self, char):
if char not in self.char_dic.keys():
self.char_dic[char] = 1
else:
self.char_dic[char] += 1
self.char_s += char

``````

# 57. 二叉树的下一个结点

## 题目描述

1. 给定的节点为空——返回空；
2. 给定的节点有右子树——沿着该右子树，返回右子树的第一个左叶子节点；
3. 给定的节点没有右子树——如果位于某个节点的左子树中，则上溯直至找到该节点；否则就返回空。
``````class Solution:
def GetNext(self, pNode):
if not pNode:
return None
if pNode.right:
pNode = pNode.right
while pNode.left:
pNode = pNode.left
return pNode
else:
while pNode.next:
if pNode == pNode.next.left:
return pNode.next
pNode = pNode.next
return None
``````

# 58. 对称的二叉树

## 题目描述

``````class Solution:
def isSymmetrical(self, pRoot):
if not pRoot:
return True
return self.checkSymmetrical(pRoot.left, pRoot.right)

def checkSymmetrical(self, left, right):
if not left and not right:
return True
if not left or not right:
return False
if left.val != right.val:
return False
return self.checkSymmetrical(left.left, right.right) and self.checkSymmetrical(left.right, right.left)

``````

# 59. 按之字形顺序打印二叉树

## 题目描述

``````class Solution:
def Print(self, pRoot):
if not pRoot:
return []
i = -1
result_list = []
current_layer = [pRoot]
while current_layer:
i *= -1
current_list = []
next_layer = []
for node in current_layer:
current_list.append(node.val)
if node.left:
next_layer.append(node.left)
if node.right:
next_layer.append(node.right)
result_list.append(current_list[::i])
current_layer = next_layer
return result_list

``````

# 60. 把二叉树打印成多行

## 题目描述

``````class Solution:
# 返回二维列表[[1,2],[4,5]]
def Print(self, pRoot):
if not pRoot:
return []

result_list = []
current_layer = [pRoot]

while current_layer:
current_list = []
next_layer = []
for node in current_layer:
current_list.append(node.val)
if node.left:
next_layer.append(node.left)
if node.right:
next_layer.append(node.right)
result_list.append(current_list)
current_layer = next_layer
return result_list

``````

# 61. 序列化二叉树

## 题目描述

``````class Solution:
def Serialize(self, root):
if not root:
return '#'
return str(root.val) + ',' + self.Serialize(root.left) + ',' + self.Serialize(root.right)

def Deserialize(self, s):
s_list = s.split(',')
return self.DeserializeTree(s_list)

def DeserializeTree(self, s_list):
if len(s_list) == 0:
return None
value = s_list.pop(0)
root = None
if value != '#':
root = TreeNode(int(value))
root.left = self.DeserializeTree(s_list)
root.right = self.DeserializeTree(s_list)
return root
``````

# 62. 二叉搜索树的第k个结点

## 题目描述

``````class Solution:
count = 0
def KthNode(self, pRoot, k):
if not pRoot:
return None
node = self.KthNode(pRoot.left, k)
if node:
return node
self.count += 1
if self.count == k:
return pRoot
node = self.KthNode(pRoot.right, k)
if node:
return node

``````

# 63. 数据流中的中位数

## 题目描述

``````class Solution:
def __init__(self):
self.arr = []
def Insert(self, num):
self.arr.append(num)
self.arr.sort()
def GetMedian(self, n=None):
length = len(self.arr)
if length % 2 == 1:
return self.arr[length//2]
else:
return (self.arr[length//2 - 1] + self.arr[length//2]) / 2.0

``````

# 64. 滑动窗口的最大值

## 题目描述

``````class Solution:
def maxInWindows(self, num, size):
result = []
if not num or len(num) < size or size < 1:
return result
for i in range(len(num)-size + 1):
result.append(max(num[i:i+size]))
return result
``````

# 65. 矩阵中的路径

## 题目描述

``````class Solution:
def hasPath(self, matrix, rows, cols, path):
if len(matrix) == 0 or len(matrix) != rows * cols or len(path) == 0:
return False
visited = [False] * len(matrix)
pathLengthFound = 0
for x in range(cols):
for y in range(rows):
if self.hasPathAlgorithm(matrix, rows, cols, path, x, y, visited, pathLengthFound):
return True
return False

def hasPathAlgorithm(self, matrix, rows, cols, path, x, y, visited, pathLengthFound):
if pathLengthFound == len(path):
return True
current_haspath = False
if 0 <= x <cols and 0 <= y < rows and matrix[y * cols + x] == path[pathLengthFound] and not visited[y * cols + x]:
visited[y * cols + x] = True
pathLengthFound += 1
current_haspath = self.hasPathAlgorithm(matrix, rows, cols, path, x-1, y, visited, pathLengthFound) or self.hasPathAlgorithm(matrix, rows, cols, path, x, y-1, visited, pathLengthFound) or self.hasPathAlgorithm(matrix, rows, cols, path, x+1, y, visited, pathLengthFound) or self.hasPathAlgorithm(matrix, rows, cols, path, x, y+1, visited, pathLengthFound)

if not current_haspath:
pathLengthFound -= 1
visited[y * cols + x] = False
return current_haspath

``````

# 66. 机器人的运动范围

## 题目描述

``````class Solution:
def movingCount(self, threshold, rows, cols):
matrix = [[0 for _ in range(cols)] for _ in range(rows)]
count = self.find_grid(threshold, rows, cols, matrix, 0, 0)
return count

def find_grid(self, threshold, rows, cols, matrix, x, y):
count = 0
if 0 <= x < rows and 0 <= y < cols and matrix[x][y] == 0 and self.judge(threshold, x, y):
matrix[x][y] = 1
count = 1 + self.find_grid(threshold, rows, cols, matrix, x-1, y) + self.find_grid(threshold, rows, cols, matrix, x, y-1) + self.find_grid(threshold, rows, cols, matrix, x+1, y) + self.find_grid(threshold, rows, cols, matrix, x, y+1)
return count

def judge(self, threshold, x, y):
if sum(map(int, str(x) + str(y))) <= threshold:
return True
else:
return False
``````