题目如下:

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. Struts2基础-4 -struts拦截器

    Struts2拦截器工作原理 拦截器围绕着 Action和 Result的执行而执行. Struts2拦截器的工作方式如图10.2所示.从上图中可以看出, Struts2拦截器的实现原理和 Servl ...

  2. oracle集合的应用

    union:求并集,公共部分只包含一个 ABC 和AB都没有显示出来 2:union all 相同的数据会包含两个 3:交集 intersect 既包含A又包含B 4:求差集  A集合中的数据B集合中 ...

  3. PHP curl_file_create函数

    curl_file_create — 创建一个 CURLFile 对象. 说明 CURLFile curl_file_create ( string $filename [, string $mime ...

  4. JS中数据结构之集合

    集合(set)是一种包含不同元素的数据结构.集合中的元素称为成员.集合的两个最重要特性是:首先,集合中的成员是无序的:其次,集合中不允许相同成员存在.当你想要创建一个数据结构用来保存一些独一无二的元素 ...

  5. html标签<td><tr><th>全称及缩写说明

    <td> 是table data cell 的缩写,单元格 <tr> 是table row 的缩写,表格中的一行 <th> 是table header cell 的 ...

  6. webbrowser 防止读取 缓存

    http://bbs.csdn.net/topics/240011502 引用 3 楼 kelei0017 的回复: Delphi(Pascal) codeprocedure TInformation ...

  7. 基于windows下,node.js之npm

    1.下载node.js一路安装下去 在开始 node文件夹下,打开cmd 2.创建一个开发目录 mkdir reactQa && cd reactQa 3.初始化一个nmp的开发环境 ...

  8. 【读书笔记】:MIT线性代数(4):Independence, Basis and Dimension

    Independence: The columns of A are independent when the nullspace N (A) contains only the zero vecto ...

  9. ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

    https://blog.csdn.net/ztf312/article/details/50708302 报错原因: Numpy对逻辑表达式判别不清楚,它可以返回False如果等号两边两个式子是数值 ...

  10. Https socket 连接

    介: 本文主要介绍了网络安全通讯协议 SSL/TLS 和 Java 中关于安全通讯的实现部分.并通过一个简单的样例程序实现,来展示如何在 Java 平台上正确建立安全通讯. 在人类建立了通信系统之后, ...