forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0996.cpp
33 lines (29 loc) · 744 Bytes
/
0996.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
int numSquarefulPerms(vector<int>& A)
{
sort(A.begin(), A.end());
int res = 0;
permute(A, 0, res);
return res;
}
private:
void permute(vector<int> A, int i, int& res)
{
if (i == A.size())
{
++res; return;
}
for (int j = i; j < A.size(); ++j)
{
if (j > i && A[i] == A[j]) continue;
swap(A[j], A[i]);
if ((i == 0) or pow((int)sqrt(A[i] + A[i-1]), 2) == A[i] + A[i-1])
{
permute(A, i + 1, res);
}
}
}
};