作者: 负雪明烛
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. 1 <= A.length <= 12
  2. 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++)的更多相关文章

  1. leetcode 996. Number of Squareful Arrays

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

  2. 【leetcode】996. Number of Squareful Arrays

    题目如下: Given an array A of non-negative integers, the array is squareful if for every pair of adjacen ...

  3. 996. Number of Squareful Arrays

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

  4. 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 ...

  5. LeetCode 349 Intersection of Two Arrays 解题报告

    题目要求 Given two arrays, write a function to compute their intersection. 题目分析及思路 给定两个数组,要求得到它们之中共同拥有的元 ...

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

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

  7. 【LeetCode】299. Bulls and Cows 解题报告(Python)

    [LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

  8. 【LeetCode】518. Coin Change 2 解题报告(Python)

    [LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...

  9. 【LeetCode】474. Ones and Zeroes 解题报告(Python)

    [LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

随机推荐

  1. R语言与医学统计图形-【27】ggplot2图形组合、字体、保存

    ggplot2绘图系统--图形组合.字体选择.保存输出 1.图形组合 一页多图在基础包中利用par和layout函数来切分画布. ggplot2是先铺好网格背景,再进行绘图,所以要通过切分网格背景来实 ...

  2. centos 7的命令变化

    1.service -> systemctl命令 2.ifconfig -> ip 命令 3.netstat -> ss命令 4.route -> ip route命令 5.t ...

  3. 论文翻译:2020_Weighted speech distortion losses for neural-network-based real-time speech enhancement

    论文地址:基于神经网络的实时语音增强的加权语音失真损失 论文代码:https://github.com/GuillaumeVW/NSNet 引用:Xia Y, Braun S, Reddy C K A ...

  4. day01互联网架构理论

  5. Maven 学习第一步[转载]

    转载至:http://www.cnblogs.com/haippy/archive/2012/07/04/2576453.html 什么是 Maven?(摘自百度百科) Maven是Apache的一个 ...

  6. jenkins之邮箱设置

  7. mysql读写分离(proxySQL) lamp+proxysql+nfs

    先在主从节点安装mysql [root@master-mariadb ~]# yum install mariadb-server -y [root@slave-mariadb ~]# yum ins ...

  8. 【Java 与数据库】How to Timeout JDBC Queries

    How to Timeout JDBC Queries JDBC queries by default do not have any timeout, which means that a quer ...

  9. 什么是maven(一)

    转自博主--一杯凉茶 我记得在搞懂maven之前看了几次重复的maven的教学视频.不知道是自己悟性太低还是怎么滴,就是搞不清楚,现在弄清楚了,基本上入门了.写该篇博文,就是为了帮助那些和我一样对于m ...

  10. for循环中的变量泄漏

    经典的案例 let arr = [] for(var i =0;i<=5;i++){ arr[i]= function fn(){ console.log(i) } } arr[0]() //6 ...