leetcode-面试题13-机器人的运动范围
题目描述:
方法一:dfs/bfs
class Solution: def movingCount(self, m: int, n: int, k: int) -> int: def digitsum(n): ans = 0 while n: ans += n %10 n//=10 return ans from queue import Queue q = Queue() q.put((0,0)) s = set() while not q.empty(): x,y = q.get() if (x,y) not in s and 0 <=x <m and 0<=y<n and digitsum(x)+digitsum(y) <=k: s.add((x,y)) for nx,ny in [(x+1,y),(x,y+1)]: q.put((nx,ny)) return len(s)
方法二:递推
class Solution: def movingCount(self, m: int, n: int, k: int) -> int: def digitsum(n): ans = 0 while n: ans += n %10 n//=10 return ans vis = set([(0,0)]) for i in range(m): for j in range(n): if ((i-1,j) in vis or (i,j-1) in vis) and digitsum(i) + digitsum(j) <= k: vis.add((i,j)) return len(vis)
相关推荐
quyunfei 2020-11-19
机器人智力研究 2020-11-18
聊天终结者机器人 2020-11-18
txq0 2020-11-20
zCSDN 2020-11-09
机器人智力研究 2020-11-05
ARMOTO机器人 2020-11-06
txq0 2020-11-06
遇见人工智能 2020-11-03
聊天终结者机器人 2020-11-02
clliuhust 2020-10-30
yatou0 2020-10-29
雨燕 2020-10-29
nodid 2020-10-29
yatou0 2020-10-29
zCSDN 2020-10-27
dhyddy 2020-10-27
聊天终结者机器人 2020-10-26