9.15 小米测开题解Python
1. 质因数
从2开始除,能整除就继续,不能整除就+1,直到1为止
def primeFactorization(num): res = [] i = 2 while num > 1: while num % i == 0: res.append(i) num //= i i += 1 return res2. 单词搜索,力扣79
输入格式用
while True: try: ... except: break剩下的回溯就行了
temp = [] while True: try: tmp = input().split(",") temp.append(tmp) except: break board = temp[:len(temp)-1] word = temp[-1][0] def dfs(current,x,y,board): if current == "": return True if board[x][y] == current[0]: board[x][y] = "0" for dx,dy in [[1,0],[-1,0],[0,1],[0,-1]]: newx = x + dx newy = y + dy if 0 <= newx < len(board) and 0 <= newy < len(board[0]): if dfs(current[1:],newx,newy,board): return True board[x][y] = current[0] return False flag = False for i in range(len(board)): for j in range(len(board[0])): if dfs(word,i,j,board): flag = True break if flag: print("true") else: print("false")