1 class Solution: 2 def tictactoe(self, moves: List[List[int]]) -> str: 3 n = len(moves) 4 rowA = [0,0,0] 5 rowB = [0,0,0] 6 colA = [0,0,0] 7 colB = [0,0,0] 8 diaA,diaB,a_diaA,a_diaB =0,0,0,0 9 for i in range(n): 10 x,y = moves[i][0],moves[i][1] 11 if i % 2 == 0:#turn A 12 rowA[x] += 1 13 colA[y] += 1 14 if x == y: 15 diaA += 1 16 if x + y == 2: 17 a_diaA += 1 18 else:#turn B 19 rowB[x] += 1 20 colB[y] += 1 21 if x == y: 22 diaB += 1 23 if x + y == 2: 24 a_diaB += 1 25 if diaA == 3 or a_diaA == 3: 26 return 'A' 27 if diaB == 3 or a_diaB == 3: 28 return 'B' 29 for j in range(3): 30 if rowA[j] == 3 or colA[j] == 3: 31 return 'A' 32 if rowB[j] == 3 or colB[j] == 3: 33 return 'B' 34 if n == 9: 35 return 'Draw' 36 return 'Pending' 37 38 39
使用多个变量,存储行、列、对角线、反对角线等信息。