【leetcode】996. Number of Squareful Arrays
题目如下:
Given an array
Aof 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
A1andA2differ if and only if there is some indexisuch thatA1[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: 1Note:
1 <= A.length <= 120 <= 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的更多相关文章
- 【LeetCode】996. Number of Squareful Arrays 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
- 【LeetCode】Single Number I & II & III
Single Number I : Given an array of integers, every element appears twice except for one. Find that ...
- leetcode 996. Number of Squareful Arrays
给定一个长度小于 12 的数组 要求排列方式的种数 使得相邻和为完全平方 不考虑数学结构 将问题转化为 一笔画问题 和为完全平方代表 之间存在通路 回溯法 N^N 记忆化搜索 NN 2^N 判断是否是 ...
- 996. Number of Squareful Arrays
Given an array A of non-negative integers, the array is squareful if for every pair of adjacent elem ...
- 【LeetCode】476. Number Complement (java实现)
原题链接 https://leetcode.com/problems/number-complement/ 原题 Given a positive integer, output its comple ...
- 【LeetCode】624. Maximum Distance in Arrays 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 大根堆+小根堆 保存已有的最大最小 日期 题目地址:h ...
随机推荐
- Ubuntu分区小知识与分区方案
Most PC operating systems still work with an ancient disk partition scheme that historically makes d ...
- Pycharm的debug单步调试
首先设置断点,点击行号显示的一侧,在指定行设置断点.比如这里,我们设断点在创建对象时: .如果这时我们直接右键run这个代码.他还是会直接运行,不会理会断点 结果 所以要右键选择run下面的debug ...
- 第二周作业—N42-虚怀若谷
一.描述Linux发行版的系统目录名称命名规则以及用途 1.文件名命名规则 (1).文件名最长255字节 (2).包括路径在内的文件名称最长4095个字节 (3).除了斜扛和NUL,所有字符都有效.但 ...
- 使用Docker搭建Cloudera Hadoop 环境搭建
单节点 单节点:https://hub.docker.com/r/cloudera/quickstart/ 相关命令 docker pull cloudera/quickstart:latest do ...
- 误删SQL Server日志文件后怎样附加数据库
SQL Server日志文件因为误操作被删除,当附加数据库的时候提示:附加数据库失败. 解决办法如下: 1.新建一个同名数据库. 2.停止数据库服务,覆盖新建的数据库主文件(小技巧:最好放在同一个磁盘 ...
- Buuctf | sqli-labs
这个是赵师傅给我们提供的训练靶场,最好都打一遍,但是出于找flag的角度,特此记录一下,flag在哪里[没错,我就是喜欢我的蓝变红,哈] ?id=1' :报错,说明就是用这个闭合的 ?id=0' un ...
- Tomcat安装后修改路径方法
tomcat+mysql+jdk 本地已经安装tomcat.mysql.jdk服务,需要更改安装目录由D盘改为C盘,方法如下 一.停止tomcat.mysql服务 二.安装文件由D盘拷贝到C盘,原D盘 ...
- TList TObjectList的区别和使用
所在的单元 TList(Classes.pas) TObjectList(Contnrs.pas) TObjectList对象的创建方法有一个参数: constructor TObjectList.C ...
- 笔记大神推荐的个人知识文档管理工具mybase
铛铛铛,今天我要给大家推荐一款个人知识笔记管理神器,不出你们所料,它就是mybase. 那mybase究竟能干啥呢?借用mybase中文官网的一句话来说,mybase软件可以将电脑上的文档.知识.笔记 ...
- css控制文本对齐
h1 {text-align:center;} p.date {text-align:right;} p.main {text-align:justify;} text-decoration 属性用来 ...