1class Solution:2def isSquare(self, n: int) -> bool:3sq = int(math.sqrt(n))4return sq * sq == n56def numSquares(self, n: int) -> int:7# Lagrange's four-square theorem8if self.isSquare(n):9return 110while (n & 3) == 0:11n >>= 212if (n & 7) == 7:13return 414sq = int(math.sqrt(n)) + 115for i in range(1, sq):16if self.isSquare(n - i * i):17return 218return 3