题目如下:

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:

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

Example 2:

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

Note:

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

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

代码如下:

  1. class Solution(object):
  2. def numSquarefulPerms(self, A):
  3. """
  4. :type A: List[int]
  5. :rtype: int
  6. """
  7. import math
  8. def is_sqr(n):
  9. a = int(math.sqrt(n))
  10. return a * a == n
  11. queue = []
  12. for v in ((list(set(A)))):
  13. inx = A.index(v)
  14. queue.append((v,A[:inx] + A[inx+1:]))
  15.  
  16. res = 0
  17. while len(queue) > 0:
  18. last,choice = queue.pop(0)
  19. if len(choice) == 0:
  20. res += 1
  21. for i,v in enumerate(list(set(choice))):
  22. if is_sqr(last+v):
  23. inx = choice.index(v)
  24. queue.append((v,choice[:inx] + choice[inx+1:]))
  25. 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. js 自定义map

    <script> function HashMap(){this.map = {};} HashMap.prototype = { put : function(key, value){ ...

  2. 【CF1257A】Two Rival Students【思维】

    题意:给你n个人和两个尖子生a,b,你可以操作k次,每次操作交换相邻两个人的位置,求问操作k次以内使得ab两人距离最远是多少 题解:贪心尽可能的将两人往两边移动,总结一下就是min(n-1,|a-b| ...

  3. AT2000 Leftmost Ball(计数dp+组合数学)

    传送门 解题思路 设\(f[i][j]\)表示填了\(i\)个白色,\(j\)种彩色的方案数,那么显然\(j<=i\).考虑这个的转移,首先可以填一个白色,就是\(f[i][j]=f[i-1][ ...

  4. [CSP-S模拟测试]:喝喝喝(模拟)

    题目描述 奥利维尔和雪拉扎德在喝酒.两人连喝$18$瓶后,奥利维尔最终倒下了.奥利维尔服用了教会研究的醒酒药后,因为服用了太多产生了副作用,第二天睡不着了.他只好用数数的方式度过无聊的时光,不过他毕竟 ...

  5. 2018-2019-2 20175120 实验四《Android程序设计》实验报告

    任务一:Android Studio的安装测试 任务要求:参考<Java和Android开发学习指南(第二版)(EPUBIT,Java for Android 2nd)>第二十四章: 参考 ...

  6. eclipse项目(java project)如何导入jar包的解决方案列表?

    右键项目-properties-java build path(左侧菜单)-选择libraries 有两种方式,导入jar包实际上就是建立一种链接,并不是copy式的导入 一.导入外部包,add ex ...

  7. Qt/Qte/Qtopia三者的区别

    Qt泛指 Qt software的所有版本的图像界面库,包括 Qt/X11(Unix/Linux),Qt Windows, Qt Mac 等,但这只是相对于二进制来说的.Qt作为一个跨平台的GUI 框 ...

  8. error C2065: ‘_bstr_t’ : undeclared identifier

    转自VC错误:http://www.vcerror.com/?p=828 问题描述: error C2065: '_bstr_t' : undeclared identifier 解决方法: 详细的解 ...

  9. vue 点击其他地方隐藏dom

    document.addEventListener('click', function (e) { if (document.getElementsByClassName('keywordContai ...

  10. 获取header中content-type的值

    后台传过来的值需要根据content-Type的值来判定成功与否 获取header中content-Tyep的值 用res.header['Content-Type']