题目如下:

Given an array A of non-negative integers, the array is squareful if for every pair of adjacent elements, their sum is a perfect square.

Return the number of permutations of A that are squareful.  Two permutations A1 and A2 differ if and only if there is some index i such that A1[i] != A2[i].

Example 1:

Input: [1,17,8]
Output: 2
Explanation:
[1,8,17] and [17,8,1] are the valid permutations.

Example 2:

Input: [2,2,2]
Output: 1

Note:

  1. 1 <= A.length <= 12
  2. 0 <= A[i] <= 1e9

解题思路:因为A的长度最大才12,直接BFS计算就可以了。依次把[已经选好的数字中最后一个,可以选择的数字列表]加入队列即可。这里要注意去重,在可以选择的数字列表中可以存在重复,如果重复的数字恰好可以与已经选好的数字中最后一个的和可以开平方,那么重复的数字中只能选择一个加入队列。

代码如下:

class Solution(object):
def numSquarefulPerms(self, A):
"""
:type A: List[int]
:rtype: int
"""
import math
def is_sqr(n):
a = int(math.sqrt(n))
return a * a == n
queue = []
for v in ((list(set(A)))):
inx = A.index(v)
queue.append((v,A[:inx] + A[inx+1:])) res = 0
while len(queue) > 0:
last,choice = queue.pop(0)
if len(choice) == 0:
res += 1
for i,v in enumerate(list(set(choice))):
if is_sqr(last+v):
inx = choice.index(v)
queue.append((v,choice[:inx] + choice[inx+1:]))
return res

【leetcode】996. Number of Squareful Arrays的更多相关文章

  1. 【LeetCode】996. Number of Squareful Arrays 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  2. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  3. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  4. 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)

    [LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...

  5. 【LeetCode】Single Number I & II & III

    Single Number I : Given an array of integers, every element appears twice except for one. Find that ...

  6. leetcode 996. Number of Squareful Arrays

    给定一个长度小于 12 的数组 要求排列方式的种数 使得相邻和为完全平方 不考虑数学结构 将问题转化为 一笔画问题 和为完全平方代表 之间存在通路 回溯法 N^N 记忆化搜索 NN 2^N 判断是否是 ...

  7. 996. Number of Squareful Arrays

    Given an array A of non-negative integers, the array is squareful if for every pair of adjacent elem ...

  8. 【LeetCode】476. Number Complement (java实现)

    原题链接 https://leetcode.com/problems/number-complement/ 原题 Given a positive integer, output its comple ...

  9. 【LeetCode】624. Maximum Distance in Arrays 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 大根堆+小根堆 保存已有的最大最小 日期 题目地址:h ...

随机推荐

  1. Linux 统计文件夹下文件个数及目录个数

    1. 统计文件夹下文件的个数 ls -l | grep "^-" | wc -l 2.统计文件夹下目录的个数 ls -l | grep "^d" | wc -l ...

  2. C++ 概率算法 利用蒙特卡罗算法计算圆周率

    概率算法大致可分为4种形式: 数值概率算法: 蒙特卡罗算法: 拉斯维加斯算法: 舍伍德算法: 计算蒙特卡罗概率的算法实现: #include "stdio.h" #include ...

  3. python环境变量

    下载并升级更新pip python -m pip install -U pip 变量名:PY_HOME   变量值:python路径 path:win10加在最后(记得用;号隔开):win7加在前面记 ...

  4. Struts2基础-3 -继承ActionSupport接口创建Action控制器+javaBean接收请求参数+ 默认Action配置处理请求错误 + 使用ActionContext访问ServletAPI

    1.目录结构及导入的jar包 2.web.xml 配置 <?xml version="1.0" encoding="UTF-8"?> <web ...

  5. 解决“element表单验证输入的数字检测出来是string”的问题

    form表单: 校验规则: 注意:一.数字类型的验证需要在 v-model 处加上 .number 的修饰符,这是 Vue 自身提供的用于将绑定值转化为 number 类型的修饰符.二.校验中是否添加 ...

  6. 洛谷 4099 [HEOI2013]SAO——树形DP

    题目:https://www.luogu.org/problemnew/show/P4099 结果还是看了题解才会…… 关键是状态,f[ i ][ j ] 表示 i 子树. i 号点是第 j 个出现的 ...

  7. 在使用KVO遇到的一个问题

    在项目开发中定义了一个单例对象RHUserData的对象,RHOLUserInfo类是单例对象的一个property属性,RHOLUserInfo里面有个userId的属性,在其他类里面进行设置KVO ...

  8. YII 1.0 常用CURD写法

    <?php //yii1.0 curd简单写法 //查询 Yii::app()->db->createCommand($sql)->queryAll();//查询所有行数据 ​ ...

  9. Linux系统结构 详解

    Linux系统一般有4个主要部分: 内核.shell.文件系统和应用程序.内核.shell和文件系统一起形成了基本的操作系统结构,它们使得用户可以运行程序.管理文件并使用系统.部分层次结构如图1-1所 ...

  10. anti-nim 游戏

    游戏描述: 桌上有n堆石子,游戏双方轮流取石子,每次只能从一堆中取出任意数目的石子,不能不取,取走最后一个石子者失败. 结论: 先手必胜,当且仅当: ①.所有堆的石子数都为1,且游戏的SG值为0. ② ...