【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/ ...
随机推荐
- R语言实战-Part 2笔记
R 语言实战(第二版) part 2 基本方法 -------------第6章 基本图形------------------ #1.条形图 #一般是类别型(离散)变量 library(vcd) he ...
- Python基础之字典内置方法
目录 1. 字典 1.1 字典的作用 1.2 创建和使用字典 1.2.1 dict类 1.2.2 基本的字典操作 1.2.3 字典方法 1. 字典 映射:可以通过名称来访问其各个值的数据结构. 字典是 ...
- 【模板】网络最大流(EK、Dinic、ISAP)(网络流)/洛谷P3376
题目链接 https://www.luogu.com.cn/problem/P3376 题目大意 输入格式 第一行包含四个正整数 \(n,m,s,t\),分别表示点的个数.有向边的个数.源点序号.汇点 ...
- Redis数据类型内部编码规则及优化方式
Redis的每个键值都是使用一个redisObject结构体保存的,redisObject的定义如下: typedef struct redisObject { unsigned type:4; un ...
- When should we write our own copy constructor?
Reference:http://www.fredosaurus.com/notes-cpp/oop-condestructors/copyconstructors.html Please write ...
- 【Linux】【Services】【SaaS】Docker+kubernetes(12. 部署prometheus/grafana/Influxdb实现监控)
1.简介 1.1. 官方网站: promethos:https://prometheus.io/ grafana:https://grafana.com/ 1.2. 架构图 2. 环境 2.1. 机器 ...
- 常用 HTTP 状态码
下面是列举的我在项目中用到过的一些 HTTP 状态码,当然,在具体的使用中并不是用到的状态码越多越好,需要结合自己项目情况来选用适合自己的 HTTP 状态码. HTTP 状态码 含义说明 200 ...
- 最基础的SSM框架整合篇
一.简单理解 Spring.Spring MVC和MyBatis的整合主要原理就是将我们在单独使用Spring MVC和MyBatis过程中需要自己实例化的类都交由Ioc容器来管理,过程分为两步: 第 ...
- 关于python中的随机种子——random_state
random_state是一个随机种子,是在任意带有随机性的类或函数里作为参数来控制随机模式.当random_state取某一个值时,也就确定了一种规则. random_state可以用于很多函数,我 ...
- linux 让.net 控制台后台运行
命令 nohup 你的shell命令 & 例如 nohup dotnet MQTTClient.dll & 输入完成后,终端会有提示 这时再按下回车 回到shell命 ...