classSolution: defminKBitFlips(self, A: 'List[int]', K: 'int') -> 'int': rev = [0]*(len(A)+10) ls = 0 ans = 0 for i in range(len(A)): ls ^= rev[i]# 主要是用来判断该位置需不需要翻转 A[i] ^= ls #该位置之前多次操作最后的结果 if A[i] == 0: ans += 1 ls ^= 1#flag翻转一下 if i+K > len(A): return-1 rev[i+K] = 1# 标记翻转的结束位置 return ans
import copy import math classSolution: defisSqr(self,num): return int(math.sqrt(num))**2 == num
defdfs(self, A, b): if len(A) == 0: return1 ans = 0 for i in range(len(A)): if i==0or A[i] != A[i-1]: ifnot self.isSqr(A[i]+b): continue B = copy.deepcopy(A) B.remove(B[i]) ans += self.dfs(B, A[i]) return ans
defnumSquarefulPerms(self, A: 'List[int]') -> 'int': ans = 0 A.sort() # 排序的原因是为了在搜索时避免重复查找 for i in range(len(A)): if i==0or A[i] != A[i-1]: B = copy.deepcopy(A) B.remove(B[i]) ans += self.dfs(B, A[i]) return ans