【LeetCode】996. Number of Squareful Arrays 解题报告(C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/number-of-squareful-arrays/
题目描述
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 <= A.length <= 12
- 0 <= A[i] <= 1e9
题目大意
给出了一个非负数字组成的数组,如果一个数组是可平方的,那么这个数组每两个相邻的元素的和是一个平方数字。判断给出的数组的所有排列中,有多少个不同的排列是可平方的。
解题方法
回溯法
这个题的问题规模只有12个,也就是提醒我们可以使用O(N!)的算法,所以可以直接使用回溯法。
首先要排序使得相同的数字都排列在一起,这个题的回溯策略是使用visited数组表示每个数字是否用过了,从起点位置0开始,每次向后遍历,如果后面的这个数字没有用过,并且如果前面的数字和它相同、那么前面的数字也没有用过,和前面的数字相加是可以平方的,那么把当前数字放到路径cur中,设置当前的数组访问状态为已访问,然后继续从0开始遍历即可。
这个题虽然是Hard,但是还不是很难,应该会才对。
C++代码如下:
class Solution {
public:
int numSquarefulPerms(vector<int>& A) {
sort(A.begin(), A.end());
vector<int> cur;
vector<bool> visited(A.size());
int res = 0;
dfs(A, visited, res, cur);
return res;
}
int squareful(int x, int y) {
int s = sqrt(x + y);
return s * s == x + y;
}
void dfs(vector<int>& A, vector<bool>& visited, int& res, vector<int>& cur) {
if (cur.size() == A.size()) {
++res;
return;
}
for (int i = 0; i < A.size(); ++i) {
if (visited[i]) continue;
if (i > 0 && !visited[i - 1] && A[i] == A[i - 1]) continue;
if (!cur.empty() && !squareful(cur.back(), A[i])) continue;
cur.push_back(A[i]);
visited[i] = true;
dfs(A, visited, res, cur);
visited[i] = false;
cur.pop_back();
}
}
};
参考资料:https://zxi.mytechroad.com/blog/searching/leetcode-996-number-of-squareful-arrays/
日期
2019 年 2 月 28 日 —— 二月最后一天
【LeetCode】996. Number of Squareful Arrays 解题报告(C++)的更多相关文章
- leetcode 996. Number of Squareful Arrays
给定一个长度小于 12 的数组 要求排列方式的种数 使得相邻和为完全平方 不考虑数学结构 将问题转化为 一笔画问题 和为完全平方代表 之间存在通路 回溯法 N^N 记忆化搜索 NN 2^N 判断是否是 ...
- 【leetcode】996. Number of Squareful Arrays
题目如下: Given an array A of non-negative integers, the array is squareful if for every pair of adjacen ...
- 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: Median of Two Sorted Arrays 解题报告
Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...
- LeetCode 349 Intersection of Two Arrays 解题报告
题目要求 Given two arrays, write a function to compute their intersection. 题目分析及思路 给定两个数组,要求得到它们之中共同拥有的元 ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】299. Bulls and Cows 解题报告(Python)
[LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 【LeetCode】518. Coin Change 2 解题报告(Python)
[LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...
- 【LeetCode】474. Ones and Zeroes 解题报告(Python)
[LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
随机推荐
- python12对象初
- 「Spark从精通到重新入门(一)」Spark 中不可不知的动态优化
前言 Apache Spark 自 2010 年面世,到现在已经发展为大数据批计算的首选引擎.而在 2020 年 6 月份发布的Spark 3.0 版本也是 Spark 有史以来最大的 Release ...
- day16 Linux三剑客之awk
day16 Linux三剑客之awk 1.什么是awk,主要作用是什么? 什么是awk,主要作用是什么? awk 主要用来处理文件,将文本按照指定的格式输出.其中包含变量,循环以及数组. 2.awk的 ...
- Hadoop 相关知识点(一)
作业提交流程(MR执行过程) Mapreduce2.x Client:用来提交作业 ResourceManager:协调集群上的计算资源的分配 NodeManager:负责启动和监控集群上的计算容器( ...
- 零基础学习java------25--------jdbc
jdbc开发步骤图 以下要用到的products表 一. JDBC简介 补充 JDBC本质:其实是官方(sun公司)定义的一套操作所有关系型数据库的规则,即接口,各个数据库厂商趋势线这个接口,提 ...
- oracle extract
select extract(year from systimestamp) year ,extract(month from systimestamp) month ...
- spring boot-jpa整合QueryDSL来简化复杂操作
spring boot-jpa整合QueryDSL来简化复杂操作 SpringDataJPA+QueryDSL玩转态动条件/投影查询
- HashMap 和 HashSet
对于HashSet而言,系统采用Hash算法决定集合元素的存储位置,这样可以保证快速存取集合元素: 对于HashMap,系统将value当成key的附属,系统根据Hash算法来决定key的存储位置,这 ...
- spring生成EntityManagerFactory的三种方式
spring生成EntityManagerFactory的三种方式 1.LocalEntityManagerFactoryBean只是简单环境中使用.它使用JPA PersistenceProvide ...
- 【Linux】【Basis】磁盘分区
1. Linux磁盘及文件系统管理 1.1. 基本概念: 1.1.1. 磁盘接口类型: IDE(ata):并口,133MB/s,设备/dev/hd[a-z] SCSI:并口,Ultrascsi320, ...